๐ฌ ์ต๋ํ ๋งค๋๋ฝ๊ฒ ํด์ํ๊ณ ์ ๋ ธ๋ ฅํ์ง๋ง ์ด์ํ ๋ฌธ์ฅ์ด ์์ ์ ์์ต๋๋ค. ํผ๋๋ฐฑ์ ์ธ์ ๋ ํ์์ ๋๋ค ๐
์๋ณธ ๊ธ ์ฃผ์ : https://medium.com/geekculture/introduction-to-stemming-and-lemmatization-nlp-3b7617d84e65
์์ฐ์ด์ฒ๋ฆฌ(Natural Language Processing, NLP)
ํ ์คํธ ๋ฐ์ดํฐ๋ ๋ค์ํ ์์ค๋ก๋ถํฐ ์ป์ ์ ์๋ค. ์ฐ๋ฆฌ์ ๋ชฉํ๋ ํ์คํฌ์ ๊ด๋ จ์๋ ์์ค ๊ณ ์ ์ ๋งํฌ์ ์ด๋ ๊ตฌ๋ฌธ์ด ์๋ ํ๋ฌธ์ ์ถ์ถํ๋ ๊ฒ์ด๋ค.
๊ตฌ๋์ , ๋๋ฌธ์์ ๊ฐ์ ์ธ์ด์ ๋ช๋ช ํน์ฑ๊ณผ "a"/"of"/"the"์ ๊ฐ์ ์ผ๋ฐ์ ์ธ ๋จ์ด๋ค์ด ๋ฌธ์์ ๊ตฌ์กฐ ์ ๊ณต์ ๋์์ ์ฃผ๊ธด ํ์ง๋ง ๋ง์ ์๋ฏธ๋ฅผ ์ฃผ์ง๋ ์๋๋ค. ๋ฐ๋ผ์ ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ์ฌ ์์ฐ์ด ์ฒ๋ฆฌ ํ์ดํ๋ผ์ธ์ ์ ๋ ฅํ๊ธฐ ์ ์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ง์ฐ๋ ๊ฒ์ด ์ข๋ค.
Stemming์ด๋?
Stemming์ ๋จ์ด์ ์ด๊ทผ์ด๋ ์ด๊ฐ ํ์์ ์ค์ด๋ ๊ณผ์ ์ด๋ค. ์ธ ๋จ์ด "branched", "branching", "branches" ์ธ ๋จ์ด๋ฅผ ๊ณ ๋ คํด๋ณด์. ๋ชจ๋ "branch"๋ผ๋ ๊ฐ์ ๋จ์ด๋ก ์ค์ฌ์ง ์ ์๋ค. ๊ฒฐ๊ตญ ์ธ ๊ฐ ๋ค branches๋ก๋ถํฐ ๋ถ๊ธฐ๋ ๊ฐ์ ์์ด๋์ด๋ฅผ ์ ๋ฌํ๋ค. ๋ค์ ๋งํด, (๋จ์ด์ ์ํ์ผ๋ก ์ค์ด๋ ๊ฒ์) ์ด ์ธ ๋จ์ด๊ฐ ์ ๋ฌํ๋ ์๋ฏธ์ ๋ณธ์ง์ ์ ์งํ๋ฉด์ ๋ณต์ก๋๋ฅผ ์ค์ด๋ ๋ฐ์ ๋์์ด ๋๋ค.
ํํธ, Stemming์ ๊ฒ์๊ณผ ์นํ์ด๋ผ๋ ๋งค์ฐ ๊ฐ๋จํ ์์ ์ ์ ์ฉํ ๋น ๋ฅด๊ณ crudeํ ์ฐ์ฐ์ด๋ค.
๋ค๋ฅธ ์์๋ก ์ ๋ฏธ์ฌ "ing"์ "ed"๋ ์ ๊ฑฐ๋ ์ ์์ผ๋ฉฐ "ies"๋ "y"๋ก ๋์ฒด๋ ์ ์๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ๋ฐ๋ฅด๋ฉด ์ฐ๋ฆฌ๋ ๋ถ์์ ํ ๋จ์ด๋ฅผ ์ป๊ฒ ๋์ง๋ง ์๊ด์๋ค. ์๋ํ๋ฉด corpus(๋ง๋ญ์น)์ ์๋ ๋จ์ด์ ๋ชจ๋ ํํ๋ค์ ๊ฐ์ ํํ๋ก ์ค์ฌ์ง๊ธฐ ๋๋ฌธ์ด๋ค.
words = ['first', 'time', 'see', 'second', 'renaissance', 'may',
'look', 'boring', 'look', 'least', 'twice', 'definitely', 'watch', 'part',
'2', 'change', 'view', 'matrix', 'human', 'people', 'ones', 'started', 'war', 'ai', 'bad', 'thing']
๋ํ NLTK๋ ์์ฐ์ด์ฒ๋ฆฌ Toolkit๋ PorterStemmer๋ SnowballStemmer์ฒ๋ผ ์ฐ๋ฆฌ๊ฐ ์ ํํ ์ ์๋ ๋ค๋ฅธ ์ข ๋ฅ์ stemmer๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์ฌ๊ธฐ์๋ ๊ฐ๋จํ stemming ๊ตฌํ์ ์ํด PorterStemmer๋ฅผ importํด๋ณด์.
from nltk.stem.porter import PorterStemmer
Stemmer๊ฐ ๋์ํ๋ ค๋ฉด corpus๋ก๋ถํฐ ํ ๋จ์ด์ฉ ์ ๋ฌํ๊ธฐ๋ง ํ๋ฉด ๋๋ค. ์ด ์์์์ stopwords๋ ์ด๋ฏธ ์ ๊ฑฐ๋์ด ์๋ค.
โญ stopwords ์ ๊ฑฐ ์ฝ๋(๋ฒ์ญ ์ค ์ถ๊ฐ)
nltk.download('stopwords')
# word ๋ฆฌ์คํธ๋ ๋ณธ๋ฌธ ๊ทธ๋๋ก ์ฌ์ฉ
filtered = []
for w in words:
if w not in stopword:
filtered.append(w)
words = filtered
stemmed_words = [PorterStemmer().stem(w) for w in words]
print(stemmed_words)
์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ถ๋ ฅ์ ์ป์ ์ ์๋ค.
์ถ๋ ฅ์ ๋ณด๋ฉด "started"๊ฐ "start"๋ก ์ถ์๋๊ณ , "people"์ ๋ง์ง๋ง "e"๊ฐ ์ฌ๋ผ์ง๊ณ "ones"๊ฐ "one"์ผ๋ก ์ค์ด๋๋ ๊ฝค ์ํธํ ๋ณํ์ด ์๋ ๊ฒ์ ์ ์ ์๋ค. ** people์์ e๊ฐ ์ฌ๋ผ์ง๋ ๊ฑด why ์ํธํ ๋ณํ ์์์ ๋ค์ด๊ฐ ์๋ ๊ฒ?
Lemmatization ์๊ฐ
Lemmatization์ ๋จ์ด๋ฅผ ์ ๊ทํ ํํ๋ก ์ค์ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋๋ค๋ฅธ ๊ธฐ์ ์ด๋ค. Lemmatization์์ transformation์ ๋จ์ด์ ๋ณํ์ ๊ทธ ๋จ์ด์ ์ํ์ผ๋ก ๋งคํํ๊ธฐ ์ํด ์ฌ์ ์ ์ฌ์ฉํ๋ค. ์ด ๋ฐฉ์์ ํตํด "is", "was", "were"์ ๊ฐ์ ์์ ๋ณํ์ ์ด๊ทผ "be"๋ก ๋๋๋ฆด ์ ์๋ค.
์๋ ์์ ๋ฅผ ์ํด WordNetLemmatizer๋ฅผ importํด๋ณด์.
from nltk.stem.wordnet import WordNetLemmatizer
lemmed_words = [WordNetLemmatizer().lemmatize(w) for w in words]
print(lemmed_words)
NLTK์ ๊ธฐ๋ณธ lemmatizer๋ ๋จ์ด๋ฅผ ๊ทธ๋ค์ ์ํ์ ๋งคํํ๊ธฐ ์ํด wordnet ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํ๋ค. lemmatization ๋จ๊ณ์ ์ถ๋ ฅ์ ๋ณด์.
"ones"๋ง "one"์ผ๋ก ๋ฐ๋๊ณ ๋ค๋ฅธ ๊ฒ๋ค์ ๋ฐ๋์ง ์์ ๊ฒ์ฒ๋ผ ๋ณด์ธ๋ค. ์
๋ ฅ์ ์์ธํ ์ดํด๋ณด๋ฉด, "ones"๋ง์ด ์ ์ผํ ๋ณต์๋ช
์ฌ์ธ ๊ฒ์ ํ์ธํ ์ ์๋ค. ์ด๊ฒ์ด "ones"๋ง ๋ฐ๋ ์ด์ ๋ค!
PoS๋ฅผ ์ด์ฉํ Lemmatization
Lemmatizer๋ ์ด๊ทผ ํํ๋ก ๋๋ฆฌ๋ ค๊ณ ํ๋ ๊ฐ ๋จ์ด์ ๋ํ ์ธ์ด์ ๋ถ๋ถ์ ์๊ณ ์๊ฑฐ๋ ์ถ์ธกํ ํ์๊ฐ ์๋ค. ์ด ๊ฒฝ์ฐ WordNetLemmatizer๋ ๋ช ์ฌ๊ฐ ๊ธฐ๋ณธ์ผ๋ก ์ค์ ๋์ด ์์ง๋ง ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค. "pos" ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ํ๋ฉด ๊ธฐ๋ณธ ์ค์ ๊ฐ์ ๋ฎ์ด์ธ ์ ์๋ค. ๋์ฌ๋ "v"๋ก ํ์.
lemmed = [WordNetLemmatizer().lemmatize(w, pos='v') for w in words]
print(lemmed)
์ด๋ฒ์๋ "boring"๊ณผ "started" ๋ ๋์ฌ๊ฐ ๋ฐ๋์๋ค.
๊ฒฐ๋ก
์์ ์์์์ ๋ดค๋ฏ์ด, stemming์ ๋ถ์์ ํ ํํ์ ์ด๊ฐ์ ๊ฒฐ๊ณผ๋ก ๋ด๊ธฐ๋ ํ๋ค. ํ์ง๋ง ๋ชจ๋ ๋จ์ด๋ค์ด ๋๊ฐ์ ํํ๋ก ์ค๊ธฐ ๋๋ฌธ์ ์ด ์ฌ์ค์ ๋ฌธ์ ๊ฐ ๋์ง ์๋๋ค.
Lemmatization์ ์ต์ข ํํ๋ ์๋ฏธ์๋ ๋จ์ด๋ผ๋ ์ ์ ๋นผ๋ฉด stemming๊ณผ ์ ์ฌํ๋ค. Stemming ๋ฐฉ์์ Lemmatization์ฒ๋ผ ์ฌ์ ์ด ํ์ํ์ง ์๋ค. ๋ฐ๋ผ์ ์ ์ฝ ์ฌํญ์ ๋ฐ๋ผ ๋ฉ๋ชจ๋ฆฌ ๋ถํ๊ฐ ๋ฎ์ ์ต์ ์ด ๋ ์ ์๋ค.
'Archive' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฒ์ญ] Micro, Macro & Weighted Averages of F1 Score, Clearly Explained (1) | 2022.12.20 |
---|---|
[๋ฒ์ญ] Foundations of NLP Explained Visually: Beam Search, How It Works (0) | 2022.08.01 |
[๋ฒ์ญ] Word2Vec Research Paper Explained (0) | 2022.07.04 |
[๋ฒ์ญ] Entropy, Cross-Entropy, KL-Divergence (0) | 2022.03.31 |
[๋ฒ์ญ] Attention: Sequence 2 Sequence model with Attention Mechanism (1) | 2022.03.16 |