반응형
쉼표, 마침표 등의 구두점 제거 방법
Test = 'Hello Mr. Future. I am so happy to be learning AI now~'
import string
string.punctuation

위의 문장에서, 글자를 앞에서부터 하나씩 가져와서,
구두점인지 확인한 후, 구두점이 아닌 문자만 리스트로 저장
# 결과 예시) [ 'H', 'e', 'l', ... 'M', 'r', ' ','F' ... ]
Test_punc_removed = []
for char in Test :
if char not in string.punctuation :
Test_punc_removed.append( char )
컴프리핸션을 이용해 코드를 작성
[ char for char in Test if char not in string.punctuation ]
위의 리스트의 문자들을 하나의 문자열로 만들기
Test_punc_removed_join = ''.join(Test_punc_removed)
Test_punc_removed_join

불용어 제거
불용어란?
우리가 언어를 분석할 때, 의미가 있는 단어와, 의미가 없는 단어나 조사 등이 있다.
이렇게 의미가 없는 것들을 stopwords 라고 한다.
불용어 ( Stopwords )는 그때 그때, 사람이 판단하여,
불용어 리스트에, 원하는 단어를 추가하거나 제거하면서 사용하면 된다.
import nltk
nltk.download('stopwords')

from nltk.corpus import stopwords
my_stopwords = stopwords.words('english')
구두점 제거한 문자열을
불용어에 단어에 해당하지 않는 단어들만 모아서 리스트로 만들기
Test_punc_removed_join
Test_punc_removed_join_clean = []
for word in Test_punc_removed_join.split() :
if word.lower() not in my_stopwords :
Test_punc_removed_join_clean.append(word)
Test_punc_removed_join_clean

파이프 라이닝
( Pipe Lining )
구두점제거
불용어제거
이 두가지를 하나의 함수로 묶어서 사용한다. == Pipe Lining(파이프라이닝)
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

파이프라이닝 테스트
message_cleaning('Hello~! my name is, heheheh! nice to meet you!!!!@')

반응형
'Machine Learning' 카테고리의 다른 글
| Machine Learning - WordCloud Visualizing (0) | 2022.05.11 |
|---|---|
| Machine Learning - CountVectorizer (analyzer) (0) | 2022.05.11 |
| Machine Learning - GridSearchCV (0) | 2022.05.09 |
| Machine Learning - Word Cloud (Stopwords) (0) | 2022.05.09 |
| Machine Learning - Hierarchical Clustering (Dendrogram) (0) | 2022.05.09 |