ย
n-๊ทธ๋จ(n-gram)์ ๊ณ ์ ๊ธธ์ด(n)์ ์ฐ์๋ ํ ํฐ ์ํ์ค์
๋๋ค. ์ ๋๊ทธ๋จ(unigram)์ ํ ํฐ ํ ๊ฐ, ๋ฐ์ด๊ทธ๋จ(bigram)์ ํ ํฐ ๋ ๊ฐ๋ก ์ด๋ฃจ์ด์ง๋๋ค. ํ ํฐ n๊ฐ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค๋ฉด n-๊ทธ๋จ์ด ๋ฉ๋๋ค. ์์ ํ์ฉํ๋ spaCy์ NLTK ๊ฐ์ ํจํค์ง๋ฅผ ํ์ฉํ๋ฉด n-๊ทธ๋จ์ ํธ๋ฆฌํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
n-๊ทธ๋จ์ ์ฌ์ฉํ๋ ์ด์ ๋ ํ๋์ ํ ํฐ์ผ๋ก ์ ํํ ์๋ฏธ๋ฅผ ํ์
ํ๊ธฐ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ์๋ค ๋ฌธ๋งฅ์ ๋ฐ๋ผ ๊ฐ์ ๋จ์ด๋ ๋ค๋ฅธ ์๋ฏธ๋ฅผ ๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฏ๋ก, ๋ง๋ญ์น์์ n๊ฐ์ ๋จ์ด ๋ญ์น๋ก ๋์ด ํ๋์ ํ ํฐ์ผ๋ก ๊ฐ์ฃผํ๋ค๋ฉด ์ข ๋ ํจ์จ์ ์ผ๋ก ๋ฌธ์ฅ์ ์๋ฏธ๋ฅผ ๋ถ์ํ ์ ์๊ฒ ๋ฉ๋๋ค. ๋ค๋ง, n์ ๋๋ฌด ํฌ๊ฒ ์ก์ ๊ฒฝ์ฐ ํฌ์์ฑ, ๋ชจ๋ธ ์ฌ์ด์ฆ๊ฐ ์ปค์ง๋ ๋ฑ์ ๋ฌธ์ ๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ต๋ 5๋ฅผ ๋๊ฒ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ผ๋ก ๊ถ์ฅ๋๊ณ ์์ต๋๋ค.
ย
๋ค์์ ๊ฐ๋จํ unigram, bigram, n-gram์ ๊ตฌํํ ์ฝ๋์
๋๋ค. ์ดํด๋ฅผ ๋๊ธฐ ์ํด ๊ฐ๋จํ ํ
์คํธ๋ก ์์๋ฅผ ๋ ๊ฒ์ด๋ฏ๋ก, ๋ณต์กํ ๋ง๋ญ์น๋ฅผ ํ ํฐํํ ๊ฒฝ์ฐ ์ฌ๋ฌ ํจํค์ง์์ ์ ๊ณตํ๋ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์
๋๋ค.
import re def n_gram(text, n): #uni_gram text_low = text.lower() text_low = re.sub('(\W+)', ' ', text_low) words = text_low.split(' ') if '' in words: words.remove('') #bi_gram if n == 2: bi_words = [] for i in range(len(words) - 1): bi_words.append(' '.join(words[i:i+2])) return bi_words #n_gram n_words = [] for i in range(len(words) - n + 1): n_words.append(' '.join(words[i:i+n])) return n_words #์์ text = "Yes, I can do it!" >>> n_gram(text, 1) #uni_gram ['yes', 'i', 'can', 'do', 'it'] >>> n_gram(text, 2) #bi_gram bigram = ['yes i', 'i can', 'can do', 'do it'] >>> n_gram(text, 3) #tri_gram trigram = ['yes i can', 'i can do', 'can do it']
ย
์ด์ ๊ธ ์ฝ๊ธฐ
๋ค์ ๊ธ ์ฝ๊ธฐ