[ํ ์คํธ ๋ถ์] 1. ํ ์คํธ ๋ถ์์ ์ดํด
ํ๋ก์ ํธ๋ฅผ ํ๋ฉด์ ํ ์คํธ ๋ถ์์ ์ ํด๋ณผ ๊ธฐํ๊ฐ ์์๋๋ฐ ์์ ํ ์ดํด๋ฅผ ํ๊ณ ์ฌ์ฉํ ๊ฒ์ ์๋๋ผ ์ค๋ฅ๋ ๋ง์ด ์ ํด๋ณด์๊ณ ๊ทธ๋์ ๋์ฑ ์ฐจ๊ทผ์ฐจ๊ทผ ํ ์คํธ ๋ถ์์ ๋ํด ๊ณต๋ถํด๋ณด๋ ค๊ณ ํฉ๋๋ค! ํ
xod22.tistory.com
์ ๋ฒ ํฌ์คํ ์์ ํ ์คํธ ๋ถ์์ ์ดํด์ ๋ํด ๊ฐ๋จํ ์ ์ด๋ณด์๋๋ฐ์!
์ค๋์ ํ ์คํธ ๋ถ์์ ํ๋ก์ธ์ค ์ค ์ฒซ๋ฒ์งธ์ธ ํ ์คํธ ์ ์ฒ๋ฆฌ ๋ฐ ์ ๊ทํ์ ๋ํด ๊ณต๋ถํด๋ณด๋ ค๊ณ ํฉ๋๋ค!

๋ฌธ์์์ ๋จ์ด ๊ธฐ๋ฐ์ผ๋ก ํ์ฌ ํผ์ฒ๋ฅผ ๋ฝ์ ํ์ ํผ์ฒ์ ๋ฒกํฐ ๊ฐ์ ๋ถ์ฌํ๋ ์์ ์ด ํ์ํ๋ค.
ํ์ง๋ง ํผ์ฒ์ ๋ฒกํฐ๊ฐ์ ๋ถ์ฌํ๋ ์์ ์ ํ๊ธฐ์ ! ๋ณธ ๋ฐ์ดํฐ์ ๋ํ ์ ์ฒ๋ฆฌ ์์ ์ด ํ์์ ์ด๋ค..!
ํ ์คํธ ๋ถ์์ ์ ์ฒ๋ฆฌ ์์ ์ ์ ๋ฆฌํด๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
1. Cleansing(ํด๋ ์ง)
2. Tokenization(ํ ํฐํ)
3. ํํฐ๋ง / stopwords(๋ถ์ฉ์ด) ์ ๊ฑฐ / ์ฒ ์ ์์
4. Stemming & Lemmatization
1. Cleansing(ํด๋ ์ง)
: ํ ์คํธ ๋ถ์์์ ์คํ๋ ค ๋ฐฉํด๊ฐ ๋๋ ๋ถํ์ํ ๋ฌธ์ ๋ฑ์ ์ ๊ฑฐํ๋ ์์ ์ ๋งํ๋ค!
์๋ฅผ๋ค์ด ์ธํฐ๋ท์์ ํฌ๋กค๋งํ ๋ฐ์ดํฐ๊ฐ ์๋ค๊ณ ํ์ ๋, html ๊ธฐํธ ๋ฑ์ ์ฌ์ ์ ์ญ์ ํ๋ ์์ ์ด ์ด์ ์ํจ..!
2. Tokenization(ํ ํฐํ)
: ๋ ๊ฒ์ ํ ์คํธ๋ฅผ ๋ฌธ์ฅ๋ณ, ๋จ์ด๋ณ๋ก ๋๋๊ธฐ..!
ํ ํฐํ์ ์ ํ์ ํฌ๊ฒ ๋๊ฐ์ง๊ฐ ์๋ค.
1) ๋ฌธ์ฅ ํ ํฐํ : ๋ฌธ์์์ ๋ฌธ์ฅ์ ๋ถ๋ฆฌ
2) ๋จ์ด ํ ํฐํ : ๋ฌธ์ฅ์์ ๋จ์ด๋ฅผ ํ ํฐ์ผ๋ก ๋ถ๋ฆฌ
์ฆ ๋ฌธ์->๋ฌธ์ฅ, ๋ฌธ์ฅ->๋จ์ด
~ํจํค์ง ์ํฌํธ~
from nltk import sent_tokenize
import nltk
nltk.download('punkt')
~๋ฌธ์ฅ ํ ํฐํ~
: ๋ฌธ์ฅ ํ ํฐํ๋ ์ฃผ๋ก ๋ฌธ์ฅ์ ๋ง์ง๋ง์ ๋ปํ๋ ๊ธฐํธ๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์ด ์งํ๋๋ค..!
๋ฌธ์ฅ์ ๋ง์ง๋ง์ ์์งํ๋ ๊ธฐํธ๋ก๋ ๋ํ์ ์ผ๋ก ๋ง์นจํ(.)์ ๊ฐํ๋ฌธ์(\n) ๋ฑ์ด ์๋ค.
๋ํ ์ ๊ทํ ํํ์์ ๋ฐ๋ฅธ ๋ฌธ์ฅ ํ ํฐํ๋ ๊ฐ๋ฅํ๋ค.
text_sample = '''The Matrix is everywhere its all around us,
here even in this room. you can see it out your window or on your television.
you feel it when you go to work, or go to church or pay your taxes.'''
# sent_tokenize(text="์
๋ ฅํ ํ
์คํธ")
sentences = sent_tokenize(text=text_sample)
print("๊ฒฐ๊ณผ :", sentences)
# text๊ฐ ๋ช๊ฐ์ ๋ฌธ์ฅ์ผ๋ก ๋์ด์๋์ง ๊ฐ์๋ฅผ ์ธ์ค
print("๋ฌธ์ฅ ๊ฐ์ :", len(sentences))

print๋ก ํ ์คํธ๋ฅผ ํ์ธํด๋ณด๋ฉด, ๋ฌธ์ฅ๋ณ๋ก ๋ถ๋ฆฌ๊ฐ ๋์ด ๋ฆฌ์คํธ์ ๋ด๊ฒจ์๋ ๊ฒ์ ์ ์ ์๋ค!
~๋จ์ด ํ ํฐํ~
: ๋จ์ด ํ ํฐํ๋ ๋ฌธ์ฅ์ ๋จ์ด๋ก ํ ํฐํ ํจ์ ์๋ฏธํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก๋ ๊ณต๋ฐฑ, ์ฝค๋ง(,), ๋ง์นจํ(.), ๊ฐํ๋ฌธ์ ๋ฑ์ ๊ธฐ์ค์ผ๋ก ํ ํฐํ ์ํจ๋ค.
from nltk import word_tokenize
sentence = "The Matrix is everywhere its all around us, here even in this room"
words = word_tokenize(sentence)
print(type(words))
print("๊ฒฐ๊ณผ :", words)

๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ฐ ๋จ์ด๋ค์ด ๋ฆฌ์คํธ์ ๋ด๊ฒจ ๋ํ๋๊ณ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
~๋ฌธ์ฅ ํ ํฐํ์ ๋จ์ด ํ ํฐํ์ ๊ฒฐํฉ~
from nltk import word_tokenize, sent_tokenize
#ํจ์ ์์ฑ
def tokenize_text(text):
#๋ฌธ์ฅ๋ณ๋ก ๋ถ๋ฆฌ
sentences = sent_tokenize(text)
#๋ถ๋ฆฌ๋ ๋ฌธ์ฅ๋ณ ๋จ์ด ํ ํฐํ
word_tokens = [word_tokenize(sentence) for sentence in sentences]
return word_tokens
#ํจ์์ text_sample์ ๋ฃ์ด์ค
word_tokens = tokenize_text(text_sample)
#๋ฐํ๊ฐ word_tokens ์ถ๋ ฅ
print(type(word_tokens), len(word_tokens))
print(word_tokens)

3๊ฐ์ ๋ฌธ์ฅ๋ค์ด ๊ฐ๊ฐ ๋จ์ด ํ ํฐํ๋์ด ํ๋์ ๋ฆฌ์คํธ์ ๋ด๊ฒจ์์์ ํ์ธํ ์ ์๋ค!
-> ์ธ๊ฐ์ ๋ฌธ์ฅ์ด๋ฏ๋ก ์ธ๊ฐ์ ๋ฆฌ์คํธ..!
'๐ ๋จธ์ ๋ฌ๋ | ๋ฅ๋ฌ๋ > ํ ์คํธ ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ ์คํธ ๋ถ์] 4-1. ์ง๋ํ์ต ๊ธฐ๋ฐ ๊ฐ์ฑ ๋ถ์ - IMDB์ํํ (0) | 2022.02.26 |
---|---|
[ํ ์คํธ ๋ถ์] 3. Bag of Words (BOW) (0) | 2022.02.24 |
[ํ ์คํธ ๋ถ์] 2-3. ํ ์คํธ ์ ์ฒ๋ฆฌ - Stemming๊ณผ Lemmatization (0) | 2022.02.23 |
[ํ ์คํธ ๋ถ์] 2-2. ํ ์คํธ ์ ์ฒ๋ฆฌ - ๋ถ์ฉ์ด ์ ๊ฑฐ (0) | 2022.02.20 |
[ํ ์คํธ ๋ถ์] 1. ํ ์คํธ ๋ถ์์ ์ดํด (0) | 2022.02.19 |
[ํ ์คํธ ๋ถ์] 1. ํ ์คํธ ๋ถ์์ ์ดํด
ํ๋ก์ ํธ๋ฅผ ํ๋ฉด์ ํ ์คํธ ๋ถ์์ ์ ํด๋ณผ ๊ธฐํ๊ฐ ์์๋๋ฐ ์์ ํ ์ดํด๋ฅผ ํ๊ณ ์ฌ์ฉํ ๊ฒ์ ์๋๋ผ ์ค๋ฅ๋ ๋ง์ด ์ ํด๋ณด์๊ณ ๊ทธ๋์ ๋์ฑ ์ฐจ๊ทผ์ฐจ๊ทผ ํ ์คํธ ๋ถ์์ ๋ํด ๊ณต๋ถํด๋ณด๋ ค๊ณ ํฉ๋๋ค! ํ
xod22.tistory.com
์ ๋ฒ ํฌ์คํ ์์ ํ ์คํธ ๋ถ์์ ์ดํด์ ๋ํด ๊ฐ๋จํ ์ ์ด๋ณด์๋๋ฐ์!
์ค๋์ ํ ์คํธ ๋ถ์์ ํ๋ก์ธ์ค ์ค ์ฒซ๋ฒ์งธ์ธ ํ ์คํธ ์ ์ฒ๋ฆฌ ๋ฐ ์ ๊ทํ์ ๋ํด ๊ณต๋ถํด๋ณด๋ ค๊ณ ํฉ๋๋ค!

๋ฌธ์์์ ๋จ์ด ๊ธฐ๋ฐ์ผ๋ก ํ์ฌ ํผ์ฒ๋ฅผ ๋ฝ์ ํ์ ํผ์ฒ์ ๋ฒกํฐ ๊ฐ์ ๋ถ์ฌํ๋ ์์ ์ด ํ์ํ๋ค.
ํ์ง๋ง ํผ์ฒ์ ๋ฒกํฐ๊ฐ์ ๋ถ์ฌํ๋ ์์ ์ ํ๊ธฐ์ ! ๋ณธ ๋ฐ์ดํฐ์ ๋ํ ์ ์ฒ๋ฆฌ ์์ ์ด ํ์์ ์ด๋ค..!
ํ ์คํธ ๋ถ์์ ์ ์ฒ๋ฆฌ ์์ ์ ์ ๋ฆฌํด๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
1. Cleansing(ํด๋ ์ง)
2. Tokenization(ํ ํฐํ)
3. ํํฐ๋ง / stopwords(๋ถ์ฉ์ด) ์ ๊ฑฐ / ์ฒ ์ ์์
4. Stemming & Lemmatization
1. Cleansing(ํด๋ ์ง)
: ํ ์คํธ ๋ถ์์์ ์คํ๋ ค ๋ฐฉํด๊ฐ ๋๋ ๋ถํ์ํ ๋ฌธ์ ๋ฑ์ ์ ๊ฑฐํ๋ ์์ ์ ๋งํ๋ค!
์๋ฅผ๋ค์ด ์ธํฐ๋ท์์ ํฌ๋กค๋งํ ๋ฐ์ดํฐ๊ฐ ์๋ค๊ณ ํ์ ๋, html ๊ธฐํธ ๋ฑ์ ์ฌ์ ์ ์ญ์ ํ๋ ์์ ์ด ์ด์ ์ํจ..!
2. Tokenization(ํ ํฐํ)
: ๋ ๊ฒ์ ํ ์คํธ๋ฅผ ๋ฌธ์ฅ๋ณ, ๋จ์ด๋ณ๋ก ๋๋๊ธฐ..!
ํ ํฐํ์ ์ ํ์ ํฌ๊ฒ ๋๊ฐ์ง๊ฐ ์๋ค.
1) ๋ฌธ์ฅ ํ ํฐํ : ๋ฌธ์์์ ๋ฌธ์ฅ์ ๋ถ๋ฆฌ
2) ๋จ์ด ํ ํฐํ : ๋ฌธ์ฅ์์ ๋จ์ด๋ฅผ ํ ํฐ์ผ๋ก ๋ถ๋ฆฌ
์ฆ ๋ฌธ์->๋ฌธ์ฅ, ๋ฌธ์ฅ->๋จ์ด
~ํจํค์ง ์ํฌํธ~
from nltk import sent_tokenize
import nltk
nltk.download('punkt')
~๋ฌธ์ฅ ํ ํฐํ~
: ๋ฌธ์ฅ ํ ํฐํ๋ ์ฃผ๋ก ๋ฌธ์ฅ์ ๋ง์ง๋ง์ ๋ปํ๋ ๊ธฐํธ๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์ด ์งํ๋๋ค..!
๋ฌธ์ฅ์ ๋ง์ง๋ง์ ์์งํ๋ ๊ธฐํธ๋ก๋ ๋ํ์ ์ผ๋ก ๋ง์นจํ(.)์ ๊ฐํ๋ฌธ์(\n) ๋ฑ์ด ์๋ค.
๋ํ ์ ๊ทํ ํํ์์ ๋ฐ๋ฅธ ๋ฌธ์ฅ ํ ํฐํ๋ ๊ฐ๋ฅํ๋ค.
text_sample = '''The Matrix is everywhere its all around us,
here even in this room. you can see it out your window or on your television.
you feel it when you go to work, or go to church or pay your taxes.'''
# sent_tokenize(text="์
๋ ฅํ ํ
์คํธ")
sentences = sent_tokenize(text=text_sample)
print("๊ฒฐ๊ณผ :", sentences)
# text๊ฐ ๋ช๊ฐ์ ๋ฌธ์ฅ์ผ๋ก ๋์ด์๋์ง ๊ฐ์๋ฅผ ์ธ์ค
print("๋ฌธ์ฅ ๊ฐ์ :", len(sentences))

print๋ก ํ ์คํธ๋ฅผ ํ์ธํด๋ณด๋ฉด, ๋ฌธ์ฅ๋ณ๋ก ๋ถ๋ฆฌ๊ฐ ๋์ด ๋ฆฌ์คํธ์ ๋ด๊ฒจ์๋ ๊ฒ์ ์ ์ ์๋ค!
~๋จ์ด ํ ํฐํ~
: ๋จ์ด ํ ํฐํ๋ ๋ฌธ์ฅ์ ๋จ์ด๋ก ํ ํฐํ ํจ์ ์๋ฏธํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก๋ ๊ณต๋ฐฑ, ์ฝค๋ง(,), ๋ง์นจํ(.), ๊ฐํ๋ฌธ์ ๋ฑ์ ๊ธฐ์ค์ผ๋ก ํ ํฐํ ์ํจ๋ค.
from nltk import word_tokenize
sentence = "The Matrix is everywhere its all around us, here even in this room"
words = word_tokenize(sentence)
print(type(words))
print("๊ฒฐ๊ณผ :", words)

๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ฐ ๋จ์ด๋ค์ด ๋ฆฌ์คํธ์ ๋ด๊ฒจ ๋ํ๋๊ณ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
~๋ฌธ์ฅ ํ ํฐํ์ ๋จ์ด ํ ํฐํ์ ๊ฒฐํฉ~
from nltk import word_tokenize, sent_tokenize
#ํจ์ ์์ฑ
def tokenize_text(text):
#๋ฌธ์ฅ๋ณ๋ก ๋ถ๋ฆฌ
sentences = sent_tokenize(text)
#๋ถ๋ฆฌ๋ ๋ฌธ์ฅ๋ณ ๋จ์ด ํ ํฐํ
word_tokens = [word_tokenize(sentence) for sentence in sentences]
return word_tokens
#ํจ์์ text_sample์ ๋ฃ์ด์ค
word_tokens = tokenize_text(text_sample)
#๋ฐํ๊ฐ word_tokens ์ถ๋ ฅ
print(type(word_tokens), len(word_tokens))
print(word_tokens)

3๊ฐ์ ๋ฌธ์ฅ๋ค์ด ๊ฐ๊ฐ ๋จ์ด ํ ํฐํ๋์ด ํ๋์ ๋ฆฌ์คํธ์ ๋ด๊ฒจ์์์ ํ์ธํ ์ ์๋ค!
-> ์ธ๊ฐ์ ๋ฌธ์ฅ์ด๋ฏ๋ก ์ธ๊ฐ์ ๋ฆฌ์คํธ..!
'๐ ๋จธ์ ๋ฌ๋ | ๋ฅ๋ฌ๋ > ํ ์คํธ ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ ์คํธ ๋ถ์] 4-1. ์ง๋ํ์ต ๊ธฐ๋ฐ ๊ฐ์ฑ ๋ถ์ - IMDB์ํํ (0) | 2022.02.26 |
---|---|
[ํ ์คํธ ๋ถ์] 3. Bag of Words (BOW) (0) | 2022.02.24 |
[ํ ์คํธ ๋ถ์] 2-3. ํ ์คํธ ์ ์ฒ๋ฆฌ - Stemming๊ณผ Lemmatization (0) | 2022.02.23 |
[ํ ์คํธ ๋ถ์] 2-2. ํ ์คํธ ์ ์ฒ๋ฆฌ - ๋ถ์ฉ์ด ์ ๊ฑฐ (0) | 2022.02.20 |
[ํ ์คํธ ๋ถ์] 1. ํ ์คํธ ๋ถ์์ ์ดํด (0) | 2022.02.19 |