예제1) 하기 sentence에서 불용어(stop_words)를 제거하기

조건)

- 불용어 리스트 : on, in the

- 문장 : 'singer on the stage

답)

['singer', 'stage']

 

코드)

stop_words = 'on in the'

stop_words = stop_words.split()  #공백을 기준으로 나누기

sentence = 'singer on the stage'
sentence = sentence.split()

nouns = []

# for i in range(0,len(sentence)):
#     if sentence[i] not in stop_words:
#         nouns.append(sentence[i])

for noun in sentence:
    if noun not in stop_words:
        nouns.append(noun)

nouns

 

 

예제2) nltk 패키지의 불용어 리스트를 사용해보기

조건)  

 - 예제 문장 : If you do not walk today, you will have to run tomorrow

 - 사용 함수:  nltk.corpus 모듈의 stopwords, nltk.tokenize 모듈의 word_tokenize

결과)

['If', 'walk', 'today', ',', 'run', 'tomorrow']

코드)

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

stop_words = stopwords.words('english')

# 토큰화

s = 'If you do not walk today, you will have to run tomorrow'

words = word_tokenize(s)

nouns = []

for noun in words:
    if noun not in stop_words:
        nouns.append(noun)

nouns

 

 

예제3) 하기 한국어 문장에서 불용어 제거하기

조건)

 - 문장: 걷는 놈 위에 뛰는 놈 있고, 뛰는 놈 위에 나는 놈 있다. 나는 놈 위에는 즐기는 놈이 있다.

- 사용함수 : kiwipiepy.utils 모듈의 Stopwords

결과)

['걷',
 '놈',
 '위',
 '뛰',
 '놈',
 '뛰',
 '놈',
 '위',
 '날',
 '놈',
 '있',
 '나',
 '놈',
 '위',
 '즐기',
 '놈']

코드)

from kiwipiepy.utils import Stopwords
from kiwipiepy import Kiwi

stopwords = Stopwords()
kiwi = Kiwi()

text = '걷는 놈 위에 뛰는 놈 있고, 뛰는 놈 위에 나는 놈 있다. 나는 놈 위에는 즐기는 놈이 있다.'

token_text = kiwi.tokenize(text, stopwords=stopwords)

token_kr = []
for t in token_text:
    token_kr.append(t.form)

token_kr

 

+ Recent posts