bareun-mal 0.0.0-stage → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -3
- package/index.d.ts +45 -0
- package/package.json +23 -5
- package/src/engine.js +354 -0
- package/src/generated/expanded-dict.json +680848 -0
- package/src/index.js +93 -0
- package/src/normalizer.js +370 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { buildIndex, evaluateText } = require('./engine');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_CATEGORIES = ['profanity', 'slang', 'sexual', 'hate', 'english'];
|
|
6
|
+
|
|
7
|
+
class bareunMal {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.threshold = typeof options.threshold === 'number' ? options.threshold : 0.7;
|
|
10
|
+
const categoryOption = options.enabledCategories || options.categories || DEFAULT_CATEGORIES;
|
|
11
|
+
this.enabledCategories = new Set(categoryOption);
|
|
12
|
+
|
|
13
|
+
const dictPath = options.dictionaryPath || path.join(__dirname, 'generated', 'expanded-dict.json');
|
|
14
|
+
const loaded = this._loadDictionaryFile(dictPath);
|
|
15
|
+
|
|
16
|
+
this.dictionary = {
|
|
17
|
+
profanity: [...(loaded.dictionary.profanity || [])],
|
|
18
|
+
slang: [...(loaded.dictionary.slang || [])],
|
|
19
|
+
sexual: [...(loaded.dictionary.sexual || [])],
|
|
20
|
+
hate: [...(loaded.dictionary.hate || [])],
|
|
21
|
+
english: [...(loaded.dictionary.english || [])]
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
this.exceptions = new Set(loaded.exceptions || []);
|
|
25
|
+
|
|
26
|
+
if (options.customWords) {
|
|
27
|
+
for (const [cat, words] of Object.entries(options.customWords)) {
|
|
28
|
+
if (!this.dictionary[cat]) this.dictionary[cat] = [];
|
|
29
|
+
this.dictionary[cat].push(...words);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (Array.isArray(options.exceptions)) {
|
|
34
|
+
for (const ex of options.exceptions) {
|
|
35
|
+
this.exceptions.add(ex);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this._rebuildIndex();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_loadDictionaryFile(dictPath) {
|
|
43
|
+
if (!fs.existsSync(dictPath)) {
|
|
44
|
+
throw new Error(`Dictionary file not found at ${dictPath}. Run "npm run build:dict" first.`);
|
|
45
|
+
}
|
|
46
|
+
const raw = fs.readFileSync(dictPath, 'utf8');
|
|
47
|
+
return JSON.parse(raw);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_rebuildIndex() {
|
|
51
|
+
this.index = buildIndex(this.dictionary, this.exceptions);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
addWords(category, words = []) {
|
|
55
|
+
if (!this.dictionary[category]) {
|
|
56
|
+
this.dictionary[category] = [];
|
|
57
|
+
}
|
|
58
|
+
this.dictionary[category].push(...words);
|
|
59
|
+
this._rebuildIndex();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
addExceptions(words = []) {
|
|
63
|
+
for (const w of words) {
|
|
64
|
+
this.exceptions.add(w);
|
|
65
|
+
}
|
|
66
|
+
this._rebuildIndex();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
checkSync(text) {
|
|
70
|
+
const evalResult = evaluateText(text, this.index, this.enabledCategories);
|
|
71
|
+
const isProfane = evalResult.score >= this.threshold;
|
|
72
|
+
let action = 'allow';
|
|
73
|
+
if (evalResult.score >= this.threshold) {
|
|
74
|
+
action = 'block';
|
|
75
|
+
} else if (evalResult.score >= 0.45) {
|
|
76
|
+
action = 'review';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
isProfane,
|
|
81
|
+
action,
|
|
82
|
+
score: evalResult.score,
|
|
83
|
+
categories: evalResult.categories,
|
|
84
|
+
hits: evalResult.hits
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async check(text) {
|
|
89
|
+
return this.checkSync(text);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = bareunMal;
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
const CHOSUNG = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'];
|
|
2
|
+
const JUNGSUNG = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ'];
|
|
3
|
+
const JONGSUNG = ['', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'];
|
|
4
|
+
|
|
5
|
+
const COMPLEX_JONG = {
|
|
6
|
+
'ㄳ': 'ㄱㅅ', 'ㄵ': 'ㄴㅈ', 'ㄶ': 'ㄴㅎ', 'ㄺ': 'ㄹㄱ', 'ㄻ': 'ㄹㅁ',
|
|
7
|
+
'ㄼ': 'ㄹㅂ', 'ㄽ': 'ㄹㅅ', 'ㄾ': 'ㄹㅌ', 'ㄿ': 'ㄹㅍ', 'ㅀ': 'ㄹㅎ', 'ㅄ': 'ㅂㅅ'
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const CHOSUNG_SET = new Set(CHOSUNG);
|
|
11
|
+
const JUNGSUNG_SET = new Set(JUNGSUNG);
|
|
12
|
+
|
|
13
|
+
const CHOSEONG_TO_COMPAT = {
|
|
14
|
+
'\u1100': 'ㄱ', '\u1101': 'ㄲ', '\u1102': 'ㄴ', '\u1103': 'ㄷ', '\u1104': 'ㄸ',
|
|
15
|
+
'\u1105': 'ㄹ', '\u1106': 'ㅁ', '\u1107': 'ㅂ', '\u1108': 'ㅃ', '\u1109': 'ㅅ',
|
|
16
|
+
'\u110A': 'ㅆ', '\u110B': 'ㅇ', '\u110C': 'ㅈ', '\u110D': 'ㅉ', '\u110E': 'ㅊ',
|
|
17
|
+
'\u110F': 'ㅋ', '\u1110': 'ㅌ', '\u1111': 'ㅍ', '\u1112': 'ㅎ'
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const JUNGSEONG_TO_COMPAT = {
|
|
21
|
+
'\u1161': 'ㅏ', '\u1162': 'ㅐ', '\u1163': 'ㅑ', '\u1164': 'ㅒ', '\u1165': 'ㅓ',
|
|
22
|
+
'\u1166': 'ㅔ', '\u1167': 'ㅕ', '\u1168': 'ㅖ', '\u1169': 'ㅗ', '\u116A': 'ㅘ',
|
|
23
|
+
'\u116B': 'ㅙ', '\u116C': 'ㅚ', '\u116D': 'ㅛ', '\u116E': 'ㅜ', '\u116F': 'ㅝ',
|
|
24
|
+
'\u1170': 'ㅞ', '\u1171': 'ㅟ', '\u1172': 'ㅠ', '\u1173': 'ㅡ', '\u1174': 'ㅢ',
|
|
25
|
+
'\u1175': 'ㅣ'
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const JONGSEONG_TO_COMPAT = {
|
|
29
|
+
'\u11A8': 'ㄱ', '\u11A9': 'ㄲ', '\u11AA': 'ㄱㅅ', '\u11AB': 'ㄴ', '\u11AC': 'ㄴㅈ',
|
|
30
|
+
'\u11AD': 'ㄴㅎ', '\u11AE': 'ㄷ', '\u11AF': 'ㄹ', '\u11B0': 'ㄹㄱ', '\u11B1': 'ㄹㅁ',
|
|
31
|
+
'\u11B2': 'ㄹㅂ', '\u11B3': 'ㄹㅅ', '\u11B4': 'ㄹㅌ', '\u11B5': 'ㄹㅍ', '\u11B6': 'ㄹㅎ',
|
|
32
|
+
'\u11B7': 'ㅁ', '\u11B8': 'ㅂ', '\u11B9': 'ㅂㅅ', '\u11BA': 'ㅅ', '\u11BB': 'ㅆ',
|
|
33
|
+
'\u11BC': 'ㅇ', '\u11BD': 'ㅈ', '\u11BE': 'ㅊ', '\u11BF': 'ㅋ', '\u11C0': 'ㅌ',
|
|
34
|
+
'\u11C1': 'ㅍ', '\u11C2': 'ㅎ'
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const QWERTY_TO_KR = {
|
|
38
|
+
q: 'ㅂ', Q: 'ㅃ', w: 'ㅈ', W: 'ㅉ', e: 'ㄷ', E: 'ㄸ', r: 'ㄱ', R: 'ㄲ', t: 'ㅅ', T: 'ㅆ',
|
|
39
|
+
y: 'ㅛ', u: 'ㅕ', i: 'ㅑ', o: 'ㅐ', O: 'ㅒ', p: 'ㅔ', P: 'ㅖ',
|
|
40
|
+
a: 'ㅁ', s: 'ㄴ', d: 'ㅇ', f: 'ㄹ', g: 'ㅎ', h: 'ㅗ', j: 'ㅓ', k: 'ㅏ', l: 'ㅣ',
|
|
41
|
+
z: 'ㅋ', x: 'ㅌ', c: 'ㅊ', v: 'ㅍ', b: 'ㅠ', n: 'ㅜ', m: 'ㅡ'
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const QWERTY_DIRECT_PROFANITY = new Map([
|
|
45
|
+
['tlqkf', { category: 'profanity', root: '시발(한영타)' }],
|
|
46
|
+
['qudtls', { category: 'profanity', root: '병신(한영타)' }],
|
|
47
|
+
['wlfkf', { category: 'profanity', root: '지랄(한영타)' }],
|
|
48
|
+
['torl', { category: 'profanity', root: '새끼(한영타)' }],
|
|
49
|
+
['emdtls', { category: 'profanity', root: '등신(한영타)' }],
|
|
50
|
+
['aksgka', { category: 'hate', root: '한남(한영타)' }],
|
|
51
|
+
['zmfk', { category: 'sexual', root: '클리(한영타)' }],
|
|
52
|
+
['qhwl', { category: 'sexual', root: '보지(한영타)' }],
|
|
53
|
+
['wkwl', { category: 'sexual', root: '자지(한영타)' }],
|
|
54
|
+
['whssk', { category: 'profanity', root: '존나(한영타)' }],
|
|
55
|
+
['tqtq', { category: 'profanity', root: 'ㅅㅂ(한영타)' }],
|
|
56
|
+
['qtqt', { category: 'profanity', root: 'ㅂㅅ(한영타)' }],
|
|
57
|
+
['wf', { category: 'profanity', root: 'ㅈㄹ(한영타)' }],
|
|
58
|
+
['tr', { category: 'profanity', root: 'ㅅㄲ(한영타)' }]
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
const EMOJI_AND_SYMBOL_SEEDS = [
|
|
62
|
+
{ pattern: /🖕|凸/g, category: 'profanity', root: '뻐큐(이모지/기호)' },
|
|
63
|
+
{ pattern: /👉\s*👌|👉\s*🕳️?/g, category: 'sexual', root: '섹스(이모지)' },
|
|
64
|
+
{ pattern: /🍆/g, category: 'sexual', root: '자지(이모지)' },
|
|
65
|
+
{ pattern: /🍑/g, category: 'sexual', root: '보지(이모지)' },
|
|
66
|
+
{ pattern: /💦/g, category: 'sexual', root: '정액(이모지)' },
|
|
67
|
+
{ pattern: /卐|卍/g, category: 'hate', root: '나치(심볼)' }
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const MULTI_CHAR_REPLACEMENTS = [
|
|
71
|
+
[/_\s*[ㅣ|1!Il/\\]\s*_/g, '뻐큐'],
|
|
72
|
+
[/[\//][\\\]/g, '시'],
|
|
73
|
+
[/[\^^]{2}\s*[ㅣ|1!Il|¦]/g, '씨'],
|
|
74
|
+
[/[\^^]{2}\s*\/?\s*발/g, '씨발'],
|
|
75
|
+
[/[\^^]{2}\/?/g, 'ㅆ'],
|
|
76
|
+
[/77/g, 'ㄲ'],
|
|
77
|
+
[/ㅇl=스|ㅇㅣ=스/gi, '섹스'],
|
|
78
|
+
[/ㅇㅣ-ㅣ|ㅇl-l/gi, '애'],
|
|
79
|
+
[/lㅣ|1ㅣ|\|ㅣ/gi, '니'],
|
|
80
|
+
[/ㅁㅣ/g, '미'],
|
|
81
|
+
[/([가-힣ㄱ-ㅎㅏ-ㅣ])[!@#$%^&*_+\-=~?.,:;]{2,}([가-힣ㄱ-ㅎㅏ-ㅣ])/g, '$1$2'],
|
|
82
|
+
[/([가-힣])[0-79]+([가-힣])/g, '$1$2']
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
const UNICODE_STYLIZED_MAP = {
|
|
86
|
+
'🅰': 'a', '🅱': 'b', '🅲': 'c', '🅳': 'd', '🅴': 'e', '🅵': 'f', '🅶': 'g',
|
|
87
|
+
'🅷': 'h', '🅸': 'i', '🅹': 'j', '🅺': 'k', '🅻': 'l', '🅼': 'm', '🅽': 'n',
|
|
88
|
+
'🅾': 'o', '🅿': 'p', '🆀': 'q', '🆁': 'r', '🆂': 's', '🆃': 't', '🆄': 'u',
|
|
89
|
+
'🆅': 'v', '🆆': 'w', '🆇': 'x', '🆈': 'y', '🆉': 'z',
|
|
90
|
+
'🇦': 'a', '🇧': 'b', '🇨': 'c', '🇩': 'd', '🇪': 'e', '🇫': 'f', '🇬': 'g',
|
|
91
|
+
'🇭': 'h', '🇮': 'i', '🇯': 'j', '🇰': 'k', '🇱': 'l', '🇲': 'm', '🇳': 'n',
|
|
92
|
+
'🇴': 'o', '🇵': 'p', '🇶': 'q', '🇷': 'r', '🇸': 's', '🇹': 't', '🇺': 'u',
|
|
93
|
+
'🇻': 'v', '🇼': 'w', '🇽': 'x', '🇾': 'y', '🇿': 'z',
|
|
94
|
+
'ℹ': 'i', '🔞': '18',
|
|
95
|
+
'ᶠ': 'f', 'ᵘ': 'u', 'ᶜ': 'c', 'ᵏ': 'k', 'ˢ': 's', 'ʰ': 'h', 'ⁱ': 'i', 'ᵗ': 't',
|
|
96
|
+
'ᵇ': 'b', 'ᵃ': 'a', 'ᵒ': 'o', 'ˡ': 'l', 'ᵉ': 'e', 'ⁿ': 'n', 'ᵍ': 'g', 'ʳ': 'r',
|
|
97
|
+
'υ': 'u', 'і': 'i', 'а': 'a', 'е': 'e', 'о': 'o', 'р': 'p', 'с': 'c', 'у': 'y', 'х': 'x',
|
|
98
|
+
'①': '1', '②': '2', '③': '3', '④': '4', '⑤': '5', '⑥': '6', '⑦': '7', '⑧': '8', '⑨': '9', '⑩': '10'
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const KR_VISUAL_MAP = {
|
|
102
|
+
'8': '발',
|
|
103
|
+
'^': 'ㅅ', '^': 'ㅅ', '人': 'ㅅ', '∧': 'ㅅ', 'Λ': 'ㅅ',
|
|
104
|
+
'ㅗ': '오', '┻': '오', '┴': '오', '⊥': '오', '†': '오',
|
|
105
|
+
'甘': 'ㅂ', '廿': 'ㅂ', '田': 'ㅂ', '口': 'ㅂ', '日': 'ㅂ', '目': 'ㅂ',
|
|
106
|
+
'己': 'ㄹ', '乙': 'ㄹ', '已': 'ㄹ', '巳': 'ㄹ',
|
|
107
|
+
'匚': 'ㄷ',
|
|
108
|
+
'卜': 'ㅏ', '丨': 'ㅏ', '⼁': 'ㅏ',
|
|
109
|
+
'|': 'ㅣ', '!': 'ㅣ', 'I': 'ㅣ', 'l': 'ㅣ', '¦': 'ㅣ', '|': 'ㅣ',
|
|
110
|
+
'0': 'ㅇ', 'O': 'ㅇ', 'o': 'ㅇ', '◯': 'ㅇ', '○': 'ㅇ', '●': 'ㅇ', '◎': 'ㅇ', '⭕': 'ㅇ'
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const EN_LEET_MAP = {
|
|
114
|
+
'@': 'a', '4': 'a', 'α': 'a',
|
|
115
|
+
'8': 'b',
|
|
116
|
+
'(': 'c', '<': 'c', '¢': 'c', '©': 'c',
|
|
117
|
+
'3': 'e', '€': 'e', 'є': 'e',
|
|
118
|
+
'6': 'g', '9': 'g',
|
|
119
|
+
'#': 'h',
|
|
120
|
+
'1': 'i', '!': 'i', '|': 'i', 'l': 'i',
|
|
121
|
+
'0': 'o', '◯': 'o', '○': 'o',
|
|
122
|
+
'$': 's', '5': 's', '§': 's',
|
|
123
|
+
'7': 't', '+': 't',
|
|
124
|
+
'v': 'u', 'µ': 'u',
|
|
125
|
+
'2': 'z'
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
function stripInvisibleAndMarks(str) {
|
|
129
|
+
return str
|
|
130
|
+
.replace(/[\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF\u00AD\u180E\uFE0E\uFE0F\u20E3\u034F]/g, '')
|
|
131
|
+
.replace(/[\u0300-\u036F]/g, '');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function stripEmojiCodes(str) {
|
|
135
|
+
return str
|
|
136
|
+
.replace(/<a?:[a-zA-Z0-9_]+:\d+>/g, '')
|
|
137
|
+
.replace(/:[a-zA-Z0-9_+-]{1,32}:/g, '');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function mapMathAndEnclosedCodePoint(cp) {
|
|
141
|
+
if (cp >= 0x249c && cp <= 0x24b5) return String.fromCharCode(cp - 0x249c + 97);
|
|
142
|
+
if (cp >= 0x24b6 && cp <= 0x24cf) return String.fromCharCode(cp - 0x24b6 + 65);
|
|
143
|
+
if (cp >= 0x24d0 && cp <= 0x24e9) return String.fromCharCode(cp - 0x24d0 + 97);
|
|
144
|
+
if (cp >= 0xff01 && cp <= 0xff5e) return String.fromCharCode(cp - 0xfee0);
|
|
145
|
+
|
|
146
|
+
const mathRanges = [
|
|
147
|
+
[0x1d400, 0x1d419, 65], [0x1d41a, 0x1d433, 97],
|
|
148
|
+
[0x1d434, 0x1d44d, 65], [0x1d44e, 0x1d467, 97],
|
|
149
|
+
[0x1d468, 0x1d481, 65], [0x1d482, 0x1d49b, 97],
|
|
150
|
+
[0x1d49c, 0x1d4b5, 65], [0x1d4b6, 0x1d4cf, 97],
|
|
151
|
+
[0x1d4d0, 0x1d4e9, 65], [0x1d4ea, 0x1d503, 97],
|
|
152
|
+
[0x1d504, 0x1d51d, 65], [0x1d51e, 0x1d537, 97],
|
|
153
|
+
[0x1d538, 0x1d551, 65], [0x1d552, 0x1d56b, 97],
|
|
154
|
+
[0x1d56c, 0x1d585, 65], [0x1d586, 0x1d59f, 97],
|
|
155
|
+
[0x1d5a0, 0x1d5b9, 65], [0x1d5ba, 0x1d5d3, 97],
|
|
156
|
+
[0x1d5d4, 0x1d5ed, 65], [0x1d5ee, 0x1d607, 97],
|
|
157
|
+
[0x1d608, 0x1d621, 65], [0x1d622, 0x1d63b, 97],
|
|
158
|
+
[0x1d63c, 0x1d655, 65], [0x1d656, 0x1d66f, 97],
|
|
159
|
+
[0x1d670, 0x1d689, 65], [0x1d68a, 0x1d6a3, 97]
|
|
160
|
+
];
|
|
161
|
+
for (const [start, end, base] of mathRanges) {
|
|
162
|
+
if (cp >= start && cp <= end) {
|
|
163
|
+
return String.fromCharCode(cp - start + base);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const mathExceptions = {
|
|
167
|
+
0x212c: 'B', 0x2130: 'E', 0x2131: 'F', 0x210b: 'H', 0x2110: 'I',
|
|
168
|
+
0x2112: 'L', 0x2133: 'M', 0x211b: 'R', 0x212f: 'e', 0x210a: 'g',
|
|
169
|
+
0x2134: 'o', 0x2102: 'C', 0x210d: 'H', 0x2115: 'N', 0x2119: 'P',
|
|
170
|
+
0x211a: 'Q', 0x211d: 'R', 0x2124: 'Z', 0x212d: 'C', 0x210c: 'H',
|
|
171
|
+
0x2111: 'I', 0x211c: 'R', 0x2128: 'Z'
|
|
172
|
+
};
|
|
173
|
+
return mathExceptions[cp] || null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function normalizeCompatJamoAndWidth(str) {
|
|
177
|
+
let out = '';
|
|
178
|
+
for (const ch of str) {
|
|
179
|
+
if (UNICODE_STYLIZED_MAP[ch]) {
|
|
180
|
+
out += UNICODE_STYLIZED_MAP[ch];
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (CHOSEONG_TO_COMPAT[ch]) {
|
|
184
|
+
out += CHOSEONG_TO_COMPAT[ch];
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (JUNGSEONG_TO_COMPAT[ch]) {
|
|
188
|
+
out += JUNGSEONG_TO_COMPAT[ch];
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (JONGSEONG_TO_COMPAT[ch]) {
|
|
192
|
+
out += JONGSEONG_TO_COMPAT[ch];
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const cp = ch.codePointAt(0);
|
|
196
|
+
const mapped = mapMathAndEnclosedCodePoint(cp);
|
|
197
|
+
if (mapped) {
|
|
198
|
+
out += mapped;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
out += ch;
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function extractEmojiHits(str) {
|
|
207
|
+
const found = [];
|
|
208
|
+
for (const item of EMOJI_AND_SYMBOL_SEEDS) {
|
|
209
|
+
if (item.pattern.test(str)) {
|
|
210
|
+
found.push({ category: item.category, root: item.root });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return found;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function applyMultiCharRules(str) {
|
|
217
|
+
let out = str;
|
|
218
|
+
for (const [regex, replacement] of MULTI_CHAR_REPLACEMENTS) {
|
|
219
|
+
out = out.replace(regex, replacement);
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function applyCharMap(str, map) {
|
|
225
|
+
let out = '';
|
|
226
|
+
for (const ch of str) {
|
|
227
|
+
out += map[ch] || ch;
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function qwertyToJamo(text) {
|
|
233
|
+
let out = '';
|
|
234
|
+
for (const ch of text) {
|
|
235
|
+
out += QWERTY_TO_KR[ch] || ch;
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function decomposeHangul(text) {
|
|
241
|
+
let jamo = '';
|
|
242
|
+
let standaloneConsonants = '';
|
|
243
|
+
|
|
244
|
+
for (const ch of text) {
|
|
245
|
+
const code = ch.charCodeAt(0) - 0xac00;
|
|
246
|
+
if (code >= 0 && code <= 11171) {
|
|
247
|
+
const cho = Math.floor(code / 588);
|
|
248
|
+
const jung = Math.floor((code % 588) / 28);
|
|
249
|
+
const jong = code % 28;
|
|
250
|
+
|
|
251
|
+
const c = CHOSUNG[cho];
|
|
252
|
+
jamo += c + JUNGSUNG[jung];
|
|
253
|
+
|
|
254
|
+
if (jong > 0) {
|
|
255
|
+
const j = JONGSUNG[jong];
|
|
256
|
+
jamo += COMPLEX_JONG[j] || j;
|
|
257
|
+
}
|
|
258
|
+
} else if (COMPLEX_JONG[ch]) {
|
|
259
|
+
const expanded = COMPLEX_JONG[ch];
|
|
260
|
+
jamo += expanded;
|
|
261
|
+
standaloneConsonants += expanded;
|
|
262
|
+
} else if (CHOSUNG_SET.has(ch)) {
|
|
263
|
+
jamo += ch;
|
|
264
|
+
standaloneConsonants += ch;
|
|
265
|
+
} else if (JUNGSUNG_SET.has(ch)) {
|
|
266
|
+
jamo += ch;
|
|
267
|
+
} else {
|
|
268
|
+
jamo += ch;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return { jamo, standaloneConsonants };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function collapseStretchedVowels(jamoStr) {
|
|
276
|
+
return jamoStr
|
|
277
|
+
.replace(/([ㅏ-ㅣ])(?:ㅇ\1)+/g, '$1')
|
|
278
|
+
.replace(/([ㅏ-ㅣ])ㅇ(?:ㅇ[ㅏ-ㅣ])+/g, '$1');
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function convertMixedHangulQwerty(str) {
|
|
282
|
+
return str
|
|
283
|
+
.replace(/([가-힣ㄱ-ㅎㅏ-ㅣ])rI/g, '$1ㅐ')
|
|
284
|
+
.replace(/([가-힣ㄱ-ㅎㅏ-ㅣ])([rR])(?=[가-힣ㄱ-ㅎㅏ-ㅣI|1!l\s]|$)/g, '$1ㅏ')
|
|
285
|
+
.replace(/([가-힣ㄱ-ㅎㅏ-ㅣ])1(?=[가-힣ㄱ-ㅎㅏ-ㅣ\s]|$)/g, '$1ㅣ')
|
|
286
|
+
.replace(/si(?=[ㄱ-ㅎㅏ-ㅣ가-힣])/gi, '시')
|
|
287
|
+
.replace(/([가-힣ㄱ-ㅎㅏ-ㅣ])([a-zA-Z]{2,4})/g, (full, kr, en) => {
|
|
288
|
+
if (/^[I|l]+$/.test(en)) return kr + 'ㅣ'.repeat(en.length);
|
|
289
|
+
return kr + qwertyToJamo(en);
|
|
290
|
+
})
|
|
291
|
+
.replace(/([a-zA-Z]{2,4})([가-힣ㄱ-ㅎㅏ-ㅣ])/g, (_, en, kr) => qwertyToJamo(en) + kr);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function createTextViews(rawText, preStrippedUrls = false) {
|
|
295
|
+
const rawStr = String(rawText || '');
|
|
296
|
+
const emojiHits = extractEmojiHits(rawStr);
|
|
297
|
+
const emojiCodeStripped = stripEmojiCodes(rawStr);
|
|
298
|
+
const stripped = stripInvisibleAndMarks(emojiCodeStripped);
|
|
299
|
+
const normalized = normalizeCompatJamoAndWidth(stripped);
|
|
300
|
+
const collapsed = normalized.replace(/([^\s])\1{2,}/g, '$1$1');
|
|
301
|
+
const withoutUrls = preStrippedUrls ? collapsed : collapsed.replace(/https?:\/\/\S+|www\.\S+/gi, ' ');
|
|
302
|
+
const multiMapped = applyMultiCharRules(withoutUrls);
|
|
303
|
+
|
|
304
|
+
const withoutTimeExpressions = multiMapped.replace(/(^|[^가-힣0-9])\d{1,2}\s*시(?=\s+(?:출발|발송|발표|시작|종료|도착|예정|부터|까지|\d{1,2}분)|$)/g, '$1 ');
|
|
305
|
+
|
|
306
|
+
const mixedConverted = convertMixedHangulQwerty(withoutTimeExpressions);
|
|
307
|
+
const krMapped = applyCharMap(mixedConverted, KR_VISUAL_MAP).toLowerCase();
|
|
308
|
+
const krCompact = krMapped.replace(/[^a-z0-9가-힣ㄱ-ㅎㅏ-ㅣ]/g, '');
|
|
309
|
+
const krNoDigits = krCompact.replace(/[0-9]/g, '');
|
|
310
|
+
|
|
311
|
+
const { jamo, standaloneConsonants } = decomposeHangul(krNoDigits);
|
|
312
|
+
const stretchedJamo = collapseStretchedVowels(jamo);
|
|
313
|
+
|
|
314
|
+
const qwertyTokens = multiMapped
|
|
315
|
+
.replace(/[^a-zA-Z\s]/g, ' ')
|
|
316
|
+
.split(/\s+/)
|
|
317
|
+
.filter((tok) => tok.length >= 2);
|
|
318
|
+
|
|
319
|
+
const qwertyDirectHits = [];
|
|
320
|
+
for (const tok of qwertyTokens) {
|
|
321
|
+
const hit = QWERTY_DIRECT_PROFANITY.get(tok.toLowerCase());
|
|
322
|
+
if (hit) qwertyDirectHits.push(hit);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const qwertyRaw = qwertyTokens.map(qwertyToJamo).join('');
|
|
326
|
+
const qwertyDecomposed = decomposeHangul(qwertyRaw);
|
|
327
|
+
|
|
328
|
+
const shortQwertyOnlyTokens = qwertyTokens.filter((tok) => tok.length >= 2 && tok.length <= 3 && !/^[A-Z][a-z]+$/.test(tok));
|
|
329
|
+
const shortQwertyConsonants = decomposeHangul(shortQwertyOnlyTokens.map(qwertyToJamo).join('')).standaloneConsonants;
|
|
330
|
+
|
|
331
|
+
const camelSplit = multiMapped
|
|
332
|
+
.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
333
|
+
.replace(/[_-]+/g, ' ');
|
|
334
|
+
const enLower = camelSplit.toLowerCase();
|
|
335
|
+
const enLeet = applyCharMap(enLower, EN_LEET_MAP);
|
|
336
|
+
const enTokens = [
|
|
337
|
+
...new Set([
|
|
338
|
+
...enLower.replace(/[^a-z0-9\s]/g, ' ').split(/\s+/).filter(Boolean),
|
|
339
|
+
...enLeet.replace(/[^a-z\s]/g, ' ').split(/\s+/).filter(Boolean)
|
|
340
|
+
])
|
|
341
|
+
];
|
|
342
|
+
const enCompact = enLeet.replace(/[^a-z]/g, '');
|
|
343
|
+
|
|
344
|
+
const mixedEnJamoReplaced = normalized
|
|
345
|
+
.replace(/([a-zA-Z])[ㅓㅔ]([a-zA-Z])/g, '$1u$2')
|
|
346
|
+
.replace(/([a-zA-Z])[ㅣ1]([a-zA-Z])/g, '$1i$2')
|
|
347
|
+
.replace(/([a-zA-Z])[ㅋ]/g, '$1k')
|
|
348
|
+
.toLowerCase();
|
|
349
|
+
const mixedEnTokens = mixedEnJamoReplaced.replace(/[^a-z\s]/g, ' ').split(/\s+/).filter(Boolean);
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
raw: rawText,
|
|
353
|
+
emojiHits,
|
|
354
|
+
qwertyDirectHits,
|
|
355
|
+
krCompact,
|
|
356
|
+
krNoDigits,
|
|
357
|
+
jamo,
|
|
358
|
+
stretchedJamo,
|
|
359
|
+
standaloneConsonants,
|
|
360
|
+
qwertyJamo: qwertyDecomposed.jamo,
|
|
361
|
+
qwertyConsonants: shortQwertyConsonants,
|
|
362
|
+
enTokens: [...new Set([...enTokens, ...mixedEnTokens])],
|
|
363
|
+
enCompact
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
module.exports = {
|
|
368
|
+
decomposeHangul,
|
|
369
|
+
createTextViews
|
|
370
|
+
};
|