簡體   English   中英

獲取“doc2bow 需要輸入 unicode 令牌數組,而不是單個字符串”作為嘗試使用 gensim 執行 nlp 有解決方案嗎?

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

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 令牌數組,而不是單個字符串

corpora.Dictionary需要一個字符串列表,而您只向構造函數提供一個字符串。

您可能希望將字符串拆分為“文檔”。 這取決於您擁有的文本的性質。 在最壞的情況下,當每個“文檔”都是一個字符串時 - 您可以按標點符號拆分:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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