![](/img/trans.png)
[英]Gensim: TypeError: doc2bow expects an array of unicode tokens on input, not a single string
[英]TypeError: doc2bow expects an array of unicode tokens on input, not a single string when using gensim.corpora.Dictionary()
有一个像这样的数据框:
index terms
1345 ['jays', 'place', 'great', 'subway']
1543 ['described', 'communicative', 'friendly']
9874 ['great', 'sarahs', 'apartament', 'back']
2456 ['great', 'sarahs', 'apartament', 'back']
我尝试从评论语料库 ['terms'] 创建字典,但我遇到了错误消息!
from gensim import corpora, models
dictionary = corpora.Dictionary( comments['terms'] )
TypeError: doc2bow expects an array of unicode tokens on input, not a single string
每个索引都需要将其术语放在一个子列表中,所有这些都嵌套在更大的列表中。
theterms = [['jays', 'place', 'great', 'subway'],['described', 'communicative', 'friendly'], ['great', 'sarahs', 'apartament', 'back'],['great', 'sarahs', 'apartament', 'back']]
dictionary = corpora.Dictionary(theterms)
首先使用comments['terms'].tolist()
将comments['terms']
转换为一个列表,然后运行语料库,它应该可以工作。 在创建字典之前,您可以进行其他预处理,如词干提取或停用词删除等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.