TypeError: doc2bow expects an array of unicode tokens on input, not a single string

 

使用Gensim包下的corpora构造词典,报错:

 

Traceback (most recent call last):
  File "D:\BaiduNetdiskDownload\sample.py", line 38, in <module>
    dictionary = corpora.Dictionary(allwords)
  File "D:\soft\Python27\lib\site-packages\gensim\corpora\dictionary.py", line 79, in __init__
    self.add_documents(documents, prune_at=prune_at)
  File "D:\soft\Python27\lib\site-packages\gensim\corpora\dictionary.py", line 195, in add_documents
    self.doc2bow(document, allow_update=True)  # ignore the result, here we only care about updating token ids
  File "D:\soft\Python27\lib\site-packages\gensim\corpora\dictionary.py", line 233, in doc2bow
    raise 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

 

 

出错代码如下:

####分词
real_documents = []
stopwords = dt.LoadStopwords("stopwords.txt")
cut_documents = [list(jieba.cut(item_text,cut_all=False)) for item_text in tqdm(real_test_raw)]
allwords = []
for sentence in cut_documents:
    outstr = ''
    for word in sentence:
        if word not in stopwords:
            outstr += word
            outstr += ' '
            allwords.append(word)
    real_documents.append(outstr.strip())
###保存分词结果
#dt.SaveList2File("afterJson.txt",real_documents)
###构造词典
#for i in allwords:
#    print type(i),i
dictionary = corpora.Dictionary(allwords)

 

修改:将最后一行代码dictionary = corpora.Dictionary(allwords),改为下述:

 

dictionary = corpora.Dictionary([allwords])

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值