[NLP] 텍스트 전처리 - (3) 어간 추출과 표제어 추출

김규리·2022년 6월 8일
0

NLP

목록 보기
3/33
post-thumbnail

[ 어간 추출, 표제어 추출 ]

  • 정규화 방법 중 하나
  • 다른 단어지만, 같은 의미라면 하나의 단어로 일반화 > 문서 내 단어수 감소

1. 표제어(Lemma) 추출

: 단어들로부터 표제어(뿌리)를 찾는 과정, 형태학적 파싱 진행
ex. am, are, is의 표제어: be
*형태학적 파싱
(1) 어간(stem)
(2) 접사(affix)

  • NLTK: WordNetLemmatizer
from nltk.stem import WordNetLemmatizer

lemmatizer = WordNetLemmatizer()

words = ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']

print('표제어 추출 전 :',words)
print('표제어 추출 후 :',[lemmatizer.lemmatize(word) for word in words])

표제어 추출 전 : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
표제어 추출 후 : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'life', 'fly', 'dy', 'watched', 'ha', 'starting']

# WordNetLemmatizer는 입력으로 단어의 품사를 지정해 출력할 수 있음
lemmatizer.lemmatize('dies', 'v')
'die'
lemmatizer.lemmatize('watched', 'v')
'watch'
lemmatizer.lemmatize('has', 'v')
'have'

2. 어간 추출(Stemming)

  • 포터 알고리즘(Porter Algorithm)
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)
print('어간 추출 후 :',[stemmer.stem(word) for word in 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', '.']
어간 추출 후 : ['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', '.']

words = ['formalize', 'allowance', 'electricical']

print('어간 추출 전 :',words)
print('어간 추출 후 :',[stemmer.stem(word) for word in words])

어간 추출 전 : ['formalize', 'allowance', 'electricical']
어간 추출 후 : ['formal', 'allow', 'electric']
  • 랭커스터 스태머(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)
print('포터 스테머의 어간 추출 후:',[porter_stemmer.stem(w) for w in words])
print('랭커스터 스테머의 어간 추출 후:',[lancaster_stemmer.stem(w) for w in words])
어간 추출 전 : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
포터 스테머의 어간 추출 후: ['polici', 'do', 'organ', 'have', 'go', 'love', 'live', 'fli', 'die', 'watch', 'ha', 'start']
랭커스터 스테머의 어간 추출 후: ['policy', 'doing', 'org', 'hav', 'going', 'lov', 'liv', 'fly', 'die', 'watch', 'has', 'start']

Stemming
am → am
the going → the go
having → hav

Lemmatization
am → be
the going → the going
having → have

3. 한국어에서의 어간 추출

(1) 활용: 용언의 어간이 어미를 가지는 일
ㄴ 어간(stem): 활용 시, 원칙적으로 모양이 변하지 않는 부분
ㄴ 어미(end): 용언의 어간 뒤에 붙어서 활용하며 변하는 부분
(2) 규칙 활용: 활용 시, 어간 모습 일정
(3) 불규칙 활용: 활용 시, 어간의 모습이 바뀌거나 취하는 어미가 특수한 어미일 경우

profile
connecting the dots

0개의 댓글