NLTK (Natural Language Toolkit): 자연어 처리 및 문서 분석용 패키지
http://www.nltk.org/nltk_data/ (전체목록)
주요 코퍼스
import nltk
nltk.download("gutenberg")
nltk.download("reuters")
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')
import nltk
fileids = nltk.corpus.gutenberg.fileids()
print(fileids)
from nltk.corpus import gutenberg
for fileid in gutenberg.fileids():
num_chars = len(gutenberg.raw(fileid)) # 문자수
num_words = len(gutenberg.words(fileid)) # 단어수
num_sents = len(gutenberg.sents(fileid)) # 문장수
num_vocab = len(set(w.lower() for w in gutenberg.words(fileid))) # 어휘수
print(round(num_chars/num_words), round(num_words/num_sents), round(num_words/num_vocab), fileid)
moby_dick_words = nltk.corpus.gutenberg.words('melville-moby_dick.txt')
len(moby_dick_words)
import nltk
from nltk.corpus import gutenberg
# 1. read document
moby_dick_text = gutenberg.open('melville-moby_dick.txt').read()
moby_dick_text[:1000]
[Moby Dick by Herman Melville 1851]
ETYMOLOGY.
(Supplied by a Late Consumptive Usher to a Grammar School)
The pale Usher–threadbare in coat, heart, body, and brain; I see him now. He was ever dusting his old lexicons and grammars, with a queer handkerchief, mockingly embellished with all the gay flags of all the known nations of the world. He loved to dust his old grammars; it somehow mildly reminded him of his mortality.
“While you take in hand to school others, and to teach them by what name a whale-fish is to be called in our tongue leaving out, through ignorance, the letter H, which almost alone maketh the signification of the word, you deliver that which is not true.” –HACKLUYT
“WHALE. … Sw. and Dan. HVAL. This animal is named from roundness or rolling; for in Dan. HVALT is arched or vaulted.” –WEBSTER’S DICTIONARY
“WHALE. … It is more immediately from the Dut. and Ger. WALLEN; A.S. WALW-IAN, to roll, to wallow.” –RICHARDSON’S DICTIONARY
텍스트를 처리하기 위한 작은 단위(token)으로 나눈다. NLTK는 다음 토큰 생성을 제공
from nltk.tokenize import sent_tokenize # 문장으로 나누기
from nltk.tokenize import word_tokenize # 단어로 나누기
from nltk.tokenize import regexp_tokenize # 정규식으로 나누기
from nltk.tokenize import sent_tokenize
moby_sents = sent_tokenize(moby_dick_text)
moby_sents[100:110]
from nltk.tokenize import word_tokenize
moby_words = word_tokenize(moby_dick_text)
moby_words[100:110]
from nltk import regexp_tokenize
moby_retoks = regexp_tokenize(moby_dick_text.lower(), r"[\w]+")
moby_retoks[100:110]
http://www.nltk.org/api/nltk.html#nltk.text.Text
import nltk
moby = nltk.Text(moby_retoks)
print(len(moby.tokens)) # 토큰 개수
print(len(set(moby.tokens))) # 고유한 토큰 개수
freq_dist = moby.vocab() # Freq Dist (빈도 분포)
print(len(freq_dist))
print(freq_dist.most_common(50)) # 가장 많이 등장하는 50개
moby.plot(50)
moby.count('captain')
단어가 등장하는 위치를 Word Offset에 따라 배치하여 시각화
모비딕의 주요 등장인물들: ‘ahab’, ‘starbuck’, ‘queequeg’, ‘ishmael’, ‘stubb’, ‘flask’, ‘fedallah’
moby.dispersion_plot(['ahab', 'starbuck', 'queequeg', 'ishmael', 'stubb', 'flask', 'fedallah'])
Concordance (색인): 단어가 쓰인 문맥을 모두 찾아준다
moby.concordance('ahab', lines=10)
주어진 단어와 비슷한 환경(same context)에서 쓰인(≠비슷한 뜻) 단어들
moby.similar('whale')
moby.similar('ship')
moby.similar('god')
for two words used in similar contexts
moby.common_contexts(['monstrous', 'true'])
연어(連語, collocation): 함께 위치하는 단어들 (co + location), 어휘의 조합 또는 짝을 이루는 단어
moby.collocations()
NLTK 에서는 Penn Treebank Tagset 을 이용
https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html
예를 들어,
nltk.pos_tag(['The', 'earth', 'is', 'a', 'beautiful', 'planet'])
# 허먼 멜빌, "모비딕" 첫 단락
# 나를 이스마엘로 불러라. 몇 년 전, 정확히 얼마나 오래 동안 그랬는지는 모르겠지만,
# 내 지갑에 돈 한 푼 없는 빈털터리 신세에 육상에선 특별히 흥미를 끄는 어떤 것도 없던 그런 시절,
# 나는 잠시 동안이나마 배를 타고 세계의 바다를 보고 싶다는 생각을 했다.
# 그것이 우울하고 의기소침한 감정, 그 반복되는 악순환의 고리를 끊어버리는 한 방법이었다.
moby_dick_first_para_text = '''
Call me Ishmael. Some years ago -- never mind how long
precisely--having little or no money in my purse, and nothing
particular to interest me on shore, I thought I would sail about a
little and see the watery part of the world.
It is a way I have of driving off the spleen, and regulating the circulation.
'''
import nltk
from nltk.tokenize import word_tokenize
moby_words = word_tokenize(moby_dick_first_para_text)
tasg = nltk.pos_tag(moby_words)
tasg[:20]
text = 'The earth is a beautiful planet.'
tokens = text.split()
tags = nltk.pos_tag(tokens)
parser = nltk.RegexpParser("NP: {<DT>?<JJ>?<NN.*>*}")
tree = parser.parse(tags)
tree
NLTK 지원
Stemmer vs Lemmatizer
p_stmr = nltk.PorterStemmer()
p_stmr.stem("wonderful"), p_stmr.stem("flying"), p_stmr.stem("flies")
l_stmr = nltk.LancasterStemmer()
l_stmr.stem("wonderful"), l_stmr.stem("flying"), l_stmr.stem("flies")
lmtz = nltk.WordNetLemmatizer()
lmtz.lemmatize("wonderful"), lmtz.lemmatize("flying"), lmtz.lemmatize("flies")
# POS: # n=명사, v=동사, a=형용사, r=부사
print(lmtz.lemmatize("cats"))
print(lmtz.lemmatize("better", pos="a"))
print(lmtz.lemmatize("is", pos="v"))
print(lmtz.lemmatize("led"))
print(lmtz.lemmatize("led",'v'))
윈도우 운영체제의 노트북에서는 iPhone 유선 테더링이 잘 안되는 경우가 많습니다. 보통 iPhone의 드라이버가 설치가 안되있어서인…
안녕하세요, 혹시 이런 생각해 본 적 없으신가요? "내가 투자한 회사는 누가 감시하고, 어떻게 운영될까?" 오늘은…
1. Gemini CLI란 무엇인가요? Gemini CLI는 터미널 환경에서 직접 Gemini 모델과 상호작용할 수 있도록 만들어진…
과적합은 머신러닝에서 학습용데이터를 과하게 학습하여, 실제데이터를 예측하지 못하는 현상을 말합니다. 인공지능(AI)의 학습 방법은 우리가 시험공부를…