반응형

[Python 3/Natural Language Processing] - 단어 토큰화(word tokenization)

 

단어 토큰화(word tokenization)

NLP이전에 방대한 양의 문장들을 보다 쉽게 분석하고 가지고 놀기위해 어느정도 정제(cleansing)하고 정규화하는 작업이 요구됩니다. 그리고 정제와 정규화 이전에 사용자의 목적에 맞게 데이터를 토큰화하는 작업..

leo-bb.tistory.com

단어 토큰화 이전에 문서의 양이 방대해지는 경우 바로 단어 토큰화를 진행하는 것보다 문장을 토큰화해 1차적으로 정제하고 단어 토큰화를 진행하는 것도 좋은 방법입니다.

본 예제에서는 문장 단위의 토큰화 실습가 더불어 한글로 이루어진 문장의 단어 토큰화(word tokenization)을 함께 소개합니다. 

from nltk.tokenize import sent_tokenize
from konlpy.tag import Hannanum
from konlpy.tag import Kkma
from konlpy.tag import Okt

hannanum = Hannanum()
kkma = Kkma()
okt = Okt()

한글 토큰화 실습을 위해 konlpy 모듈을 설치하여 사용하였습니다. konlpy가 설치되어 있지 않은 경우 

http://konlpy.org/en/latest/#standing-on-the-shoulders-of-giants 링크에 접속하여 설명에 따라 설치해주시면 됩니다.(반드시 설명되어 있는 순서에 맞게 설치하셔야합니다!)

 

KoNLPy: Korean NLP in Python — KoNLPy 0.5.2 documentation

KoNLPy: Korean NLP in Python KoNLPy (pronounced “ko en el PIE”) is a Python package for natural language processing (NLP) of the Korean language. For installation directions, see here. For users new to NLP, go to Getting started. For step-by-step instructi

konlpy.org

text1 = "Hello, I graduated from Yonsei University. My major is bio medical engineering. But these days, I'm more interested in machine learning and data science."
text2 = "자연어 처리 어렵네요. 특히 한국어 처리는 더 어려운거 같아요. 다들 화이팅하세요!"

print("sent_tokenize를 사용하면 아래와 같이 토큰화됩니다.")
print(sent_tokenize(text1))
print(sent_tokenize(text2))

print("Hannanum을 쓰면 아래와 같이 분류됩니다.")
print(hannanum.morphs(text1))
print(hannanum.morphs(text2))

print("Kkma를 쓰면 아래와 같이 분류됩니다.")
print(kkma.morphs(text1))
print(kkma.morphs(text2))

print("Okt를 사용하면 아래와 같이 분류됩니다.")
print(okt.morphs(text1))
print(okt.morphs(text2))

sent_tokenize를 사용하면 아래와 같이 토큰화됩니다.
['Hello, I graduated from Yonsei University.', 'My major is bio medical engineering.', "But these days, I'm more interested in machine learning and data science."]
['자연어 처리 어렵네요.', '특히 한국어 처리는 더 어려운거 같아요.', '다들 화이팅하세요!']
Hannanum을 쓰면 아래와 같이 분류됩니다.
['Hello', ',', 'I', 'graduated', 'from', 'Yonsei', 'University', '.', 'My', 'major', 'is', 'bio', 'medical', 'engineering', '.', 'But', 'these', 'days', ',', 'I', "'", 'm', 'more', 'interested', 'in', 'machine', 'learning', 'and', 'data', 'science', '.']
['자연어', '처리', '어렵', '네', '요', '.', '특히', '한국어', '처리', '는', '더', '어려운거', '같', '아', '요', '.', '다들', '화이팅하세요', '!']
Kkma를 쓰면 아래와 같이 분류됩니다.
['Hello', ',', 'I', 'graduated', 'from', 'Yonsei', 'University', '.', 'My', 'major', 'is', 'bio', 'medical', 'engineering', '.', 'But', 'these', 'days', ',', 'I', "'", 'm', 'more', 'interested', 'in', 'machine', 'learning', 'and', 'data', 'science', '.']
['자연어', '처리', '어렵', '네요', '.', '특히', '한국어', '처리', '는', '더', '어렵', 'ㄴ', '거', '같', '아요', '.', '다', '들', 'ㄹ', '화이팅하세', '요', '!']       
Okt를 사용하면 아래와 같이 분류됩니다.
['Hello', ',', 'I', 'graduated', 'from', 'Yonsei', 'University', '.', 'My', 'major', 'is', 'bio', 'medical', 'engineering', '.', 'But', 'these', 'days', ',', 'I', "'", 'm', 'more', 'interested', 'in', 'machine', 'learning', 'and', 'data', 'science', '.']
['자연어', '처리', '어렵네요', '.', '특히', '한국어', '처리', '는', '더', '어려운거', '같아요', '.', '다', '들', '화이팅', '하세요', '!']

 

본자료는 딥 러닝을 이용한 자연어 처리 입문(Won Joon Yoo)을 참고합니다.
코드 전문은  https://github.com/Leo-bb/natural-language-processing에서 확인할 수 있습니다.
 

Leo-bb/natural-language-processing

Contribute to Leo-bb/natural-language-processing development by creating an account on GitHub.

github.com


jpype._jvmfinder.JVMNotFoundException:

No JVM shared library file (jng>vm.dll) found. Try setting up the JAVA_HOME environment variable properly. 오류가 발생하시는 경우

 

아나콘다설치위치\Lib\site-packages\jpype 에 들어가서 _jvmfinder.py 파일을 여신 다음

ctrl + F 로 java_home 검색 후 java_home = "자바설치경로" 로 해주시면 오류없이 실행됩니다.

 

반응형
복사했습니다!