728x90
반응형
토큰화란?
토큰(token)이라 불리는 단위로 나누는 작업을 토큰화(tokenization)라고한다. 토큰의 단위가 상황에 따라 다르지만, 보통 의미있는 단위로 토큰을 정의한다.
단어 토큰화 (Word Tokenization)
토큰의 기준을 단어(word)로 하는 경우, 단어 토큰화(word tokenization)라고 한다.
다만, 여기서 단어(word)는 단어 단위 외에도 단어구, 의미를 갖는 문자열로도 간주되기도 한다.
from nltk.tokenize import word_tokenize
text = "God is Great! I won a lottery."
print(word_tokenize(text))
['God', 'is', 'Great', '!', 'I', 'won', 'a', 'lottery', '.']
토큰화 선택의 순간
토큰화를 하는 패키지에 따라 여러가지 방법이 있다. 예를 들어 다음과 같이 어퍼스토리피(')를 포함하느냐 별도로 불리하느냐 선택의 순간이 있다.
Don't
Don t
Dont
Do n't
from nltk.tokenize import word_tokenize
from nltk.tokenize import WordPunctTokenizer
from tensorflow.keras.preprocessing.text import text_to_word_sequence
text = "I don't know"
print(word_tokenize(text))
print(WordPunctTokenizer().tokenize(text))
print(text_to_word_sequence(text))
['I', 'do', "n't", 'know', '.']
['I', 'don', "'", 't', 'know', '.']
['i', "don't", 'know']
토큰화 고려해야 할 사항
- 구두점이나 특수 문자를 단순 제외해서는 안 된다.
- 줄임말과 단어 내에 띄어쓰기가 있는 경우 (띄어쓰기가 존재하더라도 하나의 단어로 규정해야 되는 경우가 존재)
표준 토큰화 예제
- 규칙 1. 하이푼으로 구성된 단어는 하나로 유지한다.
- 규칙 2. doesn't와 같이 아포스트로피로 '접어'가 함께하는 단어는 분리한다.
from nltk.tokenize import TreebankWordTokenizer
tokenizer = TreebankWordTokenizer()
text = "Starting a home-based restaurant may be an ideal. it doesn't have a food chain or restaurant of their own."
print(tokenizer.tokenize(text))
# output : ['Starting', 'a', 'home-based', 'restaurant', 'may', 'be', 'an', 'ideal.', 'it', 'does', "n't", 'have', 'a', 'food', 'chain', 'or', 'restaurant', 'of', 'their', 'own', '.']
문장 토큰화 (Sentence Tokenization)
코퍼스 내에서 문장 단위로 구분하는 작업으로 때로는 문장 분류(sentence segmentation)라고도 부른다.
영문
from nltk.tokenize import sent_tokenize
text = "Natural language processing (NLP) uses machine learning to reveal the structure and meaning of text. With natural language processing applications, organizations can analyze text and extract information about people, places, and events to better understand social media sentiment and customer conversations.Learn how to derive insigh"
print('문장 토큰화1 : {}'.format(sent_tokenize(text)))
text = "I am actively looking for Doctor.CHO. students. and you are a Doctor.CHO student."
print('문장 토큰화2 : {}'.format(sent_tokenize(text)))
# output : 문장 토큰화1 : ['Natural language processing (NLP) uses machine learning to reveal the structure and meaning of text.', 'With natural language processing applications, organizations can analyze text and extract information about people, places, and events to better understand social media sentiment and customer conversations.Learn how to derive insigh']
# output : 문장 토큰화2 : ['I am actively looking for Doctor.CHO.', 'students.', 'and you are a Doctor.CHO student.']
한글
import kss
text = '자연어 처리(NLP)는 머신러닝을 사용하여 텍스트의 구조와 의미를 파악합니다. 자연어 처리 애플리케이션을 사용하면 조직에서 텍스트를 분석하고 사람, 장소, 사건에 대한 정보를 추출하여 소셜 미디어 감정과 고객 대화를 더욱 정확하게 이해할 수 있습니다.'
print(kss.split_sentences(text))
# output : ['자연어 처리(NLP)는 머신러닝을 사용하여 텍스트의 구조와 의미를 파악합니다.', '자연어 처리 애플리케이션을 사용하면 조직에서 텍스트를 분석하고 사람, 장소, 사건에 대한 정보를 추출하여 소셜 미디어 감정과 고객 대화를 더욱 정확하게 이해할 수 있습니다.']
728x90
반응형
'AI 인공지능 > NLP' 카테고리의 다른 글
fairseq를 활용한 기계 번역 (0) | 2022.08.31 |
---|---|
한글 형태소 분석기 종류 (0) | 2022.08.31 |
기계 번역 - 오픈 소스 벤치마킹 (0) | 2022.08.09 |
트랜스포머 (Transformer) (0) | 2021.01.19 |
Attention (어텐션) (0) | 2021.01.15 |