토큰화
문장/다니어 단위로 쪼개기
코퍼스 Corpus에서 분리자 Separator를 포함하지 않는 연속적인 문자열 단위로 문장, 단어 단위로 토큰화한다.
* 코퍼스 Corpus 자연어 처리에서 사용하는 대량의 텍스트 데이터 집합
- 토큰화 단위에 따라 문장, 단어단위로 토큰화한다. sent_tokenize
[
'Natural Language Processing is interesting.',
'I am studying NLP.',
'Tokenization is important.'
]from nltk.tokenize import sent_tokenize
text = """
Natural Language Processing is interesting.
I am studying NLP.
Tokenization is important.
"""
sentences = sent_tokenize(text)
print(sentences)
[
'Natural Language Processing is interesting.',
'I am studying NLP.',
'Tokenization is important.'
]
from nltk.tokenize import word_tokenize
sentence = "Natural Language Processing is interesting."
tokens = word_tokenize(sentence)
print(tokens)
['Natural', 'Language', 'Processing', 'is', 'interesting', '.']
- 어절단위토큰화는 영문에 적합하고, 형태소단위 토큰화, 공백 기준 분리 한글에 적합하다.
sentence = "자연어 처리는 매우 재미있습니다."
tokens = sentence.split()
print(tokens)
- subword는 형태소와 유사하나 의미대신 통계적 방법을 적용한다. 형태소단위 okt.morphs, 품사단위okt.pos
from konlpy.tag import Okt
okt = Okt()
sentence = "자연어처리는 매우 재미있습니다."
tokens = okt.morphs(sentence)
print(tokens)
['자연어', '처리', '는', '매우', '재미있습니다', '.']
from konlpy.tag import Okt
okt = Okt()
sentence = "학생들이 열심히 공부한다."
tokens = okt.pos(sentence)
print(tokens)
[
('학생', 'Noun'),
('들', 'Suffix'),
('이', 'Josa'),
('열심히', 'Adverb'),
('공부', 'Noun'),
('한다', 'Verb'),
('.', 'Punctuation')
]
- nltk Natural Language Toolkit 라이브러리를 사용한다. sent_tokenize, word_tokenize
from nltk.tokenize import sent_tokenize, word_tokenize
text = """
Python is easy.
NLP is powerful.
"""
sentences = sent_tokenize(text)
for sentence in sentences:
print(word_tokenize(sentence))