fromtensorflow.keras.preprocessing.textimportTokenizertext="안녕 클레오파트라 세상에서 제일 가는 포테이토칩 안녕 안녕 클레오파트라 세상에서 제일 가는"# 토큰화 과정
token=Tokenizer()token.fit_on_texts([text])print(f"{token.word_index}\n")# 결과: {'안녕': 1, '클레오파트라': 2, '세상에서': 3, '제일': 4, '가는': 5, '포테이토칩': 6}
# 시퀀스로 변환
x=token.texts_to_sequences([text])print(text)# 원본 텍스트 출력
원-핫 인코딩 상세
1
2
3
4
5
6
7
8
fromtensorflow.keras.utilsimportto_categorical# 단어를 인덱스로 변환
sequences=[[1,2,3]]# 원-핫 인코딩
one_hot=to_categorical(sequences,num_classes=7)print(one_hot)