본문 바로가기
A.I/Machine Learning

문자열 데이터를 처리하기 위한 구두점 제거 + Stopwors(불용어) 사용하는 코드

by healingmau 2022. 5. 10.

 

문자열 데이터를 처리하기 위한

구두점 제거 + Stopwors(불용어) 사용하는 코드

 

구두점 제거와 불용어!!

이 두가지를 하나의 함수로

묶어서 사용하겠습니다.

(용어는~ 파이프라이닝 한다)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import string
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
my_stopwords = stopwords.words('english')
 
def message_cleaning(sentence) :
  # 1. 구두점 제거
  Test_punc_removed = [char for char in sentence if char not in string.punctuation ]
 
  # 2. 각 글자들을 하나의 문자열로 합친다.
  Test_punc_removed_join = ''.join(Test_punc_removed)
 
  # 3. 문자열에 불용어가 포함되어있는지 확인해서, 불용어 제거한다.
  Test_punc_removed_join_clean = [word for word in Test_punc_removed_join.split() if word.lower() not in my_stopwords ]
 
  # 4. 결과로 남은 단어들만 리턴한다.
  return Test_punc_removed_join_clean
Colored by Color Scripter
cs

 

이제 테스트를 해본다. ↓↓

1
message_cleaning('Hello~~! my name is, heheheh! nice to meet you!!!@')
cs

댓글