【问题标题】:TypeError: doc2bow expects an array of unicode tokens on input, not a single string when using gensim.corpora.Dictionary()TypeError: doc2bow 在使用 gensim.corpora.Dictionary() 时需要输入的 unicode 标记数组,而不是单个字符串
【发布时间】:2021-05-27 06:48:37
【问题描述】:

有一个这样的数据框:

  index  terms   
  1345  ['jays', 'place', 'great', 'subway']    
  1543  ['described', 'communicative', 'friendly']    
  9874  ['great', 'sarahs', 'apartament', 'back']    
  2456  ['great', 'sarahs', 'apartament', 'back']  

我尝试从 cmets[ '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

【问题讨论】:

标签: python dictionary gensim


【解决方案1】:

每个索引都需要将其术语放在一个子列表中,所有这些都嵌套在更大的列表中。

theterms = [['jays', 'place', 'great', 'subway'],['described', 'communicative', 'friendly'], ['great', 'sarahs', 'apartament', 'back'],['great', 'sarahs', 'apartament', 'back']] 

dictionary = corpora.Dictionary(theterms)

【讨论】:

    【解决方案2】:

    首先使用comments['terms'].tolist()comments['terms'] 转换为列表,然后运行语料库,它应该可以工作。在创建词典之前,您可以进行其他预处理,例如词干提取或停用词删除等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多