msg = "I like to pet the cat's soft fur."
sentence = "Don't waste your youth. you're not always young"
kor_sentence = '아름다운 도깨비 불, 외로운 도깨비는, 눈물많은 도깨비일뿐'
tokens1 = [x for x in msg.split(' ')]
tokens2= [x for x in sentence.split(' ')]
tokens_k = [x for x in kor_sentence.split(' ')]
print(tokens1)
print(tokens2)
print(tokens_k)
['I', 'like', 'to', 'pet', 'the', "cat's", 'soft', 'fur.']
["Don't", 'waste', 'your', 'youth.', "you're", 'not', 'always', 'young']
['아름다운', '도깨비', '불,', '외로운', '도깨비는,', '눈물많은', '도깨비일뿐']
# 간단한 예시
sentence = sentence.replace(",", "")
tokens = [x for x in sentence.split(' ')]
print(tokens)
["Don't", 'waste', 'your', 'youth.', "you're", 'not', 'always', 'young']
sentence = sentence.replace(",", "")
tokens = word_tokenize(msg)
print(tokens)
kor_sentence = kor_sentence.replace(",", "")
tokens_k = word_tokenize(kor_sentence)
print(tokens_k)
['I', 'like', 'to', 'pet', 'the', 'cat', "'s", 'soft', 'fur', '.']
['아름다운', '도깨비', '불', '외로운', '도깨비는', '눈물많은', '도깨비일뿐']
펜 트리뱅크(Penn Treebank)
미국 펜실베니아 대학교에서 개발한 영어 말뭉치(Corpus)
문장마다 정교한 주석이 달려있는 언어 자원
tokens1 = TextBlob(msg)
tokens2 = TextBlob(sentence)
tokens3 = TextBlob(kor_sentence)
print(tokens1)
print(tokens2)
print(tokens3)
print(tokens3.sentiment)
print(tokens1.sentiment)
print(tokens2.sentiment)
I like to pet the cat's soft fur.
Don't waste your youth. you're not always young
아름다운 도깨비 불, 외로운 도깨비는, 눈물많은 도깨비일뿐
Sentiment(polarity=0.0, subjectivity=0.0)
Sentiment(polarity=0.1, subjectivity=0.35)
Sentiment(polarity=-0.05, subjectivity=0.2)
TextBlob 라이브러리는 기본적으로 영어 자연어 처리에 최적화되어 있음
TextBlob의 주요 기능인 감성 분석, 품사 태깅, 명사구 추출 등은 주로 영어 텍스트에 대해 설계되었고, 이를 다른 언어에 적용하기 위해서는 추가적인 설정이나 자원이 필요
tokenizer = RegexpTokenizer(r"[가-힣\w]+") # 정규식을 이용
print(tokenizer.tokenize(msg))
print(tokenizer.tokenize(sentence))
print(tokenizer.tokenize(kor_sentence))
['I', 'like', 'to', 'pet', 'the', 'cat', 's', 'soft', 'fur'] \n
['Don', 't', 'waste', 'your', 'youth', 'you', 're', 'not', 'always', 'young']\n
['아름다운', '도깨비', '불', '외로운', '도깨비는', '눈물많은', '도깨비일뿐']
sentences = "I met Mr. kim. He earned Ph.D this year."
token = sent_tokenize(sentences)
print(token)
['I met Mr. kim.', 'He earned Ph.D this year.']
words = word_tokenize(sentences)
tokens = nltk.pos_tag(words)
print(tokens)
[('I', 'PRP'), ('met', 'VBD'), ('Mr.', 'NNP'), ('kim', 'NNP'), ('.', '.'), ('He', 'PRP'), ('earned', 'VBD'), ('Ph.D', 'NNP'), ('this', 'DT'), ('year', 'NN'), ('.', '.')]