반응형

[2단계] 데이터 전처리 (Preprocessing 및 파이썬 자연어처리 라이브러리 정리)



데이터 전처리 순서 (Preprocessing Step)


  1. 토큰화 (Tokenization)
    • 문자열에서 단어로 분리시키는 단계
  2. 불용어 제거 (Stop word elimination)
    • 전치사, 관사 등 너무 많이 등장하는 단어 등 문장이나 문서의 특징을 표현하는데 불필요한 단어를 삭제하는 단계
  3. 어간 추출 (Stemming)
    • 단어의 기본 형태를 추출하는 단계
  4. 문서 표현 (Representation)
    • 주어진 문서나 문장을 하나의 벡터로 표현하는 단계
    • 단어들을 모두 인덱싱(indexing)하고 주어진 문서에 존재하는 단어의 빈도수를 사용하여 문서를 표현


자연어처리를 위한 Python 라이브러리 종류


1. KoNLPy (코엔엘파이)http://konlpy-ko.readthedocs.io/ko/v0.4.3/#

  • 한국어 자연어처리를 위한 대표적인 파이썬 라이브러리 
  • NLP (Natural Language Processing, 자연어처리)란? 텍스트(Text)에서 의미있는 정보를 분석, 추출하고 이해하는 일련의 기술 집합
  • Twitter, Komoran, Mecab 등 다양한 형태소 분석기를 내장하고 있음

관련된 글


2. NLTK (Natural Language Toolkit) http://www.nltk.org/

  • 영어로된 텍스트의 자연어처리를 위한 대표적인 파이썬 라이브러리
  • NLTK is a leading platform for building Python programs to work with human language data.
  • It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning, wrappers for industrial-strength NLP libraries, and an active discussion forum.


3. Gensim https://radimrehurek.com/gensim/

  • 주로 Topic modeling, Corpus(말뭉치) 및 Word Embedding 모델을 지원해줌
  • 한국어 및 다양한 언어를 지원해줌
관련된 글

전처리 과정


1. 토큰화 (Tokenization) : 단어를 떼어내는 단계

ex) I loved you. data-mining

= I / love / you 로 띄어쓰기나 개행을 기준으로 단어를 떼어내는 것을 말한다. 

  • 1-1) 단어를 띄어쓰기와 개행을 기준으로 단어를 떼어낸다.   ex) I / loved / you. / data-mining
  • 1-2) 문장 부호를 기준으로 한번 더 떼어낸다.                      ex) I / loved / you / . / data / - / mining
  • 1-3) 문장 부호를 떼어 내는데 예외를 둔다.                         ex) I / loved / you / . / data-mining

파이썬 3.6 Tokenizer 가이드 : https://docs.python.org/3/library/tokenize.html

파이썬 2.7 Tokenizer 가이드 : https://docs.python.org/2.7/library/tokenize.html


2. 불용어 제거 (Stop word elimination) : 불필요한 단어들을 제거하는 단계

  • 2-1) 모든 단어를 소문자화한다.
  • 2-2) 불용어 사전을 검색하여 불용어를 삭제한다. 
  • 2-3) 전제 말뭉치(corpus)에서 n번 이상 등장하지 않는 단어를 삭제한다.
    • why? 별로 중요한 단어라고 생각하지 않기 때문에

3. 어간 추출 (Stemming)
  • 단어를 기본형으로 표현하여 같은 단어가 표현형 때문에 다른 단어로 인덱싱(indexing)되는 일 없도록 한다.
  • 이때, 다양한 알고리즘을 사용하는데, 기본형으로부터 단어들을 자동으로 파생시켜서 리스트를 만들고 매칭을 시키기도 한다.
관련된 글
  • https://datascienceschool.net/view-notebook/118731eec74b4ad3bdd2f89bab077e1b/

4. 문서 표현 (Representation)

다양한 알고리즘을 사용하여서 문서(Text)를 벡터(Vector)값으로 변환하는 단계이다. (=임베딩, Embedding)
이렇게 바뀐 벡터(Vector)값을 통해서 우리는 유클리디안 거리, 코사인 유사도 등을 통해 단어간의 유사성 등을 구할 수 있다.
  • word2vec
  • doc2vec

Dictionary (사전)
  • 단어와 단어에 대한 인덱스가 표시된 사전
  • 단어 자체를 사용하여 문서를 표현하면 비효율적이다.
  • 문서 집합에 있는 문서 전체를 읽어가면서 토큰화, 불용어 제거 등의 순서를 진행하여 사전을 구축한다.
  • 불용어, 어간추출 등의 비적용/부분적용이 가능하다. (선택적)
  • ex) 사전 파일 (line 번호를 단어 index로 간주)


예제

뉴스 제목 = 한글 + 영어 + 한자 + 특수문자 

1차 가정 : 한글만 허용하고 나머지는 삭제해서 학습시켜보기 (추후에 regex 라이브러리 찾아보기, + 영어 라이브러리 적용)

Read Document 
from konlpy.corpus import kobill    # Docs from pokr.kr/bill
files_ko = kobill.fileids()         # Get file ids
doc_ko = kobill.open('1809890.txt').read()


Tokenize 

from konlpy.tag import Twitter; t = Twitter()
tokens_ko = t.morphs(doc_ko)

Load tokens
import nltk
ko = nltk.Text(tokens_ko, name='대한민국 국회 의안 제 1809890호')   # For Python 2, input `name` as u'유니코드'

기능1) tokens 
print(len(ko.tokens))       # returns number of tokens (document length)
print(len(set(ko.tokens)))  # returns number of unique tokens
ko.vocab()        
기능2) count 
ko.count('초등학교')   # Counts occurrences
기능3) Concordance 
ko.concordance('초등학교')
기능4) Similar word 
ko.similar('자녀')
ko.similar('육아휴직')
기능5) Collocations
en.collocations()
POS Tagging
from konlpy.tag import Twitter; t = Twitter()
tags_ko = t.pos("작고 노란 강아지가 페르시안 고양이에게 짖었다")

# 수집된 txt 파일을 한 줄씩 \n (줄바꿈)을 기준으로 읽어들인다.
def read_data(filename):
with open(filename, 'r') as f:
data = [line.split('\t') for line in f.read().splitlines()]
data = data[1:] # header 제외
return data
train_data = read_data('ratings_train.txt')

test_data = read_data('ratings_test.txt')

# row, column의 수가 제대로 읽혔는지 확인
print(len(train_data)) # nrows: 150000
print(len(train_data[0])) # ncols: 3
print(len(test_data)) # nrows: 50000

print(len(test_data[0])) # ncols: 3


# 형태소(POS) 분석을 하고, Tokenizing 한다.

    • Q. 꼭 형태소/품사를 나눠야하는가? 
      데이터가 정말 충분하다면, 어절 단위로도 분석이 가능하지만 데이터가 적으므로 형태소로 나누는 것이 필요하다.
      품사를 태깅해둔다면 동음이의어를 구분할 수 있다. 
from konlpy.tag import Twitter
pos_tagger = Twitter()
def tokenize(doc):
# norm, stem은 optional
return ['/'.join(t) for t in pos_tagger.pos(doc, norm=True, stem=True)]
train_docs = [(tokenize(row[1]), row[2]) for row in train_data] # Training Data ( 학습용 데이터 )
test_docs = [(tokenize(row[1]), row[2]) for row in test_data] # Test Data ( 검증용 데이터 )
# 잘 들어갔는지 확인
from pprint import pprint
pprint(train_docs[0])
# => [(['아/Exclamation',
# '더빙/Noun',
# '../Punctuation',
# '진짜/Noun',
# '짜증/Noun',
# '나다/Verb',
# '목소리/Noun'],

# '0')]


# 말뭉치(Corpus)가 어떤 특징을 가지고 있는지 Training Data의 Token 모으기

tokens = [t for d in train_docs for t in d[0]]
print(len(tokens))
# => 2194536

# NLTK를 통한 tokenize
import nltk
text = nltk.Text(tokens, name='NMSC')
print(text)
# => <Text: NMSC>

print(len(text.tokens)) # returns number of tokens
# => 2194536
print(len(set(text.tokens))) # returns number of unique tokens
# => 48765
pprint(text.vocab().most_common(10)) # returns frequency distribution
# => [('./Punctuation', 68630),
# ('영화/Noun', 51365),
# ('하다/Verb', 50281),
# ('이/Josa', 39123),
# ('보다/Verb', 34764),
# ('의/Josa', 30480),
# ('../Punctuation', 29055),
# ('에/Josa', 27108),
# ('가/Josa', 26696),


Doc2vec 예제 

from collections import namedtuple
TaggedDocument = namedtuple('TaggedDocument', 'words tags')
# 여기서는 15만개 training documents 전부 사용함
tagged_train_docs = [TaggedDocument(d, [c]) for d, c in train_docs]

tagged_test_docs = [TaggedDocument(d, [c]) for d, c in test_docs]


from gensim.models import doc2vec
# 사전 구축
doc_vectorizer = doc2vec.Doc2Vec(size=300, alpha=0.025, min_alpha=0.025, seed=1234)
doc_vectorizer.build_vocab(tagged_train_docs)
# Train document vectors!
for epoch in range(10):
doc_vectorizer.train(tagged_train_docs)
doc_vectorizer.alpha -= 0.002 # decrease the learning rate
doc_vectorizer.min_alpha = doc_vectorizer.alpha # fix the learning rate, no decay
# To save

# doc_vectorizer.save('doc2vec.model')


pprint(doc_vectorizer.most_similar('공포/Noun'))
# => [('서스펜스/Noun', 0.5669919848442078),
# ('미스터리/Noun', 0.5522832274436951),
# ('스릴러/Noun', 0.5021427869796753),
# ('장르/Noun', 0.5000861287117004),
# ('판타지/Noun', 0.4368450343608856),
# ('무게/Noun', 0.42848479747772217),
# ('호러/Noun', 0.42714330554008484),
# ('환타지/Noun', 0.41590073704719543),
# ('멜로/Noun', 0.41056352853775024),
# ('공포영화/Noun', 0.4052993059158325)]


해당 예제는 https://www.lucypark.kr/courses/2015-dm/text-mining.html 를 참고하여 만들어졌습니다. 



참고 문서


반응형

+ Recent posts