--36.전처리&사전구축.ipynb--
!pip install konlpy
base_path = r'/content/drive/MyDrive/dataset/chatbot'
from konlpy.tag import Komoran
class Preprocess:
def init(self, userdic=None): # 생성자 (userdic=사용자 사전)
# ① 형태소 분석기 초기화
self.komoran = Komoran(userdic=userdic) # 사용자 정의 사전 파일의 경로 입력
# ② 제외할 품사 (불용어 제거)
# 참조 : https://docs.komoran.kr/firststep/postypes.html
# 관계언 제거, 기호 제거
# 어미 제거
# 접미사 제거
self.exclusion_tags = [
'JKS', 'JKC', 'JKG', 'JKO', 'JKB', 'JKV', 'JKQ',
'JX', 'JC',
'SF', 'SP', 'SS', 'SE', 'SO',
'EP', 'EF', 'EC', 'ETN', 'ETM',
'XSN', 'XSV', 'XSA'
]
def pos(self, sentence):
return self.komoran.pos(sentence)
def get_keywords(self, pos, without_tag=False):
f = lambda x : x in self.exclusion_tags
word_list = []
for p in pos:
if f(p[1]) is False:
word_list.append(p if without_tag is False else p[0])
return word_list
import os
sent = "내일 오전 10시에 탕수육 주문하고 싶어"
p = Preprocess(userdic = os.path.join(base_path, 'user_dic.tsv'))
pos = p.pos(sent)
pos
ret = p.get_keywords(pos, without_tag=False)
print(ret)
ret = p.get_keywords(pos, without_tag=True)
print(ret)
챗봇엔진에서 '의도분류' 및 '개체명 인식모델' 의 학습을 하려면 단어사전을 구축해야 한다
from tensorflow.keras import preprocessing
import pickle
def read_corpus_data(filename):
with open(filename, 'r', encoding='utf-8') as f:
data = [line.split('\t') for line in f.read().splitlines()]
return data
corpus_data = read_corpus_data(os.path.join(base_path, 'corpus.txt'))
len(corpus_data)
corpus_data[:10]
p = Preprocess()
dic = []
for c in corpus_data:
pos = p.pos(c[1])
for k in pos:
dic.append(k[0])
len(dic)
dic[:20]
tokenizer = preprocessing.text.Tokenizer(oov_token='OOV') # 어휘에 없으면 'OOV'로 Token 대체
tokenizer.fit_on_texts(dic)
word_index = tokenizer.word_index # 여기서 인덱스가 만들어진다.
print(word_index)
f = open(os.path.join(base_path, 'out', 'chatbot_dict.bin'), 'wb')
try :
pickle.dump(word_index, f)
except Exception as e :
print(e)
finally :
f.close()
word_index = None
f = open(os.path.join(base_path, 'out', 'chatbot_dict.bin'), 'rb')
word_index = pickle.load(f)
f.close()
p = Preprocess(userdic = os.path.join(base_path, 'user_dict.tsv'))
sent = '내일 오전 10시에 탕수육 주문하고 싶어 ㅋㅋ'
pos = p.pos(sent)
for word in p.get_keywords(pos, without_tag=True) :
try :
print(word, word_index[word])
except KeyError :
# 해당 단어가 사전에 없는경우 OOV 처리
print(word, word_index['OOV'])
Preprocess.py
from konlpy.tag import Komoran
import pickle
import jpype # 자바와 파이썬 간의 데이터 통신을 위한 모듈
class Preprocess:
def init(self, word2index_dic = '', userdic=None): # 생성자 (word2index_dic=단어사전, userdic=사용자 사전)
# 단어인덱스 사전 불러오기
if(word2index_dic != '') :
f = open(word2index_dic, 'rb')
self.word_index = pickle.load(f)
f.close()
else :
self.word_index = None
# ① 형태소 분석기 초기화
self.komoran = Komoran(userdic=userdic) # 사용자 정의 사전 파일의 경로 입력
# ② 제외할 품사 (불용어 제거)
# 참조 : https://docs.komoran.kr/firststep/postypes.html
# 관계언 제거, 기호 제거
# 어미 제거
# 접미사 제거
self.exclusion_tags = [
'JKS', 'JKC', 'JKG', 'JKO', 'JKB', 'JKV', 'JKQ',
'JX', 'JC',
'SF', 'SP', 'SS', 'SE', 'SO',
'EP', 'EF', 'EC', 'ETN', 'ETM',
'XSN', 'XSV', 'XSA'
]
def pos(self, sentence):
jpype.attachThreadToJVM()
return self.komoran.pos(sentence)
def get_keywords(self, pos, without_tag=False):
f = lambda x : x in self.exclusion_tags
word_list = []
for p in pos:
if f(p[1]) is False:
word_list.append(p if without_tag is False else p[0])
return word_list
def get_wordidx_sequence(self, keywords) :
if self.word_index is None :
return []
w2i = []
for word in keywords :
try :
w2i.append(self.word_index[word])
except KeyError :
w2i.append(self.word_index['OOV'])
return w2i