728x90
정수 인코딩은 텍스트 데이터를 다루기 쉬운 형태인 숫자로 변환하는 것.
정수 인코딩은 대개 단어 또는 문장을 정수로 매핑하는 것으로 이루어진다. 예를 들어, "I love to eat hamburger"라는 문장을 다음과 같이 정수로 인코딩할 수 있다.
I: 1
love: 2
to: 3
eat: 4
hamburger: 5
이렇게 텍스트 데이터를 숫자 데이터로 변환하여 모델이 다루기 쉽게 만들어 주는 과정이다. 텍스트 분류, 문서 요약, 기계 번역 등에서 정수 인코딩이 자주 사용된다.
정수 인코딩을 할 때는 단어의 빈도수에 따라 인덱스를 할당하는 것이 일반적이다. 빈도수가 높은 단어일수록 낮은 인덱스를 할당하고, 빈도수가 낮은 단어일수록 높은 인덱스를 할당한다. 자주 등장하는 단어일수록 더 낮은 값을 가지게 되므로 모델이 단어의 중요도를 더 잘 파악할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# Integer Encoding
from nltk.tokenize import sent_tokenize # method to split a document or paragraph into sentences
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
raw_text = "Mj started this course last October. She got very nervous and passionate at the starting point of the course. She has yet to finish the course, but she has suffered from many happenings after the course started. Two months left. She will overcome all troubles. Every student will overcome their own troubles. Life is a journey to overcome all troubles."
# Tokenization
sentences = sent_tokenize(raw_text)
print(sentences)
"""
['Mj started this course last October.', 'She got very nervous and passionate at the starting point of the course.', 'She has yet to finish the course, but she has suffered from many happenings after the course started.', 'Two months left.', 'She will overcome all troubles.', 'Every student will overcome their own troubles.', 'Life is a journey to overcome all troubles.']
"""
# dictionary 사용하기
vocab = {}
preprocessed_sentences = []
stop_words = set(stopwords.words('english'))
for sentence in sentences:
tokenized_sentence = word_tokenize(sentence)
result = []
for word in tokenized_sentence:
word = word.lower() # 소문자로 변경하여 단어 개수 줄이기
if word not in stop_words: # 조건1 불용어 제거
if len(word) > 2: # 조건2 단어 길이가 2를 초과하는 경우에(=3)
result.append(word) # 단어를 추가한다
if word not in vocab: # 딕셔너리 단어 빈도수 기록
vocab[word] = 0
vocab[word] += 1
preprocessed_sentences.append(result)
print(preprocessed_sentences)
"""
[['started', 'course', 'last', 'october'], ['got', 'nervous', 'passionate', 'starting', 'point', 'course'], ['yet', 'finish', 'course', 'suffered', 'many', 'happenings', 'course', 'started'], ['two', 'months', 'left'], ['overcome', 'troubles'], ['every', 'student', 'overcome', 'troubles'], ['life', 'journey', 'overcome', 'troubles']]
"""
print('단어별 빈도수: ', vocab)
"""
단어별 빈도수: {'started': 2, 'course': 4, 'last': 1, 'october': 1, 'got': 1, 'nervous': 1, 'passionate': 1, 'starting': 1, 'point': 1, 'yet': 1, 'finish': 1, 'suffered': 1, 'many': 1, 'happenings': 1, 'two': 1, 'months': 1, 'left': 1, 'overcome': 3, 'troubles': 3, 'every': 1, 'student': 1, 'life': 1, 'journey': 1}
"""
print(vocab["course"]) # 4
# 고빈도순으로 정렬하기
"""
# lambda 함수란?
일명 무명함수(익명함수)로, 결과부분을 return 키워드 없이 자동으로 return해준다.
lambda 매개변수 : 표현식 <- 형태로 쓴다.
<def 함수를 lambda 함수로 바꿔보기>
def plus(x, y):
return x + y
print(plus(10, 20))
>>30
print((lambda x, y: x + y)(10, 20)) # 매개변수 x, y를 받아 x + y의 값으로 return한다.
"""
vocab_sorted = sorted(vocab.items(), key = lambda x:x[1], reverse = True) # x를 받아 리스트 2번째 요소를 key로 정렬한다.
print(vocab_sorted)
"""
[('course', 4), ('overcome', 3), ('troubles', 3), ('started', 2), ('last', 1), ('october', 1), ('got', 1), ('nervous', 1), ('passionate', 1), ('starting', 1), ('point', 1), ('yet', 1), ('finish', 1), ('suffered', 1), ('many', 1), ('happenings', 1), ('two', 1), ('months', 1), ('left', 1), ('every', 1), ('student', 1), ('life', 1), ('journey', 1)]
"""
# 고빈도순으로 인덱스(1, 2, 3...)를 부여한다.
word_to_index = {}
i = 0
for (word, frequency) in vocab_sorted:
if frequency > 1: # 빈도수 1 이상인 것만
i = i + 1
word_to_index[word] = i
print(word_to_index)
|
cs |
728x90
'자연어 처리(NLP) 공부' 카테고리의 다른 글
자연어 처리(NLP) - 어간 추출과 표제어 추출 모두 수행하기 (0) | 2023.03.16 |
---|---|
자연어 처리(NLP) - 어간 추출(Stemming), 표제어 추출(Lemmatization) (2) | 2023.03.16 |
자연어 처리(NLP) - 불용어(stopwords) 제거 (0) | 2023.03.15 |
자연어 처리(NLP) - 텍스트 데이터 전처리(preprocessing) 과정 (0) | 2023.03.13 |
자연어 처리(NLP) - 토큰화(Tokenization) (2) | 2023.03.11 |