Skip to content

Instantly share code, notes, and snippets.

@yunsu3042
Last active July 13, 2021 23:18
Show Gist options
  • Save yunsu3042/d1db026b48de6ff4a2b605992716fc0a to your computer and use it in GitHub Desktop.
Save yunsu3042/d1db026b48de6ff4a2b605992716fc0a to your computer and use it in GitHub Desktop.

자연어 전처리 과정 (클렌징)

  1. 문장 띄어쓰기 처리
  2. 문장 분리
  3. 맞춤법 처리
  4. 정규식 처리
  5. 형태소 토크나이징 or subword 토크나이징

문장 띄어쓰기 처리

import re
from pykospacing import Spacing 
spacing = Spacing()

sentence = '김철수는 극중 두 인격의 사나이 이광수 역을 맡았다. 철수는 한국 유일의 태권도 전승자를 가리는 결전의 날을 앞두고 10년간 함께 훈련한 사형인 유연재(김광수 분)를 찾으러 속세로 내려온 인물이다.'
print(sentence)

sentence_no_space = re.sub("\s+", "", sentence)
print(sentence_no_space)

문장분리

import kss
line = "메이크업 관련 직종을 꿈꾸는 중학교 삼학년입니다 메이크업에 관련된 과를 가고 싶은데 학과가 있는 대학들이 궁금해요 그리고 꼭 실업계 고등학교를 가야 하나요 메이크업아티스트가 되면 어떻게 일할 수 있는지 자세히 알고 싶어요 그리고 무슨 무슨 자격증이 있으면 좋은지 궁금해요 미리 따놓을 수 있으면 따고 싶어서요"
kss.split_sentences(line)

맞춤법 처리

!pip install git+https://github.com/ssut/py-hanspell.git
from hanspell import spell_checker
wrong_sent = "맞춤법 틀리면 외 않되? 쓰고싶은대로쓰면돼지 "

checked_sent = spell_checker.check(wrong_sent).checked

정규식 처리

import re
text = """
디자인을 배우는 학생으로, 외국디자이너와 그들이 일군 전통을 통해 발전해가는 문화산업이 부러웠는데. 사실 우리나라에서도 그 어려운시절에 끝까지 열정을 지킨 노라노 같은 전통이있어 저와 같은 사람들이 꿈을 꾸고 이뤄나갈 수 있다는 것에 감사합니다."
"""
re.sub(",", "", text)

형태소 분석

from konlpy.tag import Okt, Kkma

twitter = Okt()
kkma = Kkma()

Subword

from soynlp import DoublespaceLineCorpus
from soynlp.word import WordExtractor
from soynlp.tokenizer import LTokenizer

urllib.request.urlretrieve("https://raw.githubusercontent.com/lovit/soynlp/master/tutorials/2016-10-20.txt", filename="2016-10-20.txt")
corpus = DoublespaceLineCorpus("./dataset/naver/ratings.text.txt")

word_extractor = WordExtractor()
word_extractor.train(corpus)
word_score_table = word_extractor.extract()

text = "안전성에 문제있는 스마트폰을 휴대하고 탑승할 경우에 압수한다"
scores = {word:score.cohesion_forward for word, score in word_score_table.items()}
l_tokenizer = LTokenizer(scores=scores)
l_tokenizer.tokenize(text, flatten=False)

자연어 전처리 과정2(이번주 진행예정)

  • 모델에서 원하는 형태로 데이터 변환하기
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment