簡體   English   中英

類型錯誤:doc2bow 需要輸入的 unicode 標記數組,而不是使用 gensim.corpora.Dictionary() 時的單個字符串

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM