■ 정제(cleaning), 정규화(normalizing)
* 정제: 갖고있는 코퍼스로부터 노이즈 데이터를 제거한다
* 정규화 : 표현방법이 다른 단어들을 통합시켜서 같은 단어로 만들어준다.
영어로 보면
USA , US <- 동일한 의미를 가지므로 '하나의 단어'로 보는것이 정규화 라고 합니다~
어간추출하거나 표제어 추출할때는 대소문자를 통합해주어야합니다!
대소문자 통합
Automobile, automobile <- 통합할 필요가 있으면 통합
무조건 통합도 안됨 -> 'US', 'us'
영어권같은경우는 단어의 개수를 확 줄일 수 있습니다. 대표적인 정규화 방법중 하나입니다.
대문자는 문장의 앞이나 특정한 상황에서 쓰기때문에 대체로 소문자로 통합합니다!
불용어(stop word)제거
- 불필요한 단어를 제거합니다.
영향없고 노이즈를 줄주는것! 그런것들은 예를들어서 등장빈도가 적거나 길이가 짧은 단어를 일컫습니다.
이런것들을 잘 제거해줘야합니다!
예를들어서 영어의 경우는 2~3글자크기인 a, an, in, by, on.. 크기의 단어만 제거해도 노이즈를 많이 제거할 수 있어요.
한 번 해보자면
text = "I was wondering if anyone out there could enlighten me on this car."
shortword = re.compile(r'\w*\b\w{1,2}\b')
shortword.sub('',text)
결과
was wondering anyone out there could enlighten this car.
한글자, 두글자정도는 빠지는 결과를 볼 수 있습니다.
■ 어간 추출(Stemming) and 표제어 추출(Lemmatization)
표제어(Lemma) : '기본 사전형 단어'
표제어 추출은 단어들이 다른 형태를 가지더라도, 그 뿌리 단어를 찾아가서 단어의 개수를 줄일 수 있는지 판단
ex) am, are, is는 서로 다른 스펠링이지만 그 뿌리 단어는 be
형태소: '의미를 가진 가장 작은 단위'
형태소의 종류: 어간(stem)과 접사(affix)
1) 어간(stem)
: 단어의 의미를 담고 있는 단어의 핵심 부분.
2) 접사(affix)
: 단어에 추가적인 의미를 주는 부분.
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
words = ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
print(words)
['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
nltk.download('wordnet')
print(words)
print([lemmatizer.lemmatize(word) for word in words])
결과
['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting']
['policy', 'doing', 'organization', 'have', 'going', 'love', 'life', 'fly', 'dy', 'watched', 'ha', 'starting']
생각보다 정확하거나 그러진 않는것같습니다.
그래서 본래 단어의 품사 정보를 제공해주어봤습니다.
lemmatizer.lemmatize('dies')
#dy
lemmatizer.lemmatize('dies', 'v')
#die
품사정보를 제공해주니 (dies를 v(동사)라고) 제대로 잘 뽑는 결과를 받을 수 있습니다.
어간추출해보겠슴다.
어간추출은 stemming이라고도 합니다.
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
sentence = "This was not the map we found in Billy Bones's chest, but an accurate copy, complete in all things--names and heights and soundings--with the single exception of the red crosses and the written notes."
tokenized_sentence = word_tokenize(sentence)
print(tokenized_sentence)
결과
['This', 'was', 'not', 'the', 'map', 'we', 'found', 'in', 'Billy', 'Bones', "'s", 'chest', ',', 'but', 'an', 'accurate', 'copy', ',', 'complete', 'in', 'all', 'things', '--', 'names', 'and', 'heights', 'and', 'soundings', '--', 'with', 'the', 'single', 'exception', 'of', 'the', 'red', 'crosses', 'and', 'the', 'written', 'notes', '.']
어간추출을 하니까 이렇게 보이는데 이런것들은 또 사전에 당연히 없죠.,
영어는 이렇고 한국어는어떨까요?
한국어에서 어간 추출
5언 9품사 구조
체언 - 명사, 대명사, 수사
수식언 - 관형사, 부사
관계언 - 조사
독립언 - 감탄사
용어 - 동사, 형용사 <= 어간(stem) 과 어미(ending)의 결합
■ 불용어(stopword)제거
NLTK에서 불용어 제거
from nltk.corpus import stopwords
# 불용어사전 다운
nltk.download('stopwords')
stop_word_list = stopwords.words('english')
print(len(stop_word_list))
print(stop_word_list)
179개
영어 예제로 보자면
example = "Family is not an important thing. It's everything."
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example)
result = []
for word in word_tokens:
if word not in stop_words:
result.append(word)
print('불용어 제거 전:', word_tokens)
print('불용어 제거 후:', result)
결과
불용어 제거 전: ['Family', 'is', 'not', 'an', 'important', 'thing', '.', 'It', "'s", 'everything', '.']
불용어 제거 후: ['Family', 'important', 'thing', '.', 'It', "'s", 'everything', '.']
로 나오게 됩니다.
한국어에서 불용어를 제거해보자면?
okt = Okt()
example = "고기를 아무렇게나 구우려고 하면 안 돼. 고기라고 다 같은 게 아니거든. 예컨대 삼겹살을 구울 때는 중요한 게 있지."
stop_words = "를 아무렇게나 구 우려 고 안 돼 같은 게 구울 때 는"
#set : 중복된거 날라가는 파이썬 함수 ㅠㅠ
stop_words = set(stop_words.split(' '))
word_tokens = okt.morphs(example)
result = [word for word in word_tokens if not word in stop_words]
print('불용어 제거 전:', word_tokens)
print('불용어 제거 후:', result)
불용어 제거 전: ['고기', '를', '아무렇게나', '구', '우려', '고', '하면', '안', '돼', '.', '고기', '라고', '다', '같은', '게', '아니거든', '.', '예컨대', '삼겹살', '을', '구울', '때', '는', '중요한', '게', '있지', '.']
불용어 제거 후: ['고기', '하면', '.', '고기', '라고', '다', '아니거든', '.', '예컨대', '삼겹살', '을', '중요한', '있지', '.']
불용어리스트 사이트!
https://www.ranks.nl/stopwords/korean
Korean Stopwords
www.ranks.nl
'AI > 자연어처리' 카테고리의 다른 글
[AI활용 자연어처리 챗봇프로젝트] Padding, 원핫인코딩 (5) | 2024.12.09 |
---|---|
[AI활용 자연어처리 챗봇프로젝트] OOV란? oov인덱스 번호는? (0) | 2024.12.06 |
[AI활용 자연어처리 챗봇프로젝트] 케라스를 이용한 전처리 (1) | 2024.12.05 |
[AI활용 자연어처리 챗봇프로젝트] 토큰화 및 문장처리 (1) | 2024.12.03 |
[AI활용 자연어처리 챗봇프로젝트] 자연어처리(NLP)란? (3) | 2024.12.02 |