Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

AI study/์ž์—ฐ์–ด ์ฒ˜๋ฆฌ (NLP)

[NLP] ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ธฐ์ดˆ / ๋ง๋ญ‰์น˜, ํ† ํฐํ™”, n-gram

๊ฐ์ž ๐Ÿฅ” 2021. 7. 19. 08:01
๋ฐ˜์‘ํ˜•

- ๋ณธ ํฌ์ŠคํŒ…์€ ํŒŒ์ดํ† ์น˜๋กœ ๋ฐฐ์šฐ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ (ํ•œ๋น›๋ฏธ๋””์–ด) ์ฑ…์„ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑ๋œ ๊ธ€์ž…๋‹ˆ๋‹ค.

1. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ

  • ๋ง๋ญ‰์น˜ (corpus, ์ฝ”ํผ์Šค) = ์ผ๋ฐ˜์ ์ธ ์›์‹œํ…์ŠคํŠธ์™€ ์ด ํ…์ŠคํŠธ์— ์—ฐ๊ด€๋ค ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•œ ๊ฒƒ
  • ์›์‹œํ…์ŠคํŠธ = ๋ฌธ์ž(๋ฐ”์ดํŠธ)์‹œํ€€์Šค ์ด์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์ž๋ฅผ ํ† ํฐ์ด๋ผ๋Š” ์—ฐ์†๋œ ๋‹จ์œ„๋กœ ๋ฌถ์—ฌ ์žˆ์Œ
  • ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ํ…์ŠคํŠธ = ์ƒ˜ํ”Œ(Sample) or ๋ฐ์ดํ„ฐํฌ์ธํŠธ ๋ผ๊ณ  ๋ถ€๋ฆ„
  • ๋ง๋ญ‰์น˜ = ์ƒ˜ํ”Œ๋“ค์˜ ๋ชจ์Œ = ๋ฐ์ดํ„ฐ์…‹

2. ํ† ํฐํ™” 

  • ์ฃผ์–ด์ง„ ๋ง๋ญ‰์น˜(์ฝ”ํผ์Šค)๋ฅผ ํ† ํฐ์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์„ ํ† ํฐํ™” ๋ผ๊ณ  ํ•จ
"Maria frapis la verda sorcistino"

ํ•ด๋‹น ํ„ฐํ‚ค์–ด ๋ฌธ์ž์—๋Š” 6๊ฐœ์˜ ํ† ํฐ์ด ์กด์žฌ (๊ณต๋ฐฑ๋ฌธ์ž์™€ ๊ตฌ๋‘์ ) ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ„ฐํ‚ค์–ด๋Š” ํ•œ๊ตญ์–ด์™€ ๊ฐ™์ด '๊ต์ฐฉ์–ด'๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. ๊ต์ฐฉ์–ด๋ž€, ์‹ค์งˆ์ ์ธ ๋‹จ์–ด ๋˜๋Š” ์–ด๊ฐ„์— ๋ฌธ๋ฒ•์ ์ธ ์š”์†Œ๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ๊ฒฐํ•ฉํ•จ์œผ๋กœ์จ ๋ฌธ์žฅ ์†์—์„œ ๋ฌธ๋ฒ•์ ์ธ ์—ญํ• ์ด๋‚˜ ๊ด€๊ณ„์˜ ์ฐจ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์–ธ์–ด์ด๋‹ค. ์ด๋Ÿด๊ฒฝ์šฐ, 'ํ† ํฐ'์„ ๊ธฐ์ค€์œผ๋กœ๋งŒ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค.

2.1 Spacy ์™€ NLTK ๋กœ ํ† ํฐํ™” ์ง„ํ–‰ํ•ด๋ณด๊ธฐ

import spacy
nlp = spacy.load('en')
text = "Mary, don't slap the green witch"
print([str(token) for token in nlp(text.lower())])

from nltk.tokenize import TweetTokenizer
tweet = u"Snow White and the Seven Degrees #MakeAMovieCold@midnight:-)"
tokenizer = TweetTokenizer()
print(tokenizer.tokenize(tweet.lower()))

ํ•ด๋‹น ์ฝ”๋“œ์—์„œ๋Š” ํ•ด์‹œํ…Œ๊ทธ(#), ๋ฉ˜์…˜(@), ๊ทธ๋ฆฌ๊ณ  :-) ์ด๋ชจํ‹ฐ์ฝ˜์„ ์™„๋ฒฝํ•˜๊ฒŒ ํ† ํฐํ™” ํ–ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” nltk์—์„œ ์ œ๊ณตํ•˜๋Š” tweettokenizer ๊ธฐ๋Šฅ ๋–„๋ฌธ์ด๋‹ค. tweettokenizer๋Š” ์ด๋ชจํ‹ฐ์ฝ˜์„ ์ธ์‹ํ•˜๋Š” ํ† ํฐํ™”๋ฅผ ์ง€์›ํ•œ๋‹ค.

import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize  
print(word_tokenize("Snow White and the Seven Degrees #MakeAMovieCold@midnight:-)"))

๋งŒ์•ฝ ๊ทธ๋ƒฅ ๋‹จ์–ด ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ํ† ํฌ๋‚˜์ด์ €์ธ word_tonkenize๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด :-) ์™€ ๋ฉ˜์…˜, ํ…Œํฌ์™€ ๊ฐ™์€ ๊ฒƒ์„ ๋ชจ๋‘ ๋ถ„ํ•ดํ•ด์„œ ํ† ํฐํ™” ํ•˜๊ฒŒ ๋œ๋‹ค. 

๋”ฐ๋ผ์„œ ์–ด๋–ค ํ† ํฌํ™” ๋ฐฉ์‹์„ ์›ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ถ”ํ›„ ์ •ํ™•๋„์— ์˜ํ–ฅ์„ ๋ผ์น  ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ๋ธ”๋กœ๊ทธ์— nltk์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ tokenizer์— ๋Œ€ํ•œ ์„ค๋ช…์ด ์ž์„ธํ•˜๊ฒŒ ๋‚˜์™€์žˆ๋‹ค.

https://jaaamj.tistory.com/77

 

NLTK ํŒจํ‚ค์ง€ ํ™œ์šฉํ•œ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ (1) ํ† ํฐํ™”

NLTK๋Š” Natural Language ToolKit์˜ ์•ฝ์ž๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ฐ ๋ถ„์„์„ ์œ„ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. NLTK๋Š” ํ† ํฐ์ƒ์„ฑํ•˜๊ธฐ, ํ˜•ํƒœ์†Œ ๋ถ„์„, ํ’ˆ์‚ฌ ํƒœ๊น…ํ•˜๊ธฐ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์žฅ ํ† ํฐํ™” (Sentenc

jaaamj.tistory.com

3. ํƒ€์ž…

  • ํƒ€์ž… = ๋ง๋ญ‰์น˜์— ๋“ฑ์žฅํ•˜๋Š” ๊ณ ์œ ํ•œ ํ† ํฐ
  • ๋ง๋ญ‰์น˜์— ์žˆ๋Š” ๋ชจ๋“  ํƒ€์ž…์˜ ์ง‘ํ•จ = ์–ดํœ˜์‚ฌ์ „ or ์–ดํœ˜
  • ๋‹จ์–ด = ๋‚ด์šฉ์–ด / ๋ถˆ์šฉ์–ด๋กœ ๊ตฌ๋ถ„
    • ๋ถˆ์šฉ์–ด = ๊ด€์‚ฌ์™€ ์ „์น˜์‚ฌ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„ ๋‚ด์šฉ์–ด๋ฅผ ๋ณด์ถฉํ•˜๋Š” ๋ฌธ๋ฒ•์ ์ธ ์šฉ๋„๋กœ ์‚ฌ์šฉ

4. n-gram 

  • n-gram = ํ…์ŠคํŠธ์— ์žˆ๋Š” ๊ณ ์ •๊ธธ์ด(n)์˜ ์—ฐ์†๋œ ํ† ํฐ ์‹œํ€€์Šค
  • ๋ฐ”์ด๊ทธ๋žจ = ํ† ํฐ ๋‘ ๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง
  • ์œ ๋‹ˆ๊ทธ๋žจ = ํ† ํฐ ํ•œ ๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง

1. n-gram์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ ์ •์˜ํ•ด์„œ ์‚ดํŽด๋ณด๊ธฐ

def n_grams(text, n):
  return[text[i:i+n] for i in range(len(text)-n+1)]

cleaned = ['mary', ',', "n't", 'slap', 'green', 'witch', '.' ]
print(n_grams(cleaned, 3))

ํ† ํฐํ™”๋œ cleaned ๋ฅผ ํŠธ๋ผ์ด๊ทธ๋žจ(3-gram)์œผ๋กœ ํ‘œํ˜„ํ•˜๋‹ˆ ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋˜์—ˆ๋‹ค. ๋ฌธ์ž์˜ 3๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ฌถ์–ด ํ† ํฐ ํ•˜๋‚˜๋กœ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

n-gram์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•๊ณผ  n-gram๋ชจ๋ธ์„ ํ™œ์šฉํ•  ๊ฒฝ์šฐ์˜ ํ•œ๊ณ„์ ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์—์„œ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋‹ค.

https://wikidocs.net/21692

 

์œ„ํ‚ค๋…์Šค

์˜จ๋ผ์ธ ์ฑ…์„ ์ œ์ž‘ ๊ณต์œ ํ•˜๋Š” ํ”Œ๋žซํผ ์„œ๋น„์Šค

wikidocs.net

 

๋ฐ˜์‘ํ˜•