feat(input): expand Araso Han/Eng contractions, acronyms, typo recovery, and fast cursor-tail replacement

This commit is contained in:
Yun Chan 2026-10-28 10:41:26 +09:00
parent 51bf664302
commit f403111256
80 changed files with 1449 additions and 458 deletions

View file

@ -3,6 +3,7 @@
import { describe, expect, it } from 'vitest'
import {
buildModelTypoHints,
buildVocabulary,
detectHangulTypedLatin,
detectJamoNoise,
@ -19,7 +20,6 @@ import {
parseModelTypoPairs,
planTypoReplacement,
alignCorrectionToPrefix,
validateModelCorrections,
typoRegionStart,
validateModelCorrections
} from '../src/typo-correction'
@ -86,6 +86,109 @@ describe('한영 전환 오타 판정', () => {
expect(detectHangulTypedLatin('ㄴ은', KO)).toBe('s은')
expect(detectHangulTypedLatin('ㅠㅕㅎ가', KO)).toBe('bug가')
})
it('한글로 친 영어 축약형·오타(ㅊ무ㅅ, ㅊ무\'ㅅ, ㅊ무ㅜ\'ㅅ, 애ㅜ\'ㅅ, ㅑ\'ㅡ, ㅗ디ㅐ, ㅔㅐㄴ냐ㅠㅣ, 세갸ㅜㅎ)를 즉시 복원한다', () => {
expect(detectHangulTypedLatin('ㅊ무ㅅ', KO)).toBe("can't")
expect(detectHangulTypedLatin("ㅊ무'ㅅ", KO)).toBe("can't")
expect(detectHangulTypedLatin("ㅊ무ㅜ'ㅅ", KO)).toBe("can't")
expect(detectHangulTypedLatin("애ㅜ'ㅅ", KO)).toBe("don't")
expect(detectHangulTypedLatin("ㅑ'ㅡ", KO)).toBe("I'm")
expect(detectHangulTypedLatin('ㅑㅡ', KO)).toBe("I'm")
expect(detectHangulTypedLatin("ㅑㅅ'ㄴ", KO)).toBe("it's")
expect(detectHangulTypedLatin('ㅗ디ㅐ', KO)).toBe('hello')
expect(detectHangulTypedLatin('ㅗ디ㅣㅐㅐ', KO)).toBe('hello')
expect(detectHangulTypedLatin('ㅔㅐㄴ냐ㅠㅣ', KO)).toBe('possible')
expect(detectHangulTypedLatin('세갸ㅜㅎ', KO)).toBe('spring')
expect(detectHangulTypedLatin('ㅡㅁ가애주', KO)).toBe('markdown')
})
it('사용자 실측 문장("I ㅊ무ㅅ ㅅ I ㅊ무ㅜ\'ㅅ help[ my self ")을 한 번에 교정한다', () => {
const prefix = "I ㅊ무ㅅ ㅅ I ㅊ무ㅜ'ㅅ help[ my self "
const merged = mergeTypoCorrections(
detectTypos(prefix, { includeTrailingToken: true, layoutOnly: true }),
detectJamoNoise(prefix)
)
const plan = planTypoReplacement(prefix, merged)
expect(plan).not.toBeNull()
expect(plan?.insertText).toBe("can't I can't help myself ")
})
it('문장 첫머리의 단독 모음 "ㅑ " 와 "ㅑ ㅊ무\'ㅅ (는)" 및 Araso 받침 호응 약어("츤를" → "CMS를", "눈를" → "SNS를")를 복원한다', () => {
// 1. 문장 첫머리 "ㅑ " 단독 입력 시 공백 경계 즉시 "I " 복원
expect(detectTypos('ㅑ ', { layoutOnly: true }).map((c) => c.to)).toEqual(['I'])
// 2. "ㅑ ㅊ무'ㅅ " 및 "ㅑ ㅊ무'ㅅ 는 안되네." / "ㅑ ㅊ무'ㅅ는 안되네."
const cantPlan = planTypoReplacement('ㅑ ㅊ무\'ㅅ ', detectTypos('ㅑ ㅊ무\'ㅅ ', { layoutOnly: true }))
expect(cantPlan?.insertText).toBe("I can't ")
const cantWithParticleSpace = planTypoReplacement(
"ㅑ ㅊ무'ㅅ 는 안되네.",
detectTypos("ㅑ ㅊ무'ㅅ 는 안되네.", { includeTrailingToken: true, layoutOnly: true })
)
expect(cantWithParticleSpace?.insertText).toBe("I can't 는 안되네.")
const cantWithAttachedParticle = planTypoReplacement(
"ㅑ ㅊ무'ㅅ는 안되네.",
detectTypos("ㅑ ㅊ무'ㅅ는 안되네.", { includeTrailingToken: true, layoutOnly: true })
)
expect(cantWithAttachedParticle?.insertText).toBe("I can't는 안되네.")
// 3. "나는 츤를 만들려고 해." → "나는 CMS를 만들려고 해." 및 Araso 받침 호응 약어+조사
const cmsPlan = planTypoReplacement(
'나는 츤를 만들려고 해.',
detectTypos('나는 츤를 만들려고 해.', { includeTrailingToken: true, layoutOnly: true })
)
expect(cmsPlan?.insertText).toBe('CMS를 만들려고 해.')
expect(detectHangulTypedLatin('츤를', KO)).toBe('CMS를')
expect(detectHangulTypedLatin('먀를', KO)).toBe('AI를')
expect(detectHangulTypedLatin('틔을', KO)).toBe('XML을')
expect(detectHangulTypedLatin('뗘를', KO)).toBe('EU를')
expect(detectHangulTypedLatin('넴를', KO)).toBe('SPA를')
expect(detectHangulTypedLatin('젳를', KO)).toBe('VPC를')
expect(detectHangulTypedLatin('눈를', KO)).toBe('SNS를')
expect(detectHangulTypedLatin('운를', KO)).toBe('DNS를')
expect(detectHangulTypedLatin('긴를', KO)).toBe('RLS를')
expect(detectHangulTypedLatin('쓰은', KO)).toBe('TM은')
expect(detectHangulTypedLatin('꾸은', KO)).toBe('RN은')
expect(detectHangulTypedLatin('띠이', KO)).toBe('EL이')
expect(detectHangulTypedLatin('꺼을', KO)).toBe('RJ을')
expect(detectHangulTypedLatin('씨은', KO)).toBe('TL은')
expect(detectHangulTypedLatin('ㅏㅅㅌ를', KO)).toBe('KTX를')
expect(detectHangulTypedLatin('ㅎㅅㅌ를', KO)).toBe('GTX를')
expect(detectHangulTypedLatin('ㅠㅅㄴ를', KO)).toBe('BTS를')
expect(detectHangulTypedLatin('ㅐㅅㅅ를', KO)).toBe('OTT를')
expect(detectHangulTypedLatin('ㄷㅅㄹ를', KO)).toBe('ETF를')
expect(detectHangulTypedLatin('ㅊㄷㅐ가', KO)).toBe('CEO가')
expect(detectHangulTypedLatin('ㅔㅡ을', KO)).toBe('PM을')
// 받침이 정상 호응하는 한국어 어절은 절대 약어로 오변환하지 않는다
expect(detectHangulTypedLatin('눈을', KO)).toBeNull()
expect(detectHangulTypedLatin('운이', KO)).toBeNull()
expect(detectHangulTypedLatin('쓰는', KO)).toBeNull()
expect(detectHangulTypedLatin('꾸는', KO)).toBeNull()
expect(detectHangulTypedLatin('이를', KO)).toBeNull()
// 4. 영문 문맥 안의 "새"(to), "애"(do), "뭉"(and), "랙"(for), "소됴"(they), "생묘"(today) 복원
expect(
planTypoReplacement('CMS는 I want 새 ', detectTypos('CMS는 I want 새 ', { layoutOnly: true }))?.insertText
).toBe('to ')
expect(
planTypoReplacement('CMS는 I want 새 애 ', detectTypos('CMS는 I want 새 애 ', { layoutOnly: true }))?.insertText
).toBe('to do ')
const fullSentence = "CMS는 I want 새 애 this but this won't change into english 새 애"
const fullPlan = planTypoReplacement(
fullSentence,
detectTypos(fullSentence, { includeTrailingToken: true, layoutOnly: true })
)
expect(
fullPlan ? fullSentence.slice(0, fullSentence.length - fullPlan.deleteCount) + fullPlan.insertText : fullSentence
).toBe("CMS는 I want to do this but this won't change into english to do")
// 한국어 문장의 "새 API", "이거 commit 해" 는 절대 to/go 로 오변환하지 않는다
expect(detectTypos('오늘 새 API를 만들었어 ', { includeTrailingToken: true, layoutOnly: true })).toEqual([])
expect(detectTypos('이거 commit 해 ', { includeTrailingToken: true, layoutOnly: true })).toEqual([])
})
})
describe('늘 틀린 철자', () => {
@ -182,6 +285,41 @@ describe('모델 교정 검증', () => {
expect(isPlausibleTypoFix('갠적', '개인적')).toBe(false)
// 긴 어절에서 빠진 음절을 채우는 것은 오타 수정이다
expect(isPlausibleTypoFix('공유드립다', '공유드립니다')).toBe(true)
// 한영 자판 오타 + 철자 오타 복합 교정 (사전에 없거나 1~2타 오타가 겹친 경우)
expect(isPlausibleTypoFix('ㅗ디ㅐ', 'hello')).toBe(true)
expect(isPlausibleTypoFix('ㅡㅁ가둥', 'markdown')).toBe(true)
expect(isPlausibleTypoFix('네갸ㅜㅎ에서', 'spring에서')).toBe(true)
expect(isPlausibleTypoFix('세갸ㅜㅎ에서', 'spring에서')).toBe(true)
expect(isPlausibleTypoFix('dkssudgktpd', '안녕하세요')).toBe(true)
expect(isPlausibleTypoFix('아ㄴ녀ㅇ', '안녕')).toBe(true)
// 단순 번역(키열 무관)은 거른다
expect(isPlausibleTypoFix('hello', '안녕')).toBe(false)
expect(isPlausibleTypoFix('회의', 'meeting')).toBe(false)
})
it('수상한 한영·자모 어절에 대해 두벌식 키 입력 힌트를 생성하고 모델 교정을 검증한다', () => {
const sentence = 'ㅡㅁ가둥 문서와 ㅗ디ㅐ 네갸ㅜㅎ에서 dkssudgktpd '
const hints = buildModelTypoHints(sentence)
expect(hints).toEqual([
'- "ㅡㅁ가둥" (영문 자판 키 입력값: "markend")',
'- "ㅗ디ㅐ" (영문 자판 키 입력값: "helo")',
'- "네갸ㅜㅎ에서" (영문 자판 키 입력값: "spring에서")',
'- "dkssudgktpd" (한글 자판 조합값: "안녕하셍")'
])
// 모델이 원문 어절("ㅡㅁ가둥")이나 힌트 키값("helo", "안녕하셍") 어느 쪽을 from 으로 돌려줘도 매칭한다
const validated = validateModelCorrections(sentence, [
{ from: 'ㅡㅁ가둥', to: 'markdown' },
{ from: 'helo', to: 'hello' },
{ from: '네갸ㅜㅎ에서', to: 'spring에서' },
{ from: '안녕하셍', to: '안녕하세요' }
])
expect(validated.map((c) => [c.from, c.to])).toEqual([
['ㅡㅁ가둥', 'markdown'],
['ㅗ디ㅐ', 'hello'],
['네갸ㅜㅎ에서', 'spring에서'],
['dkssudgktpd', '안녕하세요']
])
})
it('접두의 실제 단어 위치로 옮기고, 무시·자주 쓴 단어는 뺀다', () => {