繁体   English   中英

基于单词及其加权概率从矩阵生成文本语料库

[英]Generating text corpus from a matrix, based on words and their weighted probabilities

我有一个矩阵,我正在尝试生成文本语料库。

             chewbacca  darth  han  leia  luke  obi
chewbacca          0      0    0     0   0.66 0.33
darth              0      0    0     1     0    0
han                0      0    0     0     1    0
leia               0      0    0     0     1    0
luke               0      0    0     0     0    0
obi                0      0    0     0     0    0

我选择了chewbacca作为我的第一个词。

现在,我正在尝试根据概率为Chewbacca找到配对。 这里有两个词——luke (0.66)和obi (0.33)。

第二个词必须基于加权概率。

例如,如果“luke”与“chewbacca”配对为 0.66,“obi”与“chewbacca”配对为 0.33,则选择“luke”的可能性必须是“obi”的两倍。

如何接近它? 感谢任何提示!

如果你想创建一个二元语料库:

#remove rows that sum to 0
df = df.loc[df.sum(axis=1) != 0]
#normalizing row sum to 1
df = df.div(df.sum(axis=1), axis=0).fillna(0)
#number of bigrams you wish to generate for each row, you can change it by row as well
num_bigrams_per_word = 3
df['bigrams'] = df.apply(lambda x:[x.name+' '+s for s in np.random.choice(df.columns,p=x.values,size=num_bigrams_per_word)], axis=1)
corpus = df.bigrams.sum()

示例 output:

['chewbacca obi', 'chewbacca obi', 'chewbacca luke', 'darth leia', 'darth leia', 'darth leia', 'han luke', 'han luke', 'han luke', 'leia luke', 'leia luke', 'leia luke']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM