또르르's 개발 Story

[16-3] Spacy를 이용한 영어 전처리 본문

부스트캠프 AI 테크 U stage/실습

[16-3] Spacy를 이용한 영어 전처리

또르르21 2021. 2. 16. 01:47

1️⃣ 설정

 

spacy 모듈에서 'en'을 load 하면 tokenization를 쉽게 사용할 수 있습니다.

import spacy

spacy_en = spacy.load('en')
nlp = spacy.load('en_core_web_sm')      # english의 tokenization이 가능

 

 

2️⃣ Tokenezation

 

위에서 선언한 nlp를 사용하면 쉽게 token을 분리할 수 있습니다.

doc = nlp('This assignment is about Natural Language Processing.' 'In this assignment, we will do preprocessing')

>>> print ([token.text for token in doc])


['This', 'assignment', 'is', 'about', 'Natural', 'Language', 'Processing', '.', 'In', 'this', 'assignment', ',', 'we', 'will', 'do', 'preprocessing']

 

3️⃣ 불용어 (Stopword)

 

불용어(Stopword)는 자주 등장하지만 분석에 있어 큰 도움이 되지 않는 단어 (I, my, me, over, 조사, 접미사 같은 단어)를 뜻합니다.

spacy_stopwords = spacy.lang.en.stop_words.STOP_WORDS

for stop_word in list(spacy_stopwords)[:30]:

    print(stop_word)
  
  
everything
after
nobody
always
becomes
nothing
eleven
under
‘ll
hers
anything
would
herself
until
meanwhile
his
six
how
therefore
upon
wherein
such
everyone
she
too
well
'll
forty
beside
four

 

4️⃣ Lemmatization

 

표제어(Lemma)는 한글로는 '표제어' 또는 '기본 사전형 단어' 정도의 의미를 갖습니다. 표제어 추출은 단어들로부터 표제어를 찾아가는 과정입니다. 표제어 추출은 단어들이 다른 형태를 가지더라도, 그 뿌리 단어를 찾아가서 단어의 개수를 줄일 수 있는지 판단합니다.

예를 들어서 am, are, is는 서로 다른 스펠링이지만 그 뿌리 단어는 be라고 볼 수 있습니다. 이때, 이 단어들의 표제어는 be라고 합니다.

(출처:https://wikidocs.net/21707)

 

for token in text[:]:

    print (token, "-", token.lemma_)
  
  
development - development
began - begin
Iron - Iron
Man - Man
in - in
May - May

 

 

5️⃣ 그외 token class의 attributes

 

print("token \t is_punct \t is_space \t shape_ \t is_stop")

print("="*70)

for token in text[21:31]:

  print(token,"\t", token.is_punct, "\t\t",token.is_space,"\t\t", token.shape_, "\t\t",token.is_stop)
token 	 is_punct 	 is_space 	 shape_ 	 is_stop
======================================================================
of 	 False 		 False 		 xx 		 True
the 	 False 		 False 		 xxx 		 True
film 	 False 		 False 		 xxxx 		 False
Iron 	 False 		 False 		 Xxxx 		 False
Man 	 False 		 False 		 Xxx 		 False
in 	 False 		 False 		 xx 		 True
May 	 False 		 False 		 Xxx 		 True
2008 	 False 		 False 		 dddd 		 False
, 	 True 		 False 		 , 		 False
Marvel 	 False 		 False 		 Xxxxx 		 False
Comments