본문 바로가기
파이썬

파이썬 NLTK 불용어 목록

by º기록 2020. 12. 18.
반응형

아래에 코드가 있고 단어 목록에 불용어 목록을 적용하려고합니다. 그러나 결과는 여전히 "a"및 "the"와 같은 단어를 보여 주며이 과정에서 제거되었을 것이라고 생각했습니다. 무엇이 잘못되었는지 어떤 아이디어라도 좋을 것입니다.

import nltk
from nltk.corpus import stopwords

word_list = open("xxx.y.txt", "r")
filtered_words = [w for w in word_list if not w in stopwords.words('english')]
print filtered_words

 

해결 방법

 

몇 가지 주목할 점이 있습니다.

목록에 대한 멤버십을 반복해서 확인하려는 경우 목록 대신 세트를 사용합니다.

stopwords.words ( 'english') 소문자 불용어 목록을 반환합니다. 출처에 대문자가 포함되어있어 일치하지 않을 가능성이 큽니다.

파일을 제대로 읽지 않고 공백으로 구분 된 단어 목록이 아닌 파일 개체를 확인하고 있습니다.

함께 모아서:

import nltk
from nltk.corpus import stopwords

word_list = open("xxx.y.txt", "r")
stops = set(stopwords.words('english'))

for line in word_list:
    for w in line.split():
        if w.lower() not in stops:
            print w

 

참조 페이지 https://stackoverflow.com/questions/22763224

 

 

반응형

댓글