NLTK로 불용어 제거
nltk 툴킷을 사용하여 불용어를 제거하여 사용자가 입력 한 텍스트를 처리하려고하는데 불용어 제거를 사용하면 'and', 'or', 'not'과 같은 단어가 제거됩니다. 이 단어는 나중에 텍스트를 쿼리로 처리하는 데 필요한 연산자이므로 불용어 제거 프로세스 후에 존재하기를 원합니다. 텍스트 쿼리에서 연산자가 될 수있는 단어가 무엇인지 모르겠고 텍스트에서 불필요한 단어를 제거하고 싶습니다.
불용어 목록에서 제거하는 연산자 단어 목록을 직접 만드는 것이 좋습니다. 세트는 편리하게 뺄 수 있습니다.
operators = set(('and', 'or', 'not'))
stop = set(stopwords...) - operators
그런 다음 연산자가 불용어 목록의 일부인지 여부에 의존하지 않고 단어가 in또는 not in집합 인지 간단히 테스트 할 수 있습니다 . 그런 다음 나중에 다른 불용어 목록으로 전환하거나 연산자를 추가 할 수 있습니다.
if word.lower() not in stop:
# use word
NLTK11 개 언어 (Porter 등)에 대한 2,400 개의 불용어로 구성된 내장 불용어 목록이 있습니다. http://nltk.org/book/ch02.html을 참조 하십시오.
>>> from nltk import word_tokenize
>>> from nltk.corpus import stopwords
>>> stop = set(stopwords.words('english'))
>>> sentence = "this is a foo bar sentence"
>>> print([i for i in sentence.lower().split() if i not in stop])
['foo', 'bar', 'sentence']
>>> [i for i in word_tokenize(sentence.lower()) if i not in stop]
['foo', 'bar', 'sentence']
불용어를 제거하려면 tf-idf를 사용하는 것이 좋습니다. 형태소 분석이 빈도라는 용어에 미치는 영향을 참조하세요 .
@alvas의 답변은 작업을 수행하지만 훨씬 더 빠르게 수행 할 수 있습니다. 당신이 가지고 있다고 가정하면 documents: 문자열 목록.
from nltk.corpus import stopwords
from nltk.tokenize import wordpunct_tokenize
stop_words = set(stopwords.words('english'))
stop_words.update(['.', ',', '"', "'", '?', '!', ':', ';', '(', ')', '[', ']', '{', '}']) # remove it if you need punctuation
for doc in documents:
list_of_words = [i.lower() for i in wordpunct_tokenize(doc) if i.lower() not in stop_words]
여기에서 목록이 아닌 세트로 검색한다는 사실 때문에 속도는 이론적으로 len(stop_words)/2더 빠르며, 이는 많은 문서를 처리해야하는 경우에 중요합니다.
약 300 단어로 된 5000 문서의 경우 차이는 내 예의 경우 1.8 초이고 @alvas의 경우 20 초입니다.
PS 대부분의 경우 tf-idf가 사용되는 다른 분류 작업을 수행하기 위해 텍스트를 단어로 분할해야합니다. 따라서 형태소 분석기도 사용하는 것이 좋습니다.
from nltk.stem.porter import PorterStemmer
porter = PorterStemmer()
[porter.stem(i.lower()) for i in wordpunct_tokenize(doc) if i.lower() not in stop_words]루프 내부 에서 사용 합니다.
@alvas는 좋은 대답을 가지고 있습니다. 그러나 다시 말하지만 그것은 작업의 성격에 따라 달라집니다. 예를 들어 응용 프로그램에서 모두 conjunction예를 들어 , 또는 모두를 고려하고 싶지만 모든 determiner예를 들어 , a, 일부, 대부분, 모든 예를 들어 모든 것을 고려하는 중지 단어로 간주하려는 경우 다른 품사를 합법적 인 것으로 간주하는 경우 품사 태그 집합을 사용하여 단어를 삭제하는이 솔루션을 살펴볼 수 있습니다 . 표 5.1을 확인하십시오 .
import nltk
STOP_TYPES = ['DET', 'CNJ']
text = "some data here "
tokens = nltk.pos_tag(nltk.word_tokenize(text))
good_words = [w for w, wtype in tokens if wtype not in STOP_TYPES]
내장 NLTK 불용어 목록과 함께 string.punctuation 을 사용할 수 있습니다 .
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from string import punctuation
words = tokenize(text)
wordsWOStopwords = removeStopWords(words)
def tokenize(text):
sents = sent_tokenize(text)
return [word_tokenize(sent) for sent in sents]
def removeStopWords(words):
customStopWords = set(stopwords.words('english')+list(punctuation))
return [word for word in words if word not in customStopWords]
NLTK stopwords complete list
참고URL : https://stackoverflow.com/questions/19130512/stopword-removal-with-nltk
'IT TIP' 카테고리의 다른 글
| 'git push heroku master'가 여전히 인증을 요청합니다. (0) | 2020.10.30 |
|---|---|
| JavaScript 객체 리터럴에서 변수 키를 사용하는 방법이 있습니까? (0) | 2020.10.30 |
| AppStore-앱 상태가 판매 준비가되었지만 앱 스토어에 없습니다. (0) | 2020.10.29 |
| XSLT를 사용하여 XML에서 CSV로 (0) | 2020.10.29 |
| C #을 사용하여 레지스트리 값이 있는지 확인하는 방법은 무엇입니까? (0) | 2020.10.29 |