Repository navigation
Expand file tree
/
Copy pathpreprocessing.js
More file actions
82 lines (67 loc) · 2.17 KB
/
Copy pathpreprocessing.js
File metadata and controls
82 lines (67 loc) · 2.17 KB
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
76
77
78
79
80
81
82
/** 문장 문자열 배열 -> 토큰 배열 */
function tokenize(sentences) {
return sentences.map((sentence) => sentence.trim().split(/\s+/));
}
/**
* 단어 사전(word -> index) 생성
* - 코퍼스에서 처음 등장한 순서대로 인덱스 부여
*/
function buildWordToIndex(tokenizedText) {
const wordToIndex = {};
let currentIndex = 0;
for (const sentence of tokenizedText) {
for (const word of sentence) {
if (wordToIndex[word] !== undefined) continue;
wordToIndex[word] = currentIndex;
currentIndex += 1;
}
}
return wordToIndex;
}
/** 토큰 배열 -> 정수 인덱스 배열 */
function encode(tokenizedText, wordToIndex) {
return tokenizedText.map((sentence) => sentence.map((word) => wordToIndex[word]));
}
/** 정수 인덱스 배열 -> 토큰 배열 */
function decode(indices, wordToIndex) {
const indexToWord = Object.entries(wordToIndex)
.sort(([, indexA], [, indexB]) => indexA - indexB)
.map(([word]) => word);
return indices.map((index) => indexToWord[index]);
}
/** [이론용] 원-핫 인코딩 */
function oneHotEncode(encodedText, vocabSize) {
return encodedText.map((sentence) =>
sentence.map((wordIndex) => {
const oneHotVector = new Array(vocabSize).fill(0);
oneHotVector[wordIndex] = 1;
return oneHotVector;
}),
);
}
/** CBOW/Skip-gram용 (input, target) 쌍 생성 */
function makeTrainingData(encodedText, windowSize) {
const trainingData = [];
for (let sentenceIndex = 0; sentenceIndex < encodedText.length; sentenceIndex += 1) {
const sentence = encodedText[sentenceIndex];
for (let centerIndex = 0; centerIndex < sentence.length; centerIndex += 1) {
const input = sentence[centerIndex];
for (let offset = -windowSize; offset <= windowSize; offset += 1) {
if (offset === 0) continue;
const targetIndex = centerIndex + offset;
if (targetIndex < 0 || targetIndex >= sentence.length) continue;
const target = sentence[targetIndex];
trainingData.push({ input, target });
}
}
}
return trainingData;
}
module.exports = {
tokenize,
buildWordToIndex,
encode,
decode,
oneHotEncode,
makeTrainingData,
};