텍스트 정제

Python으로 배우는 NLP 피처 엔지니어링

Rounak Banik

Data Scientist

텍스트 정제 기법

  • 불필요한 공백과 이스케이프 시퀀스
  • 구두점
  • 특수문자(숫자, 이모지 등)
  • 불용어
Python으로 배우는 NLP 피처 엔지니어링

isalpha()

"Dog".isalpha()
True
"3dogs".isalpha()
False
"12347".isalpha()
False
"!".isalpha()
False
"❤".isalpha()
False
Python으로 배우는 NLP 피처 엔지니어링

주의 사항

  • 약어: U.S.A, U.K
  • 고유명사: word2vec, xto10x
  • 미묘한 사례는 정규식을 써서 사용자 정의 함수로 처리하십시오.
Python으로 배우는 NLP 피처 엔지니어링

알파벳이 아닌 문자 제거

string = """
OMG!!!! This is like    the best thing ever \t\n. 
Wow, such an amazing song! I'm hooked. Top 5 definitely. ❤
"""

import spacy # Generate list of tokens nlp = spacy.load('en_core_web_sm') doc = nlp(string) lemmas = [token.lemma_ for token in doc]
Python으로 배우는 NLP 피처 엔지니어링

알파벳이 아닌 문자 제거

...
...
# Remove tokens that are not alphabetic
a_lemmas = [lemma for lemma in lemmas 
            if lemma.isalpha() or lemma == '-PRON-']

# Print string after text cleaning print(' '.join(a_lemmas))
'omg this be like the good thing ever wow such an amazing song -PRON- be hooked top definitely'
Python으로 배우는 NLP 피처 엔지니어링

불용어(Stopwords)

  • 매우 자주 등장하는 단어
  • 예: 관사, be 동사, 대명사 등
Python으로 배우는 NLP 피처 엔지니어링

spaCy로 불용어 제거

# Get list of stopwords
stopwords = spacy.lang.en.stop_words.STOP_WORDS

string = """ OMG!!!! This is like the best thing ever \t\n. Wow, such an amazing song! I'm hooked. Top 5 definitely. ❤ """
Python으로 배우는 NLP 피처 엔지니어링

spaCy로 불용어 제거

...
...
# Remove stopwords and non-alphabetic tokens
a_lemmas = [lemma for lemma in lemmas 
            if lemma.isalpha() and lemma not in stopwords]
# Print string after text cleaning
print(' '.join(a_lemmas))
'omg like good thing wow amazing song hooked definitely'
Python으로 배우는 NLP 피처 엔지니어링

기타 전처리 기법

  • HTML/XML 태그 제거
  • 악센트 문자(예: é) 치환
  • 철자 교정
Python으로 배우는 NLP 피처 엔지니어링

주의 사항

항상 용도에 맞는 전처리만 사용하십시오.

Python으로 배우는 NLP 피처 엔지니어링

Let's practice!

Python으로 배우는 NLP 피처 엔지니어링

Preparing Video For Download...