standardization.js
3.77 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable no-use-before-define */
/* eslint-disable no-restricted-syntax */
const { config } = require('dotenv');
const Hangul = require('hangul-js');
const standardize = (word) => {
let result = '';
let newLetter = '';
const disassembledWord = Hangul.disassemble(word);
for (const [index, letter] of disassembledWord.entries()) {
if (isKorean(letter)) {
newLetter = Hangul.isConsonant(letter)
? standardizeConsonent(letter)
: standardizeVowel(letter);
} else {
const preLetter = index === 0 ? '' : disassembledWord[index - 1];
const nextLetter = index === disassembledWord.length - 1 ? '' : disassembledWord[index + 1];
newLetter = isVowelSpecial(preLetter, letter, nextLetter)
? standardizeSpecialVowel(letter)
: standardizeSpecialConsonent(letter);
}
result += newLetter;
}
return result;
};
const isKorean = (word) => {
const numCheck = /[0-9]/; // 숫자
const engCheck = /[a-zA-Z]/; // 문자
const specialCheck = /[~!@#$%^&*()_+|<>?:{}]/; // 특수문자
if (!numCheck.test(word) && !engCheck.test(word) && !specialCheck.test(word)) {
return true;
}
return false;
};
const standardizeConsonent = (letter) => {
switch (letter) {
case ('ㄱ', 'ㄲ', 'ㅋ'):
return 'ㄱ';
case ('ㄷ', 'ㄸ', 'ㅌ'):
return 'ㄷ';
case ('ㅂ', 'ㅃ', 'ㅍ'):
return 'ㅂ';
case ('ㅅ', 'ㅆ'):
return 'ㅅ';
case ('ㅈ', 'ㅉ', 'ㅊ'):
return 'ㅈ';
default:
return letter;
}
};
const standardizeVowel = (letter) => {
switch (letter) {
case ('ㅏ', 'ㅑ'):
return 'ㅏ';
case ('ㅗ', 'ㅛ'):
return 'ㅗ';
case ('ㅐ', 'ㅒ', 'ㅔ', 'ㅖ', 'ㅙ', 'ㅚ', 'ㅝ', 'ㅞ'):
return 'ㅐ';
case ('ㅜ', 'ㅠ'):
return 'ㅜ';
case ('ㅓ', 'ㅕ'):
return 'ㅓ';
case ('ㅟ', 'ㅢ', 'ㅣ'):
return 'ㅣ';
default:
return letter;
}
};
const isVowelSpecial = (preLetter, letter, nextLetter) => {
if (preLetter !== '') {
if (nextLetter === '') {
if (isKorean(preLetter)) {
if (Hangul.isConsonant(preLetter)) {
return true;
}
}
} else if (isKorean(preLetter) && isKorean(nextLetter)) {
if (Hangul.isConsonant(preLetter) && Hangul.isConsonant(nextLetter)) {
return true;
}
}
}
return false;
};
const standardizeSpecialConsonent = (letter) => {
switch (letter) {
case ('g', 'k', 'G', 'K'):
return 'ㄱ';
case ('b', 'v', 'B', 'V'):
return 'ㅂ';
case ('P', 'p', 'F', 'f'):
return 'ㅍ';
case ('n', 'L', 'N'):
return 'ㄴ';
case ('s', 'A', 'S', 'C', 'c'):
return 'ㅅ';
case ('d', 't', 'E'):
return 'ㄷ';
case ('o', 'O'):
return 'ㅇ';
case ('r', 'R'):
return 'ㄹ';
case ('j', 'z', 'J', 'Z'):
return 'ㅈ';
case ('m', '口', 'M'):
return 'ㅁ';
case ('h', 'H'):
return 'ㅎ';
default:
return letter;
}
};
const standardizeSpecialVowel = (letter) => {
let result = '';
switch (letter) {
case ('a', 'A'):
result = 'ㅏ';
break;
case ('u', 'U'):
result = 'ㅓ';
break;
case ('@', 'H'):
result = 'ㅐ';
break;
case ('l', 'i', 'I', '!', 1):
result = 'ㅣ';
break;
case '1':
result = 'ㅣ';
break;
default:
result = letter;
break;
}
return result;
};
const removeSpecial = (word) => {
const numCheck = /[0-9]/g; // 숫자
const engCheck = /[a-zA-Z]/g; // 문자
const specialCheck = /[~!@#$%^&*()_+|<>?:{}]/g; // 특수문자
let result = word.replace(numCheck, '');
result = result.replace(engCheck, '');
result = result.replace(specialCheck, '');
return result;
};
module.exports = { standardize, removeSpecial };