[英]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.