[NLP] 데이터 전처리 - 정제 / 정규화

2023. 9. 27. 14:33ML&DL/NLP


[NLP] 전처리 - 정제 / 정규화

 

정제

- 노이즈 데이터 제거하는 것

정규화

- 표현이 다른 단어를 같은 단어로 통합하는 것

 

정제를 하는 이유 ?

- 유의미한 단어만 추출하기 위해서는 노이즈 데이터를 제거해줘야함

- 노이즈 데이터는 빈도가 적은 단어, 의미를 갖지 않은 글자, 특수 문자 등 불필요한 데이터를 의미함

- 보통 불용어 제거라고 함

 

한국어 불용어 리스트 참고

- https://www.ranks.nl/stopwords/korean

- https://mr-doosun.tistory.com/24

 

NLTK

-  영어는 NLTK 에서 제공하는 불용어 리스트를 사용해 불용어를 제거할 수 있음

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize 
from konlpy.tag import Okt

example = "Family is not an important thing. It's everything."
stop_words = set(stopwords.words('english')) 

word_tokens = word_tokenize(example)

result = []
for word in word_tokens: 
    if word not in stop_words: 
        result.append(word) 

print('불용어 제거 전 :',word_tokens) 
print('불용어 제거 후 :',result)

- 한국어는 불용어 리스트를 제공하지 않기 때문에 사용자가 직접 리스트를 만들어서 사용하는 경우가 많음

okt = Okt()

example = "고기를 아무렇게나 구우려고 하면 안 돼. 고기라고 다 같은 게 아니거든. 예컨대 삼겹살을 구울 때는 중요한 게 있지."
stop_words = "를 아무렇게나 구 우려 고 안 돼 같은 게 구울 때 는"

stop_words = set(stop_words.split(' '))
word_tokens = okt.morphs(example)

result = [word for word in word_tokens if not word in stop_words]

print('불용어 제거 전 :',word_tokens)
print('불용어 제거 후 :',result)