ITStudy/Python

[Python]NLTK를 활용한 텍스트 처리-설치 및 토큰화, 어근동일화

Todays Goal 2021. 6. 6. 11:00
728x90
반응형

이번에는 NLTK를 활용하는 방법에 대해서 알아보겠습니다.

이번에 알아볼 사항은 아래와 같습니다.

1. NLTK 설치

2. NLTK 단어 및 문장 단위 토큰화(tokenization)

3. NLTK 어근 동일화(stemming)

 

NLTK의 설치

NLTK 설치

NLTK의 설치는 아래의 코드를 실행 시키면 간단하게 설치할 수 있습니다.

pip install nltk

개별 라이브러리의 설치

NLTK는 여러개의 라이브러리들이 합쳐져있는 패키지이기 때문에 특정한 라이브러리를 사용하기 위해서는 추가 설치작업이 필요합니다.

가령 불용어처리를 위해 stpwords를 실행시키고자 할 때 없다면 에러코드가 발생하기 때문에 정상적으로 사용하고자 할때는 설치하고자 할 때는 아래의 코드를 실행하여 설치할 수 있습니다.

import nltk

nltk.download('stopwords')

# nltk.download(): 전체 라이브러리를 일괄적으로 다운 받고 싶을 시 실행하면 Downloader가 실행됩니다.

 

NLTK 토큰화(tokenization)

머신러닝에서 토큰화는 언어학처럼 엄격하게 구분하진 않는 것 같습니다.

따라서, 그 결과물도 사용하는 토크나이저에 따라 다를 수 있습니다.

필요에 따라서 단순히 어절단위(띄어쓰기)로도 토큰을 구분할 수 있고 그 접근 방식은 단어 혹은 문장 단위일 수 있습니다.

from nltk.tokenize import sent_tokenize as st, word_tokenize as wt

text="""The approach most frequently found in text mining systems involves breakingthe text into sentences and words, 
which is calledtokenization.
The main challengein identifying sentence boundaries in the English language is distinguishing betweena period 
that signals the end of a sentence and a period that is part of a previoustoken like Mr., Dr., and so on.[1]"""

tokens=text.split()
tokens2=wt(text)
tokens3=st(text)

print(tokens)
print(tokens2)
print(tokens3)

위 코드의 실행 결과물은 아래와 같이 나타납니다.

split()는 단순히 띄어쓰기를 기준으로 구분하고 word_tokenize는 '.' 등의 구두점 등도 각기 구분해 주는 걸 볼 수 있습니다.

sent_tokenize는 '. '를 구분자로 하여 나뉘는 걸 볼 수 있습니다.

#token: split()
['The', 'approach', 'most', 'frequently', 'found', 'in', 'text', 'mining', 'systems', 'involves', 'breakingthe', 'text', 'into', 'sentences', 'and', 'words,', 'which', 'is', 'calledtokenization.', 'The', 'main', 'challengein', 'identifying', 'sentence', 'boundaries', 'in', 'the', 'English', 'language', 'is', 'distinguishing', 'betweena', 'period', 'that', 'signals', 'the', 'end', 'of', 'a', 'sentence', 'and', 'a', 'period', 'that', 'is', 'part', 'of', 'a', 'previoustoken', 'like', 'Mr.,', 'Dr.,', 'and', 'so', 'on.[1]']

#token2: sent_tokenize
['The', 'approach', 'most', 'frequently', 'found', 'in', 'text', 'mining', 'systems', 'involves', 'breakingthe', 'text', 'into', 'sentences', 'and', 'words', ',', 'which', 'is', 'calledtokenization', '.', 'The', 'main', 'challengein', 'identifying', 'sentence', 'boundaries', 'in', 'the', 'English', 'language', 'is', 'distinguishing', 'betweena', 'period', 'that', 'signals', 'the', 'end', 'of', 'a', 'sentence', 'and', 'a', 'period', 'that', 'is', 'part', 'of', 'a', 'previoustoken', 'like', 'Mr.', ',', 'Dr.', ',', 'and', 'so', 'on', '.', '[', '1', ']']

#tok2n3: sent_tokenize
['The approach most frequently found in text mining systems involves breakingthe text into sentences and words, \nwhich is calledtokenization.', 'The main challengein identifying sentence boundaries in the English language is distinguishing betweena period \nthat signals the end of a sentence and a period that is part of a previoustoken like Mr., Dr., and so on.', '[1]']

 

NLTK 어근 동일화(stemming)

한국어는 어미, 조사 등에 따라 단어의 형태가 바뀌는 걸 볼 수가 있습니다.

영어는 보다 다양하게 주어의 형태, 시제, 단수, 복수에 따라도 변경되는 걸 볼 수가 있습니다. 

각 단어의 의미는 같지만 컴퓨터가 인식할 때는 제각기 다른 단어로 인식하 수 밖에 없기 때문에 stemming 과정을 거쳐 같은 의미의 단어를 동일화하는 과정이 필요합니다.

대표적인 라이브러리는 PorterStemmer를 사용하여 위에서 사용한 텍스트 예제를 가지고 수행을 해봅니다.

from nltk.tokenize import sent_tokenize as st, word_tokenize as wt
from nltk.stem import PorterStemmer

text="""The approach most frequently found in text mining systems involves breakingthe text into sentences and words, 
which is calledtokenization.
The main challengein identifying sentence boundaries in the English language is distinguishing betweena period 
that signals the end of a sentence and a period that is part of a previoustoken like Mr., Dr., and so on.[1]"""
tokens=text.split()
tokens3=st(text)
tokens2=wt(text)

for w in tokens2:
  print(porterStemmer().stem(w),end=' ')

원래의 텍스트와 토큰화와 어근동일화를 거친 텍스트의 차이에 대해 살펴봅시다.

The approach most frequently found in text mining systems involves breakingthe text into sentences and words, which is calledtokenization. The main challengein identifying sentence boundaries in the English language is distinguishing betweena period that signals the end of a sentence and a period that is part of a previoustoken like Mr., Dr., and so on.[1]

the approach most frequent found in text mine system involv breakingth text into sentenc and word , which is calledtoken . the main challengein identifi sentenc boundari in the english languag is distinguish betweena period that signal the end of a sentenc and a period that is part of a previoustoken like mr. , dr. , and so on . [ 1 ] 

 

이상 NLTK를 설치한 후 텍스트 데이터를 토큰화하고 어근 동일화하는 방법에 대해서 알아보았습니다. 감사합니다.

 

참고문헌

Feldman, R., & Sanger, J. (2007). The Text Mining Handbook : Advanced Approaches in Analyzing Unstructured Data. Cambridge University Press."

Rajesh Arumugam, & Rajalingappaa Shanmugamani. (2018). Hands-On Natural Language Processing with Python : A Practical Guide to Applying Deep Learning Architectures to Your NLP Applications. Packt Publishing.

 

 

728x90
반응형