Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Gensim字典中输入由不同标记组成的系列/列表?_Python_Dictionary_Nlp_Typeerror_Gensim - Fatal编程技术网

Python 如何在Gensim字典中输入由不同标记组成的系列/列表?

Python 如何在Gensim字典中输入由不同标记组成的系列/列表?,python,dictionary,nlp,typeerror,gensim,Python,Dictionary,Nlp,Typeerror,Gensim,我有一个pandas数据框,其中有一列包含对话数据。我用以下方式对其进行了预处理: def preprocessing(text): return [word for word in simple_preprocess(str(text), min_len = 2, deacc = True) if word not in stop_words] dataset['preprocessed'] = dataset.apply(lambda row: preprocessing(row

我有一个pandas数据框,其中有一列包含对话数据。我用以下方式对其进行了预处理:

def preprocessing(text):
     return [word for word in simple_preprocess(str(text), min_len = 2, deacc = True) if word not in stop_words]

dataset['preprocessed'] = dataset.apply(lambda row: preprocessing(row['msgText']), axis = 1)
为了使其成为一维,我使用了(两者):

以及:

processed_docs = data['preprocessed'].tolist()
现在看起来如下:

>>> processed_docs[:2]
0    ['klinkt', 'alsof', 'zwaar', 'dingen', 'spelen...
1    ['waar', 'liefst', 'meedenk', 'betekenen', 'pe...
对于这两种情况,我都使用了:

dictionary = gensim.corpora.Dictionary(processed_docs)     
然而,在这两种情况下,我都得到了错误:

TypeError: doc2bow expects an array of unicode tokens on input, not a single string
我怎样才能修改我的数据,使我不会得到这个类型错误



鉴于之前也有人问过类似的问题,我考虑过:

根据第一个答案,我尝试了以下解决方案:

dictionary = gensim.corpora.Dictionary([processed_docs.split()])
并获得错误(/s):

在第二个答案中,有人说输入需要是令牌,这对我来说已经成立了

此外,基于(),我使用了上文所述的
.tolist()
方法,这也不起作用。

我认为您需要:


dictionary=gensim.corpora.dictionary([processed_docs[:]])
这个问题很久以前就发布了,但对于仍在疑惑的人来说。 Pandas将列表存储为字符串,因此出现TypeError,将此字符串解释为列表的一种方法是使用:

from ast import literal_eval
然后:

dictionary = gensim.corpora.Dictionary()
for doc in processed_docs:
  dictionary.add_documents([literal_eval(doc)])

谢谢你和我一起思考,但是你的建议仍然会导致同样的错误,不幸的是我发现了这个问题。显然,我的序列/列表对象中有空字段。以下代码解决了我的问题:
processed\u docs=processed\u docs.dropna(axis='rows')
from ast import literal_eval
dictionary = gensim.corpora.Dictionary()
for doc in processed_docs:
  dictionary.add_documents([literal_eval(doc)])