/** 그림 맞히기 text rules: answer normalisation, initial consonants, "아까워요" distance (docs/games/catchmind.md §5.3). */ const CHO = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']; const JUNG = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']; const JONG = ['', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']; /** Compound vowels and double finals split into their parts (§5.3 "겹받침·이중모음도 분해"). */ const SPLIT: Record = { ㅘ: 'ㅗㅏ', ㅙ: 'ㅗㅐ', ㅚ: 'ㅗㅣ', ㅝ: 'ㅜㅓ', ㅞ: 'ㅜㅔ', ㅟ: 'ㅜㅣ', ㅢ: 'ㅡㅣ', ㄳ: 'ㄱㅅ', ㄵ: 'ㄴㅈ', ㄶ: 'ㄴㅎ', ㄺ: 'ㄹㄱ', ㄻ: 'ㄹㅁ', ㄼ: 'ㄹㅂ', ㄽ: 'ㄹㅅ', ㄾ: 'ㄹㅌ', ㄿ: 'ㄹㅍ', ㅀ: 'ㄹㅎ', ㅄ: 'ㅂㅅ', }; const SYL_FIRST = 0xac00; const SYL_LAST = 0xd7a3; const isSyllable = (ch: string): boolean => { const c = ch.codePointAt(0)!; return c >= SYL_FIRST && c <= SYL_LAST; }; /** Text without spaces, punctuation and symbols (raw, before NFKC). */ const bare = (raw: string): string => raw.replace(/[\s\p{P}\p{S}\u200B-\u200D\uFEFF]/gu, ''); /** Hangul compatibility jamo only (U+3131–U+318E), ignoring spaces and punctuation. */ export function isCompatJamoOnly(raw: string): boolean { const t = bare(raw); return t.length > 0 && /^[ㄱ-ㆎ]+$/u.test(t); } const ENDINGS = ['요', '다', '이다', '입니다', '인가', '인가요']; /** §5.3 normalize(): NFKC, zero-width, spaces, punctuation/symbols/emoji, lower case. */ export function normalize(s: string): string { return s .normalize('NFKC') .replace(/[\u200B-\u200D\uFEFF\u2060\uFE0F\u20E3]/gu, '') .replace(/\s+/gu, '') .replace(/[\p{P}\p{S}\p{Extended_Pictographic}]/gu, '') .toLowerCase(); } /** Initial consonants of every syllable (other characters kept as they are). */ export function chosung(word: string): string { return [...word] .map((ch) => (isSyllable(ch) ? CHO[Math.floor((ch.codePointAt(0)! - SYL_FIRST) / 588)]! : ch)) .join(''); } export function initialOf(ch: string): string { return isSyllable(ch) ? CHO[Math.floor((ch.codePointAt(0)! - SYL_FIRST) / 588)]! : ch; } /** Syllables → compatibility jamo with compound vowels / double finals split. */ export function jamo(s: string): string { let out = ''; for (const ch of s) { if (!isSyllable(ch)) { out += SPLIT[ch] ?? ch; continue; } const i = ch.codePointAt(0)! - SYL_FIRST; const l = CHO[Math.floor(i / 588)]!; const v = JUNG[Math.floor((i % 588) / 28)]!; const t = JONG[i % 28]!; out += l + (SPLIT[v] ?? v) + (SPLIT[t] ?? t); } return out; } export function levenshtein(a: string, b: string): number { const x = [...a]; const y = [...b]; let prev = Array.from({ length: y.length + 1 }, (_, j) => j); for (let i = 1; i <= x.length; i++) { const cur = [i]; for (let j = 1; j <= y.length; j++) { cur[j] = Math.min(prev[j]! + 1, cur[j - 1]! + 1, prev[j - 1]! + (x[i - 1] === y[j - 1] ? 0 : 1)); } prev = cur; } return prev[y.length]!; } export type Judgement = 'correct' | 'chosung' | 'contains' | 'close' | 'plain'; /** * Judges one guess against the answer and its aliases (§5.3). * - Compatibility-jamo-only text is never correct; it is held back when it spells the answer's initials or the answer itself. * - correct: exact match after normalize, optionally followed by a polite ending (요/다/이다/입니다/인가/인가요). * - contains: the answer inside a longer message → held back. * - close: jamo edit distance ≤ 1 (answers up to 3 letters) or ≤ 2 (4+ letters). */ export function judge(text: string, answers: string[], closeHint: boolean): Judgement { const cands = answers.map(normalize).filter((c) => c.length > 0); if (isCompatJamoOnly(text)) { const t = bare(text); const spelled = normalize(text); if (cands.some((c) => chosung(c) === t || spelled === c || spelled.includes(c))) return 'chosung'; return 'plain'; } const n = normalize(text); if (!n) return 'plain'; for (const c of cands) { if (n === c) return 'correct'; if (n.startsWith(c) && ENDINGS.includes(n.slice(c.length))) return 'correct'; } if (cands.some((c) => n.includes(c))) return 'contains'; if (closeHint) { const jn = jamo(n); for (const c of cands) { const limit = [...c].length <= 3 ? 1 : 2; if (levenshtein(jn, jamo(c)) <= limit) return 'close'; } } return 'plain'; } // ---------------------------------------------------------------- filters /** Small built-in list (same spirit as the nickname filter). Matching ignores spaces and punctuation. */ const BANNED = ['시발', '씨발', '병신', '좆', '개새끼', '느금', '니애미', '섹스', '자살', '살인', 'fuck', 'shit', 'bitch', 'sex']; export function isBanned(text: string): boolean { const squashed = text.normalize('NFKC').toLowerCase().replace(/[\s_.\-*0-9]/g, ''); return BANNED.some((w) => squashed.includes(w)); } export function maskBanned(text: string): string { let out = text; for (const w of BANNED) { const re = new RegExp(w.split('').join('[\\s_.\\-*]*'), 'gi'); out = out.replace(re, (m) => '*'.repeat(m.length)); } return out; } /** Control characters out, trimmed. */ export function cleanText(text: string): string { return text.replace(/[\u0000-\u001f\u007f]/g, ' ').trim(); } export const CUSTOM_WORD_RE = /^[가-힣A-Za-z0-9]{1,10}$/; export const MAX_CUSTOM_WORDS = 200; /** §3.2 customWords: one per line, 1–10 Hangul/Latin/digits, filtered, deduplicated, at most 200. */ export function parseCustomWords(raw: string): string[] { const out: string[] = []; const seen = new Set(); for (const line of raw.split(/\r?\n/)) { const w = line.normalize('NFC').trim(); if (!CUSTOM_WORD_RE.test(w) || isBanned(w)) continue; const key = normalize(w); if (seen.has(key)) continue; seen.add(key); out.push(w); if (out.length >= MAX_CUSTOM_WORDS) break; } return out; }