본문 바로가기

MLOps 개발자 양성과정/ml&dl

[Day-63] 자연어 처리(NLP)①

chap05. 자연어 처리 소개

 

자연어 처리란?

- Natural Language Processing

- 사람의 언어를 이해하기 위한 인공지능 기술

 

말뭉치?

아나그램? 단어나 문장의 철자 순서를 바꾸어 다른 단어난 문장을 만드는 것

 


1. 언어를 숫자로 인코딩하는 방법

- 토큰화 문장을 시퀀스로 바꾸기

- 이때

- OOV토큰 (out of vacabulary)사용하기 

 

 

불용어 제거와 텍스트 정제

 

1) 토큰화

 

- 텐서플로 케라스는 preprocessing 패키지에 Tokenizer 클래스를 통해 단어를 토큰으로 변환

 

# 라이브러리 임포트
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.text import Tokenizer

sentences = [
'Today is a sunny day',
'Today is a rainy day'
]

# Tokenizer 객체 생성하기
tokenizer = Tokenizer(num_words = 100) # 토큰화할 수 있는 단어 개수를 지정
tokenizer.fit_on_texts(sentences) # fit_on_texts를 통해 토큰화된 단어 인덱스를 만들기
word_index = tokenizer.word_index # word_index 속성- 단어와 인덱스의 키/값 쌍이 들어있는 딕셔너리 반환

print(word_index)
>>> {'today': 1, 'is': 2, 'a': 3, 'day': 4, 'sunny': 5, 'rainy': 6}

# index_word는 반대로 인덱스와 단어의 키/값 쌍이 들어있는 딕셔너리 반환
tokenizer.index_word
>>> {1: 'today', 2: 'is', 3: 'a', 4: 'day', 5: 'sunny', 6: 'rainy'}

 

2)  문장을 시퀀스로 바꾸기

 

문장을 숫자의 시퀀스로 인코딩하기

Tokenizer 클래스는 texts_to_sequences 메서드 제공

이 메서드에 문장의 리스트를 전달하면 시퀀스 리스트를 반환합니다

 

sequences = tokenizer.texts_to_sequences(sentences)

print(sequences)
>>> [[1, 2, 3, 4, 5], [1, 2, 3, 6, 5], [2, 7, 4, 1]]

chap06. 임베딩을 사용한 감성 프로그래밍