rueki

텍스트 전처리 - 불용어(Stopword) 본문

DL/NLP

텍스트 전처리 - 불용어(Stopword)

륵기 2019. 7. 1. 15:53
728x90
반응형

본문은 https://wikidocs.net/22592 를 참고해서 작성한 글입니다.

 

 

문장에서 자주 등장은 하지만, 큰 의미가 없는 단어들은 제거해야 문장 분석이 잘 이루어진다.

이에 대한 예로 I, My, ME, Mine과 같은 경우는 수백수천번이 나와도 실질적인 의미가 없어서

문장 분석에 별 도움이 안된다.

이를 불용어(Stopword)라 하며, NLTK에서는 약 100여개의 영단어의 불용어 패키지가 있다.

from nltk.corpus import stopwords
nltk.download('stopwords')
stopwords.words('english')[:10]
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're"]

100여개 중에서 10개정도만 뽑아왔으며, 영어에서 주로 불용어로 무엇을 나타내는지 볼 수 있다.

이제 실제로 불용어를 제거해보자

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
ex = "You are my destiny. It's my own things"
stop_words = set(stopwords.words('english'))

word_tokens = word_tokenize(ex)
res = []
for w in word_tokens:
    if w not in stop_words:
     res.append(w)

    
print(word_tokens)
print(res)
['You', 'are', 'my', 'destiny', '.', 'It', "'s", 'my', 'own', 'things']
['You', 'destiny', '.', 'It', "'s", 'things']

토근화했을 때는 문장의 단어들을 각각 분리했다면, 불용어 제거를 했을 시에, 필요 명사만 남게 되었다.

 

이제 한국어로 제거를 해보자

import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
ex = "아니, 고기를 왜 그따위로 굽는거야. 그럴거면 집게를 나한테 주던가."
stop_words = "그따위로 그럴거면 아무거나 아무렇게나 어찌하든지 같다 비슷하다 예컨대 이럴정도로 하면 아니거든" #불용어 임시 리스트
words = word_tokenize(ex)
stop_words = stop_words.split(' ')#공백 기준 나누기, 불용어 리스트 생성
result = []

for w in words:
    if w not in stop_words:
        result.append(w)
        
print(words)
print(result)
['아니', ',', '고기를', '왜', '그따위로', '굽는거야', '.', '그럴거면', '집게를', '나한테', '주던가', '.']
['아니', ',', '고기를', '왜', '굽는거야', '.', '집게를', '나한테', '주던가', '.']

임시로 한국 불용어 리스트를 만들어 적용시켜본 결과 정의한대로 없어진 것을 확인할 수가 있다.

한국어 불용어 제거는 따로 정의해서 불러오는 것이 좋다고한다. 예를 들어 txt나 csv로.

불용어 리스트는 구글에 찾아보면 자세히 나와있으니 참고.

728x90
반응형
Comments