为列表中的句子创建单词词典

Create word dictionary for sentences in a list

我有一个句子列表

a = [['i am a testing'],['we are working on project']]

我正在尝试为列表中的所有句子创建一个单词词典。我试过了

vectorizer = CountVectorizer()
vectorizer.fit_transform(a)
coffee_dict2 = vectorizer.vocabulary_

我收到一个错误 AttributeError: 'list' object has no attribute 'lower'

我期待的结果是一本字典

{'i': 1, 'am': 1, 'testing': 2}

您需要展平嵌套列表:

from sklearn.feature_extraction.text import CountVectorizer
coffee_reviews_test = [['i am a testing'],['we are working on project']]

from  itertools import chain

vectorizer = CountVectorizer()
vectorizer.fit_transform(chain.from_iterable(coffee_reviews_test))

另一个解决方案:

vectorizer.fit_transform([x for y in coffee_reviews_test for x in y])

coffee_dict2 = vectorizer.vocabulary_
print (coffee_dict2)
{'am': 0, 'testing': 4, 'we': 5, 'are': 1, 'working': 6, 'on': 2, 'project': 3}