2013-12-23 2 views
5

document classification, as described in NLTK Chapter 6을하려고하는데 불용어 제거에 문제가 있습니다. 내가NLTK 스톱 워드 제거 문제

all_words = (w for w in all_words if w not in nltk.corpus.stopwords.words('english')) 

를 추가 할 때 나는 그들이() 함수는 쓸모 .KEY 렌더링, 스톱 워드 코드는 'all_words'에 사용되는 객체의 유형을 변경 한 같은데요

Traceback (most recent call last): 
    File "fiction.py", line 8, in <module> 
    word_features = all_words.keys()[:100] 
AttributeError: 'generator' object has no attribute 'keys' 

를 반환합니다. 유형을 변경하지 않고 키 기능을 사용하기 전에 중지 단어를 제거하려면 어떻게해야합니까? 아래의 전체 코드 :

import nltk 
from nltk.corpus import PlaintextCorpusReader 

corpus_root = './nltk_data/corpora/fiction' 
fiction = PlaintextCorpusReader(corpus_root, '.*') 
all_words=nltk.FreqDist(w.lower() for w in fiction.words()) 
all_words = (w for w in all_words if w not in nltk.corpus.stopwords.words('english')) 
word_features = all_words.keys()[:100] 

def document_features(document): # [_document-classify-extractor] 
    document_words = set(document) # [_document-classify-set] 
    features = {} 
    for word in word_features: 
     features['contains(%s)' % word] = (word in document_words) 
    return features 

print document_features(fiction.words('fic/11.txt')) 

답변

4
내가 처음에 FreqDist 예에 추가 피하는하여이 작업을 수행 할 것

: 당신의 신체의 크기에 따라

all_words=nltk.FreqDist(w.lower() for w in fiction.words() if w.lower() not in nltk.corpus.stopwords.words('english')) 

난 당신이 아마를 얻을 거라고 생각 일을하기 전에 중지 단어에 대한 집합을 만드는 중 성능 향상이 있음 :

stopword_set = frozenset(ntlk.corpus.stopwords.words('english')) 

그 상황에 적합하지 있다면, 당신이 FA를 활용할 수처럼 보인다 해당 FreqDistdict :

for stopword in nltk.corpus.stopwords.words('english'): 
    if stopword in all_words: 
     del all_words[stopword] 
+0

에서 상속됩니다. 고맙습니다! – user3128184

관련 문제