문자열 데이터를 처리하기 위한
구두점 제거 + 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 |
'A.I > Machine Learning' 카테고리의 다른 글
데이터프레임의 날짜문자열 칼럼을 datetime64로 변경하는법 (0) | 2022.05.11 |
---|---|
판다스(Pandas) read_csv 함수의 error_bad_lines=False 파라미터 사용법 (0) | 2022.05.11 |
Prophet(프로펫) 라이브러리 사용법 (0) | 2022.05.11 |
wordCloud에서 배경 모양을 바꾸는 방법 (0) | 2022.05.10 |
카테고리컬 데이터(Categorical Data) 확인하는 방법 (0) | 2022.05.04 |
댓글