# 어간 추출(Stemming)과 표제어 추출(Lemmatization)
- 눈으로 봤을 때는 서로 다른 단어들이지만, 하나의 단어로 일반화시킬 수 있다면 하나의 단어로 일반화시켜서 문서 내의 단어 수를 줄이기 위한 작업들
- BoW 표현을 사용하는 자연어 처리 문제에서 주로 사용
# 표제어 추출(Lemmatization)
- 표제어(Lemma) = 기본 사전형 단어
- 포제어 추출 = 단어들이 다른 형태를 가지더라도 그 뿌리 단어를 찾아가서 단어의 개수를 줄일 수 있는지 판단
- 표제어 추출을 하는 가장 섬세한 방법 = 형태학적 파싱을 먼저 진행하는 것
- 형태학(morphology) = 형태소로부터 단어들을 만들어가는 학문
- 형태소의 종류로는 어간(stem)과 접사(affix)가 존재
- 어간(stem) = 단어의 의미를 담고 있는 단어의 핵심 부분
- 접사(affix) = 단어에 추가적인 의미를 주는 부분
- 형태학적 파싱 = 어간과 접사로 분리하는 작업
ex) cats => cat(어간)+s(접사) - NLTK에서 표제어 추출을 위한 도구인 WordNetLemmatizer 지원
- 표제어 추출기가 본래 단어의 품사 정보를 알아야만 정확한 결과를 얻을 수 있기 때문에 의미를 알 수 있는 적절하지 못한 단어를 출력하기도 함
ex) dies => 'dy' / has => 'ha'
=> WordNetLemmatizer를 이용해 입력으로 동사 품사라는 사실을 알려주어 정확한 표제어를 출력할 수 있음
- 표제어 추출기가 본래 단어의 품사 정보를 알아야만 정확한 결과를 얻을 수 있기 때문에 의미를 알 수 있는 적절하지 못한 단어를 출력하기도 함
import nltk
nltk.download('wordnet')
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
words = ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print('표제어 추출 전 :',words)
# 표제어 추출 전 : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print('표제어 추출 후 :',[lemmatizer.lemmatize(word) for word in words])
# 표제어 추출 후 : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'life', 'fly', 'dy', 'watched', 'ha', 'starting']
print(lemmatizer.lemmatize('dies', 'v')) # die
print(lemmatizer.lemmatize('watched', 'v')) # watch
print(lemmatizer.lemmatize('has', 'v')) # have
# 어간 추출(Stemming)
- 형태학적 분석을 단순화한 버전이라고 볼 수도 있고, 정해진 규칙만 보고 단어의 어미를 자르는 어림짐작의 작업이라고 볼 수도 있음
- 섬세한 작업이 아니기 때문에 어간 추출 후에 나오는 결과 단어가 사전에 존재하지 않는 단어일 수도 있음
- 포터 알고리즘(Porter Algorithm)
- 어간 추출 알고리즘 중 하나
- alize => al / ance => 제거 / ical => ic
ex) formalize => formal - 어간 추출 속도는 표제어 추출보다 일반적으로 빠른데, 포터 어간 추출기는 정밀하게 설계되어 정확도가 높으므로 영어 자연어 처리에서 어간 추출을 하고자 한다면 가장 준수한 선택임
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
stemmer = PorterStemmer()
sentence = "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."
tokenized_sentence = word_tokenize(sentence)
print('어간 추출 전 :', tokenized_sentence)
# 어간 추출 전 : ['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('어간 추출 후 :',[stemmer.stem(word) for word in tokenized_sentence])
# 어간 추출 후 : ['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', '.']
- 랭커스터 스태머 알고리즘(Lancaster Stemmer)
- 포터 스태머 알고리즘과 랭커스터 스태머 알고지름은 서로 다른 알고리즘을 사용하기 때문에 다른 결과를 보여줌
from nltk.stem import PorterStemmer
from nltk.stem import LancasterStemmer
porter_stemmer = PorterStemmer()
lancaster_stemmer = LancasterStemmer()
words = ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print('어간 추출 전 :', words)
# 어간 추출 전 : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print('포터 스테머의 어간 추출 후:',[porter_stemmer.stem(w) for w in words])
# 포터 스테머의 어간 추출 후: ['polici', 'do', 'organ', 'have', 'go', 'love', 'live', 'fli', 'die', 'watch', 'ha', 'start']
print('랭커스터 스테머의 어간 추출 후:',[lancaster_stemmer.stem(w) for w in words])
# 랭커스터 스테머의 어간 추출 후: ['policy', 'doing', 'org', 'hav', 'going', 'lov', 'liv', 'fly', 'die', 'watch', 'has', 'start']
# 참고글
'딥러닝' 카테고리의 다른 글
[개념] 딥러닝 학습 방법 이해하기 (0) | 2023.01.31 |
---|---|
[개념] 한국어 전처리 패키지(Text Preprocessing Tools for Korean Text) (0) | 2023.01.25 |
[개념] 토큰화(Tokenization) (0) | 2023.01.20 |
[개념] 문서 단어 행렬(Document-Term Matrix, DTM) (0) | 2023.01.20 |
[개념] Bag of Words (0) | 2023.01.19 |