【问题标题】:Getting "doc2bow expects an array of unicode tokens on input, not a single string" as a try to do nlp using gensim" Is there a solution?获取“doc2bow 需要输入的 unicode 令牌数组,而不是单个字符串”作为尝试使用 gensim 执行 nlp” 有解决方案吗?
【发布时间】:2021-01-23 23:33:46
【问题描述】:
import gensim  
LDA = gensim.models.ldamodel.LdaModel 
dictionnary = corpora.Dictionary(docCleaned) #Error message appears here!!!
doc_term_matrix = [dictionary.doc2bow(doc) for doc in docCleaned]

错误信息 ->

TypeError: doc2bow 需要输入的 unicode 标记数组,而不是 单个字符串

【问题讨论】:

  • 请考虑将答案标记为已接受(答案左侧的灰色勾号),如果您觉得有用,请点赞。

标签: python dictionary nlp gensim lda


【解决方案1】:

corpora.Dictionary 需要一个 list 字符串,而您只向构造函数提供一个字符串。

您可能希望将字符串拆分为“文档”。这取决于您拥有的文本的性质。在最坏的情况下,当每个“文档”都是一个字符串时 - 您可以按标点符号拆分:

import string
import re
dictionnary = corpora.Dictionary(re.split('[' + re.escape(string.punctuation) + ']', docCleaned))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多