정제: 코퍼스로부터 노이즈 제거
정규화: 표현 방법이 다른 단어들을 통합시켜서 같은 단어로
USA, US
무작정 통합은 X 예로 US와 us는 다르다. 회사 이름, 사람 이름 처럼 대문자 유지해야 하는 것도 '있음.
모든 토큰을 대문자, 소문자로 하지 않고 일부만 바꾸는 방법도 존재
자연어가 아니면서 아무 의미도 갖지 않는 글자들, 또는 분석 목적에 맞지 않는 불필요 단어들을 제거해야한다.
불용어 제거, 등장 빈도가 적은 단어, 길이가 짧은 단어 제거 등이 있음.
서로 다른 단어들이지만, 하나의 단어로 일반화 가능하다면 일반화 하여 단어 수 줄이기.
표제어(Lemma): 기본 사전형 단어
are is am 의 표제어 => be
형태소: 의미를 가진 가장 작은 단위
1)어간(stem): 단어의 의미를 담고있는 핵심 부분
2)접사(affix): 단어에 추가적인 의미를 주는 부분
dogs -> dog = 어간, s = 접사
NLTK 에서는 WordNetLemmatizer 사용
from nltk.stem import WordNetLemmatizer
n=WordNetLemmatizer()
words=['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print([n.lemmatize(w) for w in words])
#['policy', 'doing', 'organization', 'have', 'going', 'love', 'life', 'fly', 'dy', 'watched', 'ha', 'starting']
lemmatizer는 품사 정보를 알아야 정확한 결과를 얻을 수 있다. 없으면 dies 를 dy has가 ha로 나온 것 처럼 알 수없는 단어로 출력된다.
n.lemmatize('dies', 'v')
#->'die'
표제어 추출은 문맥을 고려, 단어의 품사 정보를 보존
어간 추출은 품사 정보 보존 되지 않음. (사전에 존재하지 않는 단어일 경우가 많다.
Stemming
---------
am -> am
the going -> the go
having -> hav
Lemmatization
--------------
am -> be
the going -> the going
having -> have
다양한 알고리즘 존재
포터 알고리즘
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
s = PorterStemmer()
text="This was not the map we found in Billy Bones's chest, but an accurate copy, complete in all things--names and heights and soundings--with the single exception of the red crosses and the written notes."
words=word_tokenize(text)
print(words)
#['This', 'was', 'not', 'the', 'map', 'we', 'found', 'in', 'Billy', 'Bones', "'s", 'chest', ',', 'but', 'an', 'accurate', 'copy', ',', 'complete', 'in', 'all', 'things', '--', 'names', 'and', 'heights', 'and', 'soundings', '--', 'with', 'the', 'single', 'exception', 'of', 'the', 'red', 'crosses', 'and', 'the', 'written', 'notes', '.']
print([s.stem(w) for w in words])
#['thi', 'wa', 'not', 'the', 'map', 'we', 'found', 'in', 'billi', 'bone', "'s", 'chest', ',', 'but', 'an', 'accur', 'copi', ',', 'complet', 'in', 'all', 'thing', '--', 'name', 'and', 'height', 'and', 'sound', '--', 'with', 'the', 'singl', 'except', 'of', 'the', 'red', 'cross', 'and', 'the', 'written', 'note', '.']
ALIZE -> AL
ANCE -> 제거
ICAL -> IC
formalize -> formal
allowance -> allow
electrical -> electric
랭케스터 스태머(Lancaster Stemmer)
from nltk.stem import LancasterStemmer
l=LancasterStemmer()
words=['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print([l.stem(w) for w in words])
#['policy', 'doing', 'org', 'hav', 'going', 'lov', 'liv', 'fly', 'die', 'watch', 'has', 'start']
여러가지 알고리즘을 써보고 적합한 것 판단해서 쓰자.
5언 9품사
|언|품사|
|---|---|
|체언 |명사, 대명사, 수사|
|수식언|관형사, 부사|
|관계언|조사|
|독립언|감탄사|
|용언 |동사, 형용사|
활용(conjugation): 용언의 어간(stem)과 어미(ending)을 가지는 일
어간(stem):용언을 활용할 때 원칙적으로 모양이 변하지 않는 부분, 어미에 선행하는 부분. 어간의 모양 또한 바뀔 수는 있음. (긋다, 긋고, 그어서, 그어라)
어미(ending): 용언의 어간 뒤에 붙어서 활용하면서 변하는 부분.
어간이 어미를 취할 때 어간의 모습이 일정 할 경우
잡/어간 + 다/어미 => 잡다
어미를 단순 분리 하면 어간 추출 가능
어간이 어미를 취할 때 어간의 모습이 바뀌거나 취하는 어미가 특수할 경우
예를 들어 ‘듣-, 돕-, 곱-, 잇-, 오르-, 노랗-’ 등이 ‘듣/들-, 돕/도우-, 곱/고우-, 잇/이-, 올/올-, 노랗/노라-’와 같이 어간의 형식이 달라지는 일이 있거나 ‘오르+ 아/어→올라, 하+아/어→하여, 이르+아/어→이르러, 푸르+아/어→푸르러’와 같이 일반적인 어미가 아닌 특수한 어미를 취하는 경우 불규칙활용을 하는 예에 속합니다.
이 경우에는 어간이 어미가 붙는 과정에서 어간의 모습이 바뀌었으므로 단순한 분리만으로 어간 추출이 되지 않고 좀 더 복잡한 규칙을 필요로 합니다.
아래의 링크는 다양한 불규칙 활용의 예를 보여줍니다.
링크 : https://namu.wiki/w/한국어/불규칙%20활용