텍스트 분석은 주로 비정형 텍스트 데이터를 머신러닝, 통계 등의 방법으로 예측 분석이나 유용한 정보를 추출하는 데 중점을 둔다.
종류
텍스트 분류: 문서가 특정 분류 또는 카테고리에 속하는 것을 예측 (연예 / 정치 / 스포츠 같은 카테고리 분류 혹은 스팸 메일 검출). 지도 학습
감성 분석: 텍스트에서 주관적 요소를 분석하는 기법. 지도 혹은 비지도.
텍스트 요약: 텍스트 내에서 주제나 중심 사상을 추출
텍스트 군집화: 비슷한 유형의 문서를 군집화 하는 것. 비지도 학습
프로세스
텍스트 전처리: 대 / 소문자 변경, 특수 문자 제거, 토큰화, 불용어 제거, 어근 추출 등의 정규화 작업
피처 벡터화 / 추출: 텍스트에서 피처를 추출하고 벡터 값을 할당. BOW와 Word2Vec이 대표적
ML 모델 수립 및 학습 / 예측 / 평가
전처리
클렌징: 문자, 기호 등을 사전에 제거
토큰화
문장 토큰화: 마침표, 개행문자 등을 기준으로 문장을 분리. 각 문장이 가지는 의미가 중요한 경우 사용.
단어 토큰화: 공백, 콤마, 마침표, 개행문자 등으로 단어를 분리.
n-gram: 단어의 연속된 n개를 묶어서 하나의 단위로 처리하는 방법. 문장이 가지는 의미를 조금이라도 보존할 수 있다.
from nltk import sent_tokenizeimport nltknltk.download('punkt') # 문장을 분리하는 마침표, 개행문자 등의 데이터 셋 다운로드nltk.download('punkt_tab')text_sample ="The Matrix is everywhere its all around us, here even in this room. You can see it when you look out your window or when you turn on your television. You can feel it when you go to work, when you go to church, when you pay your taxes."sentences = sent_tokenize(text_sample)print(sentences)
['The Matrix is everywhere its all around us, here even in this room.', 'You can see it when you look out your window or when you turn on your television.', 'You can feel it when you go to work, when you go to church, when you pay your taxes.']
[nltk_data] Downloading package punkt to
[nltk_data] /home/cryscham123/nltk_data...
[nltk_data] Package punkt is already up-to-date!
[nltk_data] Downloading package punkt_tab to
[nltk_data] /home/cryscham123/nltk_data...
[nltk_data] Package punkt_tab is already up-to-date!
from nltk import word_tokenizesentence ="The Matrix is everywhere its all around us, here even in this room."words = word_tokenize(sentence)print(words)
def tokenize_text(text): sentences = sent_tokenize(text) words = [word_tokenize(sentence) for sentence in sentences]return wordsword_tokens = tokenize_text(text_sample)word_tokens
sw = stopwords.words('english')all_tokens = []for sentence in word_tokens: filtered_words = []for word in sentence: word = word.lower()if word notin sw: filtered_words.append(word) all_tokens.append(filtered_words)print(all_tokens)