본문 바로가기

프로젝트/Project1

Toy Project 1 (8) - Text preprocessing - Tokenization

Tokenization을 진행하기 위해 정리된 csv 파일을 x_data와 y_target으로 나눈다. 이 프로젝트의 경우 Term1에는 False data가 있고 Term2에는 True data가 있기 때문에 아래와 같이 진행했다.

 

 

 

 

data와 target으로 나눈 후 학습을 위한 train data와 평가를 위한 test data로 나눈다. 

 

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, shuffle=True, stratify=target, random_state=7)

 

 

 

mecab을 import하여 토큰화를 진행한다.

 

 

 

사용 빈도가 높은 단어 토큰을 확인한다.

 

 

 

 

빈도수가 높은 단어를 선택한다. word imbedding 방법으로 단순한 count vector를 사용한다.

 

 

전처리가 끝났다. 아래는 사용한 코드

 

from konlpy.tag import Mecab
mecab = Mecab()
import json
import os
from pprint import pprint

def tokenize(doc):
    return ['/'.join(t) for t in mecab.pos(doc)]

train_path = '/Users/jason/Test/data/train_docs.json'
test_path = '/Users/jason/Test/data/test_docs.json'

if os.path.isfile(train_path):
    with open(train_path) as f:
        train_docs = json.load(f)
    with open(test_path) as f:
        test_docs = json.load(f)
else:
    train_docs = [tokenize(row) for row in x_train]
    test_docs = [tokenize(row) for row in x_test]
    # JSON 파일로 저장
    with open(train_path, 'w', encoding="utf-8") as make_file:
        json.dump(train_docs, make_file, ensure_ascii=False, indent="\t")
    with open(test_path, 'w', encoding="utf-8") as make_file:
        json.dump(test_docs, make_file, ensure_ascii=False, indent="\t")
        
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
%matplotlib inline

plt.rcParams['axes.unicode_minus']=False #한글 처리
rc('font',family='AppleGothic')

plt.figure(figsize=(20,10))
text.plot(50)

selected_words = [f[0] for f in text.vocab().most_common(10000)]

def term_frequency(doc):
    return [doc.count(word) for word in selected_words]

x_train = [term_frequency(d) for d in train_docs]
x_test = [term_frequency(d) for d in test_docs]

import numpy as np

X_train = np.asarray(x_train).astype('float32')
X_test = np.asarray(x_test).astype('float32')

Y_train = np.asarray(y_train).astype('float32')
Y_test = np.asarray(y_test).astype('float32')