Showing
2 changed files
with
131 additions
and
0 deletions
server/service/calcDistance.js
0 → 100644
File mode changed
server/service/standardization.js
0 → 100644
1 | +/* eslint-disable no-use-before-define */ | ||
2 | +/* eslint-disable no-restricted-syntax */ | ||
3 | +const Hangul = require('hangul-js'); | ||
4 | + | ||
5 | +const standardize = (word) => { | ||
6 | + let result = ''; | ||
7 | + let newLetter = ''; | ||
8 | + const disassembledWord = Hangul.disassemble(word); | ||
9 | + for (const [index, letter] of disassembledWord.entries()) { | ||
10 | + if (isKorean(letter)) { | ||
11 | + newLetter = Hangul.isConsonant(letter) | ||
12 | + ? standardizeConsonent(letter) | ||
13 | + : standardizeVowel(letter); | ||
14 | + } else { | ||
15 | + const preLetter = index === 1 ? '' : disassembledWord[index - 1]; | ||
16 | + const nextLetter = index === disassembledWord.length - 1 ? '' : disassembledWord[index + 1]; | ||
17 | + newLetter = isVowelSpecial(preLetter, letter, nextLetter) | ||
18 | + ? standardizeSpecialVowel(letter) | ||
19 | + : standardizeSpecialConsonent(letter); | ||
20 | + } | ||
21 | + result += newLetter; | ||
22 | + } | ||
23 | + return result; | ||
24 | +}; | ||
25 | + | ||
26 | +const isKorean = (word) => { | ||
27 | + const numCheck = /[0-9]/; // 숫자 | ||
28 | + | ||
29 | + const engCheck = /[a-zA-Z]/; // 문자 | ||
30 | + | ||
31 | + const specialCheck = /[~!@#$%^&*()_+|<>?:{}]/; // 특수문자 | ||
32 | + | ||
33 | + if (!numCheck.test(word) && !engCheck.test(word) && !specialCheck.test(word)) { | ||
34 | + return true; | ||
35 | + } | ||
36 | + return false; | ||
37 | +}; | ||
38 | + | ||
39 | +const standardizeConsonent = (letter) => { | ||
40 | + switch (letter) { | ||
41 | + case ('ㄱ', 'ㄲ', 'ㅋ'): | ||
42 | + return 'ㄱ'; | ||
43 | + case ('ㄷ', 'ㄸ', 'ㅌ'): | ||
44 | + return 'ㄷ'; | ||
45 | + case ('ㅂ', 'ㅃ', 'ㅍ'): | ||
46 | + return 'ㅂ'; | ||
47 | + case ('ㅅ', 'ㅆ'): | ||
48 | + return 'ㅅ'; | ||
49 | + case ('ㅈ', 'ㅉ', 'ㅊ'): | ||
50 | + return 'ㅈ'; | ||
51 | + default: | ||
52 | + return letter; | ||
53 | + } | ||
54 | +}; | ||
55 | + | ||
56 | +const standardizeVowel = (letter) => { | ||
57 | + switch (letter) { | ||
58 | + case ('ㅏ', 'ㅑ'): | ||
59 | + return 'ㅏ'; | ||
60 | + case ('ㅗ', 'ㅛ'): | ||
61 | + return 'ㅗ'; | ||
62 | + case ('ㅐ', 'ㅒ', 'ㅔ', 'ㅖ', 'ㅙ', 'ㅚ', 'ㅝ', 'ㅞ'): | ||
63 | + return 'ㅐ'; | ||
64 | + case ('ㅜ', 'ㅠ'): | ||
65 | + return 'ㅜ'; | ||
66 | + case ('ㅓ', 'ㅕ'): | ||
67 | + return 'ㅓ'; | ||
68 | + case ('ㅟ', 'ㅢ', 'ㅣ'): | ||
69 | + return 'ㅣ'; | ||
70 | + default: | ||
71 | + return letter; | ||
72 | + } | ||
73 | +}; | ||
74 | + | ||
75 | +const isVowelSpecial = (preLetter, letter, nextLetter) => { | ||
76 | + if (preLetter !== '') { | ||
77 | + if (nextLetter === '') { | ||
78 | + if (isKorean(preLetter)) { | ||
79 | + if (Hangul.isConsonant(preLetter)) { | ||
80 | + return true; | ||
81 | + } | ||
82 | + } | ||
83 | + } else if (isKorean(preLetter) && isKorean(nextLetter)) { | ||
84 | + if (Hangul.isConsonant(preLetter) && Hangul.isConsonant(nextLetter)) { | ||
85 | + return true; | ||
86 | + } | ||
87 | + } | ||
88 | + } | ||
89 | + | ||
90 | + return false; | ||
91 | +}; | ||
92 | + | ||
93 | +const standardizeSpecialConsonent = (letter) => { | ||
94 | + switch (letter) { | ||
95 | + case ('g', 'ㅑ'): | ||
96 | + return 'ㅏ'; | ||
97 | + case ('ㅗ', 'ㅛ'): | ||
98 | + return 'ㅗ'; | ||
99 | + case ('ㅐ', 'ㅒ', 'ㅔ', 'ㅖ', 'ㅙ', 'ㅚ', 'ㅝ', 'ㅞ'): | ||
100 | + return 'ㅐ'; | ||
101 | + case ('ㅜ', 'ㅠ'): | ||
102 | + return 'ㅜ'; | ||
103 | + case ('ㅓ', 'ㅕ'): | ||
104 | + return 'ㅓ'; | ||
105 | + case ('ㅟ', 'ㅢ', 'ㅣ'): | ||
106 | + return 'ㅣ'; | ||
107 | + default: | ||
108 | + return letter; | ||
109 | + } | ||
110 | +}; | ||
111 | + | ||
112 | +const standardizeSpecialVowel = (letter) => { | ||
113 | + switch (letter) { | ||
114 | + case ('g', 'ㅑ'): | ||
115 | + return 'ㅏ'; | ||
116 | + case ('ㅗ', 'ㅛ'): | ||
117 | + return 'ㅗ'; | ||
118 | + case ('ㅐ', 'ㅒ', 'ㅔ', 'ㅖ', 'ㅙ', 'ㅚ', 'ㅝ', 'ㅞ'): | ||
119 | + return 'ㅐ'; | ||
120 | + case ('ㅜ', 'ㅠ'): | ||
121 | + return 'ㅜ'; | ||
122 | + case ('ㅓ', 'ㅕ'): | ||
123 | + return 'ㅓ'; | ||
124 | + case ('ㅟ', 'ㅢ', 'ㅣ'): | ||
125 | + return 'ㅣ'; | ||
126 | + default: | ||
127 | + return letter; | ||
128 | + } | ||
129 | +}; | ||
130 | + | ||
131 | +module.exports = { standardize }; |
-
Please register or login to post a comment