【问题标题】:Create word dictionary for sentences in a list为列表中的句子创建词典
【发布时间】:2020-01-18 06:39:54
【问题描述】:

我有一个句子列表

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}

【问题讨论】:

    标签: python pandas scikit-learn nltk countvectorizer


    【解决方案1】:

    您需要展平嵌套列表:

    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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2019-02-13
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      相关资源
      最近更新 更多