【问题标题】:CountVectorizer(): StreamBackedCorpusView' object has no attribute 'lower'CountVectorizer(): StreamBackedCorpusView' 对象没有属性 'lower'
【发布时间】:2017-09-04 23:36:58
【问题描述】:

我正在尝试使用以下代码在 NLTK 电影评论语料库上运行和实例化 CountVectorizer():

>>>import nltk
>>>import nltk.corpus
>>>from sklearn.feature_extraction.text import CountVectorizer
>>>from nltk.corpus import movie_reviews
>>>neg_rev = movie_reviews.fileids('neg')
>>>pos_rev = movie_reviews.fileids('pos')
>>>rev_list = [] # Empty List
>>>for rev in neg_rev:
    rev_list.append(nltk.corpus.movie_reviews.words(rev))
>>>for rev_pos in pos_rev:
    rev_list.append(nltk.corpus.movie_reviews.words(rev_pos))
>>>count_vect = CountVectorizer()
>>>X_count_vect = count_vect.fit_transform(rev_list)

我收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-37-00e9047daa67> in <module>()
----> 1 X_count_vect = count_vect.fit_transform(rev_list)

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py in fit_transform(self, raw_documents, y)
    837 
    838         vocabulary, X = self._count_vocab(raw_documents,
--> 839                                           self.fixed_vocabulary_)
    840 
    841         if self.binary:

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py in _count_vocab(self, raw_documents, fixed_vocab)
    760         for doc in raw_documents:
    761             feature_counter = {}
--> 762             for feature in analyze(doc):
    763                 try:
    764                     feature_idx = vocabulary[feature]

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py in <lambda>(doc)
    239 
    240             return lambda doc: self._word_ngrams(
--> 241                 tokenize(preprocess(self.decode(doc))), stop_words)
    242 
    243         else:

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py in <lambda>(x)
    205 
    206         if self.lowercase:
--> 207             return lambda x: strip_accents(x.lower())
    208         else:
    209             return strip_accents

AttributeError: 'StreamBackedCorpusView' object has no attribute 'lower'

nltk.corpus.movie_reviews.words(rev_pos) 有分词句子.... 比如:

['films', 'adapted', 'from', 'comic', 'books', 'have', ...]

谁能告诉我我做错了什么?我想我在创建 (rev_list) 的电影评论列表时有些错误。

TIA

【问题讨论】:

  • 您应该检查要附加到列表中的类型nltk.corpus.movie_reviews.words(rev_pos)。应该是CountVectorizer要处理的字符串,目前我认为不是。

标签: python scikit-learn nltk countvectorizer


【解决方案1】:

看起来你的 .words() 函数实际上并没有给你一个令牌列表,而是一系列 StreamBackedCorpusView 类。此类允许您检索令牌,但实际上并不是令牌本身的完整表示。

但是,您可以从视图中检索令牌。有关使用 StreamBackCorpusView 的更多详细信息,请参阅以下链接。

http://nltk.sourceforge.net/corpusview/corpusview.StreamBackedCorpusView-class.html

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 2019-05-27
    • 1970-01-01
    • 2021-02-10
    • 2020-10-22
    • 2020-09-22
    • 2019-03-19
    • 2021-08-23
    • 2021-11-20
    相关资源
    最近更新 更多