- 한글 문서 띄어쓰기 교정의 필요성
형태소 분석 성능 up! => 이후 모델 input의 질 up! => 모델 성능 up!
- 사용 가능한 패키지
Soyspacing – https://github.com/lovit/soyspacing : 휴리스틱 알고리즘 기반 띄어쓰기 모델. 주어진 알고리즘으로 input으로 들어간 문장들에서 띄어쓰기 규칙을 파악하고 이를 새로운 문장에 적용. 모든 한글 문서에 광범위하게 적용할 수 있는 모델을 내기에는 한계가 있겠지만, 동질적인 주제와 형식을 가진 문서들에 대해 분석을 하는 경우 특히 유용하게 쓸 수 있을 것 같다. (특정 도메인을 위한 모델)
PyKoSpacing – https://github.com/haven-jeon/PyKoSpacing : CNN에 RNN을 쌓아 올린 모델을 뉴스 데이터로 훈련. 세종 코퍼스 등 테스트 셋에서 잘 작동한다고 한다. 하지만 accuracy measure가 성능을 over represent 할 수 있을 것 같다. 대체로 띄어쓰기가 정말 난장판으로 되어있는 경우 볼만한 문서로 바꾸는데 사용할 수 있을 것 같다.
TaKos (Alpha) – https://github.com/Taekyoon/takos-alpha : 앞서 첨부한 Youtube 영상에서 소개된 프로젝트. 아직 상용화까지 개발이 진행 중인 것 같다!
- Soyspacing 활용예시
비슷한 주제/형태 (특정 도메인)의 문서를 다루고 있기에, Soyspacing으로 띄어쓰기 규칙을 발견하고 전체 문서에 적용해 보고자 하였다.
가지고 있는 한글 문서를 텍스트 파일로 저장
with open(os.path.join(dir_to_save_on, 'spacing_train_full.txt'), 'w') as f1:
for parag in text:
f1.write(parag + '\n')
f1.close()
위 텍스트 파일로 띄어쓰기 규칙 발견
from soyspacing.countbase import CountSpace
#Train a simple huristic model -- takes about 45mins with 4000000 sentences.
corpus_fname = os.path.join(dir_, 'train_sample', 'spacing_train_full.txt')
model = CountSpace()
model.train(corpus_fname)
#Save the model
model_fname = os.path.join(dir_, 'train_sample', 'spacing_model_full_0706')
model.save_model(model_fname, json_format=False)
#Load the model
model_fname = os.path.join(dir_, 'train_sample', 'spacing_model_full_0706')
model = CountSpace()
model.load_model(model_fname, json_format=False)
적용 사례
#good case
print(model.correct('이번공산베트남에'))
--> returned ('이번 공산 베트남에', [0, 1, 0, 1, 0, 0, 0, 1])
#bad case
print(model.correct('현대자동차기아자동차'))
--> returned ('현대자동차기아자동차', [0, 0, 0, 0, None, 0, 0, 0, 0, 1])
문서 뭉치에서 회사 이름의 경우 띄어쓰기가 잘 지켜지지 않는 문제가 종종 있었고, 방어적으로 띄어쓰기를 하는 이 모델의 특성으로 인해 bad case가 발생. 회사 이름 사이를 띄어 쓰게 만들기 위해서 아래의 코드로 일종의 사용자 규칙을 부여할 수 있다.
#Making a rule based dict
firms = ['현대자동차', '기아자동차', .....]
with open(os.path.join(dir_, 'train_sample', 'spacing_firm_names.txt'), 'w') as f1:
for firm in firms:
f1.write(firm + '\t' + '1' + '0'*(len(firm)-1) +'1' + '\n')
f1.close()
이렇게 만든 사용자 규칙을 RuleDict 함수로 넣어주면 모델에 반영된다.
from soyspacing.countbase import RuleDict
rule_dict = RuleDict(os.path.join(dir_, 'train_sample', 'spacing_firm_names.txt'))
model.correct('현대자동차기아자동차', rules=rule_dict)[0]
--> returned '현대자동차 기아자동차'
끝!