채팅: 한영 전환 실수 보정(dkssud→안녕, ㅗ디ㅣㅐ→hello) — 보수적 판정으로 변환문을 모델에 전달하고 info로 안내
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01400a064f
commit
b1abe5912c
3 files changed
+145
No files matched your search
@@ -86,6 +86,7 @@ import { buildChatSystemPrompt, groundingResultReadingRule } from './system-prom
|
||||
import { decideRoute } from './model-routing';
|
||||
import { weatherFabricationReplacement } from './tool-failure-backstop';
|
||||
import { weatherFollowUpHint } from './followup-hints';
|
||||
import { fixKeyboardLayout } from './hangul-layout';
|
||||
import { suspectSoundAlikePlace } from './voice-place-check';
|
||||
import { replyIsWrongLanguage, TRANSLATE_TO_KOREAN_SYSTEM } from './language-backstop';
|
||||
import { lightChat } from '../../providers/light-chat';
|
||||
@@ -232,6 +233,15 @@ async function handleChat(
|
||||
username?: string,
|
||||
historyOverride?: Array<{ role: string; content: string }>,
|
||||
): Promise<HandleChatResult> {
|
||||
// 한영 전환 실수("dkssud"→안녕, "ㅗ디ㅣㅐ"→hello)면 되돌린 문장으로 처리한다 (hangul-layout.ts). 모델에는 "한영 전환 오타로 이해했다"는 안내(info)와 함께 변환문이 간다.
|
||||
if (executionMode === 'interactive' && !sessionId.startsWith('translate-')) {
|
||||
const _lay = fixKeyboardLayout(message);
|
||||
if (_lay) {
|
||||
console.log(`[v2] KEYBOARD-LAYOUT FIX (${_lay.kind}): "${message}" → "${_lay.text}"`);
|
||||
try { sendSSE('info', { message: `한영 전환 오타로 보여 "${_lay.text}"(으)로 이해했습니다.` }); } catch { /* non-fatal */ }
|
||||
message = _lay.text;
|
||||
}
|
||||
}
|
||||
const ollama = getOllamaClient();
|
||||
const isBootStartupTurn = /\bBOOT\.md\b/i.test(String(callerContext || ''));
|
||||
const configuredWorkspace = getConfig().getWorkspacePath();
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 한영 전환 실수 보정 (2026-10-19).
|
||||
*
|
||||
* 한글 모드를 켜야 하는데 영문 모드로 쳐서 "dkssudgktpdy"(안녕하세요)가 오거나, 반대로 영어를 한글 모드로 쳐서
|
||||
* "ㅗ디ㅣㅐ"(hello)가 오는 경우를 두벌식 자판 기준으로 되돌린다. 오탐이 가장 큰 위험이라 보수적으로 판정한다:
|
||||
* - 영→한: 영문자·공백·기본 문장부호만 있고, 모든 단어가 남는 자모 없이 온전한 음절로 조립되며, 흔한 영어 단어가 없을 때만.
|
||||
* - 한→영: 낱자모가 있고, 되돌린 결과에 흔한 영어 단어가 있을 때만("ㅋㅋㅋ"→zzz 같은 웃음 표기는 건드리지 않는다).
|
||||
*/
|
||||
|
||||
const EN2JAMO: Record<string, string> = {
|
||||
q: 'ㅂ', w: 'ㅈ', e: 'ㄷ', r: 'ㄱ', t: 'ㅅ', y: 'ㅛ', u: 'ㅕ', i: 'ㅑ', o: 'ㅐ', p: 'ㅔ',
|
||||
a: 'ㅁ', s: 'ㄴ', d: 'ㅇ', f: 'ㄹ', g: 'ㅎ', h: 'ㅗ', j: 'ㅓ', k: 'ㅏ', l: 'ㅣ',
|
||||
z: 'ㅋ', x: 'ㅌ', c: 'ㅊ', v: 'ㅍ', b: 'ㅠ', n: 'ㅜ', m: 'ㅡ',
|
||||
Q: 'ㅃ', W: 'ㅉ', E: 'ㄸ', R: 'ㄲ', T: 'ㅆ', O: 'ㅒ', P: 'ㅖ',
|
||||
};
|
||||
const JAMO2EN: Record<string, string> = {};
|
||||
for (const [k, v] of Object.entries(EN2JAMO)) JAMO2EN[v] = k; // 자모마다 키가 하나뿐(쌍자음·ㅒㅖ는 Shift 키)
|
||||
|
||||
const CHO = 'ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ';
|
||||
const JUNG = 'ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ';
|
||||
const JONG = ['', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'];
|
||||
const VOWELS = new Set('ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅛㅜㅠㅡㅣ'.split(''));
|
||||
const V_COMBO: Record<string, string> = { 'ㅗㅏ': 'ㅘ', 'ㅗㅐ': 'ㅙ', 'ㅗㅣ': 'ㅚ', 'ㅜㅓ': 'ㅝ', 'ㅜㅔ': 'ㅞ', 'ㅜㅣ': 'ㅟ', 'ㅡㅣ': 'ㅢ' };
|
||||
const J_COMBO: Record<string, string> = { 'ㄱㅅ': 'ㄳ', 'ㄴㅈ': 'ㄵ', 'ㄴㅎ': 'ㄶ', 'ㄹㄱ': 'ㄺ', 'ㄹㅁ': 'ㄻ', 'ㄹㅂ': 'ㄼ', 'ㄹㅅ': 'ㄽ', 'ㄹㅌ': 'ㄾ', 'ㄹㅍ': 'ㄿ', 'ㄹㅎ': 'ㅀ', 'ㅂㅅ': 'ㅄ' };
|
||||
const J_SPLIT: Record<string, [string, string]> = {};
|
||||
for (const [k, v] of Object.entries(J_COMBO)) J_SPLIT[v] = [k[0], k[1]];
|
||||
|
||||
function syl(cho: string, jung: string, jong: string): string {
|
||||
return String.fromCharCode(0xac00 + (CHO.indexOf(cho) * 21 + JUNG.indexOf(jung)) * 28 + JONG.indexOf(jong));
|
||||
}
|
||||
|
||||
/** 자모 나열 → 음절 조립. stray = 짝을 못 찾아 낱자모로 남은 개수. */
|
||||
function compose(jamo: string[]): { text: string; stray: number; syllables: number } {
|
||||
let out = '', stray = 0, syllables = 0;
|
||||
let i = 0;
|
||||
const isV = (c?: string) => !!c && (VOWELS.has(c) || 'ㅘㅙㅚㅝㅞㅟㅢ'.includes(c));
|
||||
while (i < jamo.length) {
|
||||
const c = jamo[i];
|
||||
if (isV(c) || CHO.indexOf(c) < 0) { out += c; stray++; i++; continue; } // 모음 단독 / 초성 불가 자모
|
||||
let j = jamo[i + 1];
|
||||
if (!isV(j)) { out += c; stray++; i++; continue; } // 자음 뒤에 모음 없음
|
||||
let jung = j; i += 2;
|
||||
if (V_COMBO[jung + (jamo[i] || '')]) { jung = V_COMBO[jung + jamo[i]]; i++; }
|
||||
let jong = '';
|
||||
const a = jamo[i], b = jamo[i + 1], c2 = jamo[i + 2];
|
||||
if (a && JONG.includes(a) && !isV(a)) {
|
||||
if (isV(b)) { /* a는 다음 음절 초성 */ }
|
||||
else if (b && J_COMBO[a + b] && !isV(c2)) { jong = J_COMBO[a + b]; i += 2; }
|
||||
else if (b && J_COMBO[a + b] && isV(c2)) { jong = a; i += 1; } // 겹받침 뒤에 모음 → b는 다음 초성
|
||||
else { jong = a; i += 1; }
|
||||
}
|
||||
out += syl(c, jung, jong); syllables++;
|
||||
}
|
||||
return { text: out, stray, syllables };
|
||||
}
|
||||
|
||||
const EN_WORDS = new Set(('the a an is are was were be to of and or in on at for with from by this that these those it its i you he she we they me my your our '
|
||||
+ 'what how why when where who which can could will would should do does did not no yes ok okay hi hello hey thanks thank please help show tell give make get '
|
||||
+ 'have has had if then than so but as about into over under up down out new old good bad best top list file code test run app web http https www com org net '
|
||||
+ 'weather news price today tomorrow yesterday time date image photo video music search find open close start stop').split(' '));
|
||||
|
||||
function hasEnglishWord(words: string[]): boolean {
|
||||
return words.some((w) => EN_WORDS.has(w.toLowerCase().replace(/[^a-z]/g, '')));
|
||||
}
|
||||
|
||||
export interface LayoutFix { text: string; kind: 'en2ko' | 'ko2en'; }
|
||||
|
||||
export function fixKeyboardLayout(message: string): LayoutFix | null {
|
||||
const raw = String(message || '').trim();
|
||||
if (raw.length < 3 || raw.length > 400) return null;
|
||||
if (/https?:\/\/|[`\/\\@#$%^&*=<>{}\[\]|~_\d]/.test(raw)) return null; // URL·코드·숫자·경로가 섞이면 손대지 않는다
|
||||
|
||||
// 영문 모드로 친 한글
|
||||
if (/^[A-Za-z\s.,?!]+$/.test(raw)) {
|
||||
const words = raw.split(/\s+/).filter(Boolean);
|
||||
if (hasEnglishWord(words)) return null;
|
||||
let syllables = 0, sawLetters = false;
|
||||
const out = words.map((w) => {
|
||||
const core = w.replace(/[.,?!]+$/, ''), tail = w.slice(core.length);
|
||||
const jamo = core.split('').map((ch) => EN2JAMO[ch] ?? EN2JAMO[ch.toLowerCase()]);
|
||||
if (jamo.some((x) => !x)) return null;
|
||||
sawLetters = true;
|
||||
const r = compose(jamo);
|
||||
if (r.stray > 0) return null;
|
||||
syllables += r.syllables;
|
||||
return r.text + tail;
|
||||
});
|
||||
if (!sawLetters || out.some((x) => x === null) || syllables < 2) return null;
|
||||
return { text: out.join(' '), kind: 'en2ko' };
|
||||
}
|
||||
|
||||
// 한글 모드로 친 영어: 낱자모가 있고, 되돌린 결과에 흔한 영어 단어가 있을 때만
|
||||
if (/[ㄱ-ㅎㅏ-ㅣ]/.test(raw) && /^[ㄱ-ㅎㅏ-ㅣ가-힣\s.,?!]+$/.test(raw)) {
|
||||
let conv = '';
|
||||
for (const ch of raw) {
|
||||
const code = ch.charCodeAt(0);
|
||||
if (code >= 0xac00 && code <= 0xd7a3) {
|
||||
const n = code - 0xac00, cho = CHO[Math.floor(n / 588)], jung = JUNG[Math.floor((n % 588) / 28)], jong = JONG[n % 28];
|
||||
const parts: string[] = [cho];
|
||||
for (const v of (Object.entries(V_COMBO).find(([, x]) => x === jung)?.[0] || jung)) parts.push(v);
|
||||
if (jong) for (const jj of (J_SPLIT[jong] || [jong])) parts.push(jj);
|
||||
for (const p of parts) { const e = JAMO2EN[p]; if (!e) return null; conv += e; }
|
||||
} else if (/[ㄱ-ㅎㅏ-ㅣ]/.test(ch)) { const e = JAMO2EN[ch]; if (!e) return null; conv += e; }
|
||||
else conv += ch;
|
||||
}
|
||||
if (!/[A-Za-z]/.test(conv) || !hasEnglishWord(conv.split(/\s+/))) return null;
|
||||
return { text: conv, kind: 'ko2en' };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fixKeyboardLayout } from '../src/gateway/chat/hangul-layout';
|
||||
|
||||
test('영문 모드로 친 한글을 되돌린다', () => {
|
||||
assert.equal(fixKeyboardLayout('dkssudgktpdy')?.text, '안녕하세요');
|
||||
assert.equal(fixKeyboardLayout('rnal skfTl')?.text, '구미 날씨');
|
||||
assert.equal(fixKeyboardLayout('rnal skfTl?')?.text, '구미 날씨?');
|
||||
assert.equal(fixKeyboardLayout('rnal skfTl')?.kind, 'en2ko');
|
||||
});
|
||||
test('겹받침·겹모음·쌍자음', () => {
|
||||
assert.equal(fixKeyboardLayout('rkqt dlTek')?.text, '값 있다');
|
||||
assert.equal(fixKeyboardLayout('dhkTsk')?.text, '왔나');
|
||||
});
|
||||
test('영어 문장은 건드리지 않는다', () => {
|
||||
for (const s of ['what is the weather', 'hello there', 'dog', 'how are you', 'test code', 'thanks a lot']) assert.equal(fixKeyboardLayout(s), null, s);
|
||||
});
|
||||
test('URL·숫자·코드·정상 한글은 그대로', () => {
|
||||
for (const s of ['https://example.com', 'abc123', 'ㅋㅋㅋ', '안녕하세요', '오늘 날씨', 'ㅇㅇ 알겠어', 'ㅠㅠ']) assert.equal(fixKeyboardLayout(s), null, s);
|
||||
});
|
||||
test('한글 모드로 친 영어를 되돌린다', () => {
|
||||
assert.equal(fixKeyboardLayout('ㅗ디ㅣㅐ')?.text, 'hello');
|
||||
assert.equal(fixKeyboardLayout('ㅗㅐㅈ ㅁㄱㄷ ㅛㅐㅕ')?.text, 'how are you');
|
||||
assert.equal(fixKeyboardLayout('ㅗ디ㅣㅐ')?.kind, 'ko2en');
|
||||
});
|
||||
Reference in new issue
Block a user