feat(input): upgrade graph personalization, english morphology detection, and atomic SendInput replacement

This commit is contained in:
Yun Chan 2026-10-29 08:16:26 +09:00
parent 52eef21bab
commit cd4a452148
21 changed files with 1098 additions and 110 deletions

View file

@ -4,12 +4,16 @@
import { describe, expect, it } from 'vitest'
import {
SHARES_TERMS_MIN_SIMILARITY,
buildQueryTerms,
buildSequenceEdges,
continuationsFrom,
extractTerms,
jaccard,
rankRelated,
splitSentences
rankTwoHopGraphTargets,
scoreCandidatePersonalization,
splitSentences,
stemContinuationsFrom
} from '../src/personal-graph'
describe('extractTerms', () => {
@ -136,4 +140,50 @@ describe('rankRelated', () => {
expect(SHARES_TERMS_MIN_SIMILARITY).toBeGreaterThan(0)
expect(SHARES_TERMS_MIN_SIMILARITY).toBeLessThan(1)
})
})
describe('stemContinuationsFrom · 2-hop 그래프 · 개인화 후보 재정렬', () => {
it('조사가 달라도 어간 열이 일치하면 그 뒤의 이어쓰기를 추출한다', () => {
const out = stemContinuationsFrom(
['회의록 정리해서 내일 오전까지 공유드리겠습니다'],
'이번 주간 회의록을 정리해서'
)
expect(out).toEqual(['내일 오전까지 공유드리겠습니다'])
})
it('2-hop 경로(shares_terms -> follows)로 유사 문장 뒤에 실제로 썼던 후속 문장을 찾는다', () => {
const queryTerms = buildQueryTerms([{ text: '금요일 배포 준비 끝났습니다', weight: 2 }])
const targets = rankTwoHopGraphTargets({
firstHopEdges: [
{
from: '금요일 배포 준비 완료했습니다',
to: '스테이징 배포 점검 끝났습니다',
kind: 'shares_terms',
weight: 2
}
],
secondHopFollowEdges: [
{
from: '스테이징 배포 점검 끝났습니다',
to: '모니터링 대시보드 링크 공유드립니다',
kind: 'follows',
weight: 3
}
],
queryTerms
})
expect(targets).toEqual(['모니터링 대시보드 링크 공유드립니다'])
})
it('개인 그래프 힌트 및 어투와 일치하는 후보에 더 높은 개인화 점수를 준다', () => {
const context = {
prefix: '오늘 배포 일정 공유드립니다. ',
followUpHints: ['모니터링 대시보드 확인 부탁드립니다'],
relatedHints: ['릴리즈 노트 정리해서 올렸습니다']
}
const matchedScore = scoreCandidatePersonalization('모니터링 대시보드 확인 부탁드립니다.', context)
const mismatchedRegisterScore = scoreCandidatePersonalization('그냥 빨리 해봐.', context)
expect(matchedScore).toBeGreaterThan(4)
expect(matchedScore).toBeGreaterThan(mismatchedRegisterScore)
})
})

View file

@ -51,4 +51,10 @@ describe('PersonalNgramModel', () => {
const m = model('하나 둘 셋 넷 다섯', '하나 둘 셋 넷 다섯')
expect(m.predict('하나 ', { maxWords: 2 })?.text).toBe('둘 셋')
})
it('한국어 어절의 조사가 달라져도 어간 보조 바이그램으로 다음 어절을 예측한다', () => {
const m = new PersonalNgramModel()
m.addText('회의록은 내일 공유드릴게요', 3)
expect(m.predict('이번 회의록을 ')).toMatchObject({ text: '내일 공유드릴게요', joint: 'separate' })
})
})

View file

@ -13,12 +13,14 @@ import {
fixByVocabulary,
fixKnownMisspelling,
hasSuspiciousTypo,
isCommonEnglishWord,
mergeTypoCorrections,
hangulToLatin,
isPlausibleTypoFix,
latinToHangul,
parseModelTypoPairs,
planTypoReplacement,
rebaseTypoReplacementPlan,
alignCorrectionToPrefix,
typoRegionStart,
validateModelCorrections
@ -663,4 +665,50 @@ describe('Araso 급 한/영 자동 전환 · 합성어 · 잔류 Shift · URL
deleteCount: 'dkssudgktpdy '.length
})
})
it('생산적 영어 접미사·접두사 파생형과 문장 첫머리의 구문 호응(to be, to the, for the, and the)을 복원한다', () => {
expect(isCommonEnglishWord('implementation')).toBe(true)
expect(isCommonEnglishWord('personalization')).toBe(true)
expect(isCommonEnglishWord('unhappy')).toBe(true)
expect(isCommonEnglishWord('restart')).toBe(true)
expect(isCommonEnglishWord('disconnect')).toBe(true)
expect(isCommonEnglishWord('effectively')).toBe(true)
expect(isCommonEnglishWord('subagents')).toBe(true)
expect(applyAll('ㅑㅡㅔㅣ드둣ㅁ샤ㅐㅜ ')).toBe('implementation ')
expect(applyAll('ㅔㄷㄱ내ㅜ미ㅑㅋㅁ샤ㅐㅜ ')).toBe('personalization ')
expect(applyAll('ㅕㅜㅗ메ㅔㅛ ')).toBe('unhappy ')
expect(applyAll('ㄱㄷㄴㅅㅁㄱㅅ ')).toBe('restart ')
expect(applyAll('녀ㅠㅁㅎ둣ㄴ ')).toBe('subagents ')
// 문장 맨 앞에서도 "새 ㅠㄷ", "새 솓", "새 애 this", "랙 솓", "뭉 솓" 구문 호응을 복원한다
expect(applyAll('새 ㅠㄷ honest ')).toBe('to be honest ')
expect(applyAll('새 솓 point ')).toBe('to the point ')
expect(applyAll('새 애 this ')).toBe('to do this ')
expect(applyAll('랙 솓 team ')).toBe('for the team ')
expect(applyAll('뭉 솓 result ')).toBe('and the result ')
expect(applyAll('좨 ㅑㄴ there ')).toBe('who is there ')
// 한국어 오타 사전 확장 (괜찬아, 있읍니다, 없읍니다, 했읍니다, 금새)
expect(fixKnownMisspelling('괜찬아요')).toBe('괜찮아요')
expect(fixKnownMisspelling('있읍니다')).toBe('있습니다')
expect(fixKnownMisspelling('없읍니다')).toBe('없습니다')
expect(fixKnownMisspelling('했읍니다')).toBe('했습니다')
expect(fixKnownMisspelling('금새')).toBe('금세')
})
it('rebaseTypoReplacementPlan 은 비동기 모델 검사 중 덧붙은 짧은 꼬리 위로 교정 계획을 안전하게 리베이스한다', () => {
const base = planTypoReplacement('ㅡㅁ가둥', [
{ start: 0, end: 4, from: 'ㅡㅁ가둥', to: 'markdown', source: 'model' }
])
expect(base).not.toBeNull()
const rebased = rebaseTypoReplacementPlan(base!, 'ㅡㅁ가둥', 'ㅡㅁ가둥 문서 ', 18)
expect(rebased).toEqual({
deleteCount: 'ㅡㅁ가둥 문서 '.length,
insertText: 'markdown 문서 ',
originalText: 'ㅡㅁ가둥 문서 ',
corrections: base!.corrections
})
expect(rebaseTypoReplacementPlan(base!, 'ㅡㅁ가둥', '전혀다른문장', 18)).toBeNull()
})
})

View file

@ -10,6 +10,7 @@
import type { PhraseSource } from './input-intelligence'
import { countLexicalWords, getWordSegmenter } from './text-segmentation'
import { detectSentenceRegister } from './writing-context'
// 렌더러/프리로드도 같은 계약을 쓴다 (main 모듈을 import 할 수 없다).
export type { PhraseSource }
@ -255,6 +256,74 @@ export function continuationsFrom(
return out
}
const EDGE_PUNCT = /^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu
function normalizeWordStem(rawWord: string): string {
const cleaned = rawWord.normalize('NFC').replace(EDGE_PUNCT, '').toLowerCase()
return cleaned ? stemTerm(cleaned) : ''
}
/**
* 조사가 달라 정확한 부분 문자열(`continuationsFrom`)이 어긋날 때 어간(stem) 열이 일치하는 지점 뒤의 이어쓰기를 뽑는다.
* 예: 저장 문장 "회의록 정리해서 내일 오전까지 공유드리겠습니다" ← 입력 꼬리 "회의록을 정리해서" → "내일 오전까지 공유드리겠습니다"
*/
export function stemContinuationsFrom(
texts: readonly string[],
tail: string,
limit = 3,
maxChars = 60
): string[] {
const rawTailWords = tail
.trim()
.split(/\s+/u)
.map(normalizeWordStem)
.filter((w) => w.length >= 2)
if (rawTailWords.length === 0) return []
const tailStems = rawTailWords.slice(-3)
if (tailStems.length === 1 && tailStems[0].length < 2) return []
const out: string[] = []
for (const text of texts) {
const spans: Array<{ stem: string; end: number }> = []
const wordRegex = /\S+/gu
for (let match = wordRegex.exec(text); match !== null; match = wordRegex.exec(text)) {
const stem = normalizeWordStem(match[0])
if (stem.length > 0) {
spans.push({ stem, end: match.index + match[0].length })
}
}
if (spans.length <= tailStems.length) continue
for (let len = tailStems.length; len >= Math.min(2, tailStems.length); len -= 1) {
const needleStems = tailStems.slice(-len)
for (let i = 0; i <= spans.length - len - 1; i += 1) {
let matched = true
for (let k = 0; k < len; k += 1) {
if (spans[i + k].stem !== needleStems[k]) {
matched = false
break
}
}
if (!matched) continue
const sliceStart = spans[i + len - 1].end
const continuation = text
.slice(sliceStart)
.replace(/^[,.:;!?~\s]+/u, '')
.replace(/\s+/gu, ' ')
.trim()
if (continuation.length < 3) continue
const clipped = continuation.slice(0, maxChars)
if (!out.includes(clipped)) {
out.push(clipped)
if (out.length >= limit) return out
}
break
}
}
}
return out
}
export interface RelatedCandidate {
text: string
terms: readonly string[]
@ -390,3 +459,128 @@ export function buildQueryTerms(parts: ReadonlyArray<{ text: string; weight: num
}
return terms
}
export interface TwoHopGraphEdge {
from: string
to: string
kind: GraphEdgeKind
weight: number
}
/**
* 2-hop 그래프 경로 탐색 (A --shares_terms--> B --follows--> C 또는 A --follows--> B --follows--> C).
*
* 직전 문장(또는 지금 문장)과 직접 이어진 follows 엣지가 없더라도,
* 같은 주제를 공유하는 이웃 문장(shares_terms) 뒤에 실제로 이어 썼던 문장(follows)을
* 경로 가중치와 질의 용어 겹침으로 점수화해 끌어온다.
*/
export function rankTwoHopGraphTargets(params: {
firstHopEdges: readonly TwoHopGraphEdge[]
secondHopFollowEdges: readonly TwoHopGraphEdge[]
queryTerms: ReadonlyMap<string, number>
exclude?: (text: string) => boolean
limit?: number
}): string[] {
const { firstHopEdges, secondHopFollowEdges, queryTerms, exclude, limit = 4 } = params
if (firstHopEdges.length === 0 || secondHopFollowEdges.length === 0) return []
const firstHopScores = new Map<string, number>()
for (const edge of firstHopEdges) {
const kindMultiplier = edge.kind === 'follows' ? 1.5 : 1.0
const score = Math.log2(1 + Math.max(1, edge.weight)) * kindMultiplier
firstHopScores.set(edge.to, Math.max(firstHopScores.get(edge.to) ?? 0, score))
}
const targetScores = new Map<string, number>()
for (const edge of secondHopFollowEdges) {
if (edge.kind !== 'follows') continue
if (exclude?.(edge.to)) continue
const hop1 = firstHopScores.get(edge.from)
if (hop1 === undefined) continue
const hop2 = Math.log2(1 + Math.max(1, edge.weight)) * 1.4
const targetTerms = extractTerms(edge.to, 8)
let termBonus = 0
for (const term of targetTerms) {
termBonus += (queryTerms.get(term) ?? 0) * 0.6
}
const totalScore = hop1 * hop2 + termBonus
targetScores.set(edge.to, (targetScores.get(edge.to) ?? 0) + totalScore)
}
return [...targetScores.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, limit)
.map(([text]) => text)
}
export interface CandidatePersonalizationContext {
prefix: string
continuationHints?: readonly string[]
followUpHints?: readonly string[]
relatedHints?: readonly string[]
phraseHints?: readonly string[]
}
/**
* 생성된 제안 후보가 사용자의 개인 그래프(이어쓰기·직후 문장·관련 문장·자주 쓰는 표현) 및 현재 어투와
* 얼마나 잘 맞는지 점수화한다 (후보 재정렬용).
*/
export function scoreCandidatePersonalization(
candidateText: string,
context: CandidatePersonalizationContext
): number {
const normalizedCandidate = candidateText.replace(/\s+/gu, ' ').trim()
if (!normalizedCandidate) return 0
let score = 0
const candidateTerms = new Set(extractTerms(normalizedCandidate, 10))
for (const hint of context.continuationHints ?? []) {
const normHint = hint.replace(/\s+/gu, ' ').trim()
if (!normHint) continue
if (normalizedCandidate === normHint || normalizedCandidate.startsWith(normHint) || normHint.startsWith(normalizedCandidate)) {
score += 4.0
break
}
}
for (const hint of context.followUpHints ?? []) {
const normHint = hint.replace(/\s+/gu, ' ').trim()
if (!normHint) continue
if (normalizedCandidate === normHint || normalizedCandidate.startsWith(normHint) || normHint.startsWith(normalizedCandidate)) {
score += 3.5
break
}
}
const addOverlapScore = (hints: readonly string[] | undefined, weightPerTerm: number): void => {
if (!hints || hints.length === 0 || candidateTerms.size === 0) return
let matched = 0
for (const hint of hints) {
for (const term of extractTerms(hint, 8)) {
if (candidateTerms.has(term)) matched += 1
}
}
score += Math.min(matched, 5) * weightPerTerm
}
addOverlapScore(context.continuationHints, 1.8)
addOverlapScore(context.followUpHints, 1.6)
addOverlapScore(context.relatedHints, 1.0)
addOverlapScore(context.phraseHints, 0.8)
const prefixRegister = detectSentenceRegister(context.prefix)
const candidateRegister = detectSentenceRegister(normalizedCandidate)
if (prefixRegister && candidateRegister) {
if (prefixRegister === candidateRegister) {
score += 0.8
} else if (
(prefixRegister === 'casual' && (candidateRegister === 'formal' || candidateRegister === 'polite')) ||
((prefixRegister === 'formal' || prefixRegister === 'polite') && candidateRegister === 'casual')
) {
score -= 1.5
}
}
return score
}

View file

@ -12,6 +12,8 @@
// - 지지(같은 문맥에서 나온 횟수)와 비율(문맥 전체 중 그 어절의 몫)이 기준을 넘을 때만 낸다.
// - 확신이 이어지면 최대 maxWords 어절까지 구로 늘린다(트라이그램 → 바이그램 순).
import { stemTerm } from './personal-graph'
export interface PersonalNgramOptions {
/** 같은 문맥 뒤에 이 어절이 나온 최소 횟수 */
minSupport?: number
@ -73,13 +75,13 @@ function tokenize(sentence: string): string[] {
.filter((word) => word.length > 0)
}
function increment(table: Map<string, Map<string, number>>, context: string, word: string): void {
function increment(table: Map<string, Map<string, number>>, context: string, word: string, weight = 1): void {
let followers = table.get(context)
if (!followers) {
followers = new Map()
table.set(context, followers)
}
followers.set(word, (followers.get(word) ?? 0) + 1)
followers.set(word, (followers.get(word) ?? 0) + weight)
}
interface Pick {
@ -104,6 +106,8 @@ function best(followers: Map<string, number> | undefined, accept: (word: string)
export class PersonalNgramModel {
private readonly _bigram = new Map<string, Map<string, number>>()
private readonly _trigram = new Map<string, Map<string, number>>()
/** 한국어 어절의 조사가 달라졌을 때("회의록을" vs "회의록은") 어간 기준으로 다음 어절을 찾는 보조 테이블 */
private readonly _stemBigram = new Map<string, Map<string, number>>()
/** 단어 중간 완성용 — 문맥 없이 어절 빈도 */
private readonly _unigram = new Map<string, number>()
private _texts = 0
@ -112,15 +116,25 @@ export class PersonalNgramModel {
return this._texts
}
addText(text: string): void {
addText(text: string, weight = 1): void {
const safeWeight = Math.max(1, Math.trunc(weight))
this._texts += 1
for (const sentence of text.split(SENTENCE_SPLIT)) {
const words = tokenize(sentence)
for (let index = 0; index < words.length; index += 1) {
const word = words[index]
this._unigram.set(word, (this._unigram.get(word) ?? 0) + 1)
if (index >= 1) increment(this._bigram, words[index - 1], word)
if (index >= 2) increment(this._trigram, `${words[index - 2]} ${words[index - 1]}`, word)
this._unigram.set(word, (this._unigram.get(word) ?? 0) + safeWeight)
if (index >= 1) {
const prev = words[index - 1]
increment(this._bigram, prev, word, safeWeight)
const prevStem = stemTerm(prev)
if (prevStem !== prev && prevStem.length >= 2) {
increment(this._stemBigram, prevStem, word, safeWeight)
}
}
if (index >= 2) {
increment(this._trigram, `${words[index - 2]} ${words[index - 1]}`, word, safeWeight)
}
}
}
}
@ -184,8 +198,14 @@ export class PersonalNgramModel {
if (passes(tri)) return tri
}
if (context.length >= 1) {
const bi = best(this._bigram.get(context[context.length - 1]), accept)
const lastWord = context[context.length - 1]
const bi = best(this._bigram.get(lastWord), accept)
if (passes(bi)) return bi
const lastStem = stemTerm(lastWord)
if (lastStem !== lastWord && lastStem.length >= 2) {
const stemBi = best(this._stemBigram.get(lastStem), accept)
if (passes(stemBi) && stemBi.confidence >= 0.5) return stemBi
}
}
return null
}

View file

@ -18,7 +18,6 @@ import {
finalSoundOfAcronym,
finalSoundOfHangul,
isUrlOrPathRun,
lingersAfterKoreanShift,
normalizeLingeringShift,
particleAgrees,
splitMixedWord
@ -448,80 +447,204 @@ const COMMON_ENGLISH_WORDS: ReadonlySet<string> = new Set(
).split(/\s+/u)
)
/**
* 흔한 영단어인가 (소문자 비교 + 규칙 굴절형 지원).
*
* -s / -es / -ies / -ed / -ied / -ing / -ly / -er / -ers / -est 굴절형은
* 두벌식 한글 음절로 완전히 조합되지 않는 경우(latinToHangul(lower).complete === false)에 한해
* 어간이 상용 영단어면 영단어로 인정한다.
*/
export function isCommonEnglishWord(word: string): boolean {
const lower = word.toLowerCase()
if (COMMON_ENGLISH_WORDS.has(lower)) return true
if (lower.length < 4 || !/^[a-z]+$/u.test(lower)) return false
// 두벌식으로 빈틈없이 상용 음절이 되는 키열(예: 한글 단어 키열)은 굴절 추정으로 영단어 처리하지 않는다.
if (latinToHangul(lower).complete) return false
const EXTENDED_ENGLISH_STEMS: ReadonlySet<string> = new Set(
(
'string strings agent agents subagent subagents prompt prompts context contexts session sessions ' +
'stream streaming vector vectors graph graphs node nodes edge edges index indexed indexing ' +
'rerank ranking score scores weight weights metric metrics latency throughput memory cache cached ' +
'buffer queue worker workers atomic batch chunk chunks embed embedding embeddings inference ' +
'dataset datasets benchmark benchmarks eval evaluate evaluation verify verified validate validation ' +
'schema schemas migration migrations cursor caret selection overlay modal dialog toast banner ' +
'widget panel route routes router routing hook hooks store stores slice props handler handlers ' +
'listener listeners channel channels bridge adapter service services controller provider repository ' +
'factory singleton instance module modules package packages bundle bundler version versions ' +
'patch minor major rebase stash staging canary hotfix bugfix refactor cleanup format linter ' +
'interface interfaces generic generics tuple array object boolean symbol closure promise callback ' +
'awesome amazing perfect wonderful terrible horrible strange funny crazy busy tired hungry ' +
'excited nervous worried confused surprised interested bored annoyed upset glad proud afraid ' +
'scared careful useful helpful powerful comfortable impossible separate obvious accurate correct ' +
'valid regular formal visible stable healthy lucky usual expected connect connection suggest ' +
'suggestion complete completion replace replacement require requirement assign assignment ' +
'implement implementation configure configuration optimize optimization personalize personalization ' +
'normalize custom customize summarize synchronize'
).split(/\s+/u)
)
if (lower.endsWith('ies') && lower.length >= 4 && COMMON_ENGLISH_WORDS.has(`${lower.slice(0, -3)}y`)) {
const ENGLISH_DERIVATIONAL_PREFIXES: readonly string[] = [
'un',
're',
'pre',
'non',
'sub',
'over',
'under',
'out',
'mis',
'dis',
'auto',
'multi',
'micro',
'super',
'inter',
'hyper'
]
function hasEnglishLexiconStem(word: string): boolean {
return COMMON_ENGLISH_WORDS.has(word) || EXTENDED_ENGLISH_STEMS.has(word)
}
function matchesInflectedOrDerivedEnglish(lower: string): boolean {
if (hasEnglishLexiconStem(lower)) return true
if (lower.length < 4) return false
if (lower.endsWith('ies') && lower.length >= 4 && hasEnglishLexiconStem(`${lower.slice(0, -3)}y`)) {
return true
}
if (lower.endsWith('es') && lower.length >= 4) {
if (COMMON_ENGLISH_WORDS.has(lower.slice(0, -2)) || COMMON_ENGLISH_WORDS.has(lower.slice(0, -1))) {
if (hasEnglishLexiconStem(lower.slice(0, -2)) || hasEnglishLexiconStem(lower.slice(0, -1))) {
return true
}
}
if (lower.endsWith('s') && !lower.endsWith('ss') && COMMON_ENGLISH_WORDS.has(lower.slice(0, -1))) {
if (lower.endsWith('s') && !lower.endsWith('ss') && hasEnglishLexiconStem(lower.slice(0, -1))) {
return true
}
if (lower.endsWith('ied') && lower.length >= 4 && COMMON_ENGLISH_WORDS.has(`${lower.slice(0, -3)}y`)) {
if (lower.endsWith('ied') && lower.length >= 4 && hasEnglishLexiconStem(`${lower.slice(0, -3)}y`)) {
return true
}
if (lower.endsWith('ed') && lower.length >= 4) {
if (COMMON_ENGLISH_WORDS.has(lower.slice(0, -2)) || COMMON_ENGLISH_WORDS.has(lower.slice(0, -1))) {
if (hasEnglishLexiconStem(lower.slice(0, -2)) || hasEnglishLexiconStem(lower.slice(0, -1))) {
return true
}
if (
lower.length >= 5 &&
lower[lower.length - 3] === lower[lower.length - 4] &&
COMMON_ENGLISH_WORDS.has(lower.slice(0, -3))
hasEnglishLexiconStem(lower.slice(0, -3))
) {
return true
}
}
if (lower.endsWith('ing') && lower.length >= 5) {
const stem = lower.slice(0, -3)
if (COMMON_ENGLISH_WORDS.has(stem) || COMMON_ENGLISH_WORDS.has(`${stem}e`)) {
if (hasEnglishLexiconStem(stem) || hasEnglishLexiconStem(`${stem}e`)) {
return true
}
if (
stem.length >= 3 &&
stem[stem.length - 1] === stem[stem.length - 2] &&
COMMON_ENGLISH_WORDS.has(stem.slice(0, -1))
hasEnglishLexiconStem(stem.slice(0, -1))
) {
return true
}
}
if (lower.endsWith('ily') && lower.length >= 5 && COMMON_ENGLISH_WORDS.has(`${lower.slice(0, -3)}y`)) {
if (lower.endsWith('ily') && lower.length >= 5 && hasEnglishLexiconStem(`${lower.slice(0, -3)}y`)) {
return true
}
if (lower.endsWith('ly') && lower.length >= 5 && COMMON_ENGLISH_WORDS.has(lower.slice(0, -2))) {
if (lower.endsWith('ly') && lower.length >= 5 && hasEnglishLexiconStem(lower.slice(0, -2))) {
return true
}
if (lower.endsWith('ers') && lower.length >= 5) {
if (COMMON_ENGLISH_WORDS.has(lower.slice(0, -3)) || COMMON_ENGLISH_WORDS.has(lower.slice(0, -2))) {
if (hasEnglishLexiconStem(lower.slice(0, -3)) || hasEnglishLexiconStem(lower.slice(0, -2))) {
return true
}
}
if (lower.endsWith('er') && lower.length >= 4) {
if (COMMON_ENGLISH_WORDS.has(lower.slice(0, -2)) || COMMON_ENGLISH_WORDS.has(lower.slice(0, -1))) {
if (hasEnglishLexiconStem(lower.slice(0, -2)) || hasEnglishLexiconStem(lower.slice(0, -1))) {
return true
}
}
if (lower.endsWith('est') && lower.length >= 5) {
if (COMMON_ENGLISH_WORDS.has(lower.slice(0, -3)) || COMMON_ENGLISH_WORDS.has(lower.slice(0, -2))) {
if (hasEnglishLexiconStem(lower.slice(0, -3)) || hasEnglishLexiconStem(lower.slice(0, -2))) {
return true
}
}
// 생산적 파생 접미사 (-tion, -ation, -ment, -ness, -able, -ible, -ful, -fully, -less, -ive, -ively, -ous, -ously, -al, -ally, -ic, -ically, -ize, -ized, -izing, -or, -ors, -ist, -ism)
const core = lower.endsWith('s') && lower.length >= 6 ? lower.slice(0, -1) : lower
for (const [suffix, restoreE] of [
['ations', true],
['ation', true],
['itions', true],
['ition', true],
['tions', true],
['tion', true],
['sions', true],
['sion', true],
['ments', true],
['ment', true],
['nesses', false],
['ness', false],
['ables', true],
['able', true],
['ably', true],
['ibles', true],
['ible', true],
['fully', false],
['fulness', false],
['ful', false],
['lessly', false],
['lessness', false],
['less', false],
['ively', true],
['ives', true],
['ive', true],
['ously', true],
['ous', true],
['ally', true],
['als', true],
['al', true],
['ically', true],
['ical', true],
['ics', true],
['ic', true],
['izations', true],
['ization', true],
['izing', true],
['ized', true],
['izes', true],
['ize', true],
['ising', true],
['ised', true],
['ises', true],
['ise', true],
['ors', true],
['or', true],
['ists', true],
['ist', true],
['isms', true],
['ism', true]
] as const) {
for (const target of [lower, core]) {
if (!target.endsWith(suffix) || target.length - suffix.length < 3) continue
const stem = target.slice(0, -suffix.length)
if (hasEnglishLexiconStem(stem)) return true
if (restoreE && hasEnglishLexiconStem(`${stem}e`)) return true
if (stem.endsWith('i') && hasEnglishLexiconStem(`${stem.slice(0, -1)}y`)) return true
}
}
return false
}
/**
* 흔한 영단어인가 (소문자 비교 + 규칙 굴절형·파생형 지원).
*
* 굴절·파생 접미사 및 접두사는 두벌식 한글 음절로 완전히 조합되지 않는 경우(latinToHangul(lower).complete === false)에 한해
* 어간이 상용 영단어면 영단어로 인정한다.
*/
export function isCommonEnglishWord(word: string): boolean {
const lower = word.toLowerCase()
if (hasEnglishLexiconStem(lower)) return true
if (lower.length < 4 || !/^[a-z]+$/u.test(lower)) return false
// 두벌식으로 빈틈없이 상용 음절이 되는 키열(예: 한글 단어 키열)은 굴절 추정으로 영단어 처리하지 않는다.
if (latinToHangul(lower).complete) return false
if (matchesInflectedOrDerivedEnglish(lower)) return true
for (const prefix of ENGLISH_DERIVATIONAL_PREFIXES) {
if (lower.startsWith(prefix) && lower.length - prefix.length >= 4) {
const rest = lower.slice(prefix.length)
if (matchesInflectedOrDerivedEnglish(rest)) return true
}
}
return false
}
@ -540,6 +663,8 @@ export interface LayoutDetectOptions {
afterEnglishWord?: boolean
/** 바로 뒤 토큰이 영문(또는 한글 자판으로 친 영문)인가 */
nextIsEnglish?: boolean
/** 바로 뒤 토큰의 복원된 영단어(소문자) — "새 ㅠㄷ"→"to be", "새 솓"→"to the" 등 구문 호응 판별용 */
nextEnglishWord?: string | null
/** 토큰 바로 뒤에 공백·문장부호 등 단어 경계가 이미 확정되었는가 (예: "ㅑ " → "I ") */
hasWordBoundaryAfter?: boolean
/** 슬래시 쌍(ska/du)·약어 조사(L.A.fh)처럼 1음절 변환을 폭넓게 허용할 문맥인가 */
@ -665,7 +790,6 @@ export function detectLatinTypedHangul(
if (syllables.length === 0) return null
if (!syllables.every((char) => isCommonHangulSyllable(char))) return null
const contextTotal = context.hangul + context.latin
const hangulContext =
options.afterKoreanWord === true ||
context.hangul >= 2 ||
@ -1054,17 +1178,27 @@ const STANDALONE_SYLLABLE_ENGLISH: Readonly<Record<string, string>> = {
랴그: 'firm',
랴노: 'fish',
래그: 'form',
래가: 'fork',
햐기: 'girl',
해미: 'goal',
갸초: 'rich',
갸나: 'risk',
개차: 'rock',
애차: 'dock',
냐차: 'sick',
샤차: 'tick',
녀차: 'suck',
셔차: 'tuck',
자노: 'wash',
차노: 'cash',
겨노: 'rush',
노맏: 'shake',
냐후: 'sign',
샛미: 'total',
셔구: 'turn',
츠맏: 'cmake',
녀애: 'sudo',
좨: 'who',
먁: 'air',
앳: 'dot',
므: 'am'
@ -1087,7 +1221,14 @@ const CONTEXTUAL_SYLLABLE_ENGLISH: Readonly<Record<string, string>> = {
새주: 'town',
챠쇼: 'city',
챔초: 'coach',
쳐기: 'curl'
쳐기: 'curl',
뭉: 'and',
랙: 'for',
야노: 'dish',
다노: 'dash',
내차: 'sock',
야나: 'disk',
사나: 'task'
}
/**
@ -1106,6 +1247,97 @@ const ENGLISH_CLAUSE_SINGLE_SYLLABLE: Readonly<Record<string, string>> = {
둥: 'end'
}
/**
* 한국어 관형사 '새'(new)가 절대 수식할 수 없는 영어 동사 원형·대명사·관사.
* 문장 맨 앞이라도 '새' 바로 뒤에 이 단어가 오면("새 ㅠㄷ" → "to be", "새 애" → "to do", "새 솓" → "to the", "새 ㅡㄷ" → "to me")
* 100% 영문 부정사/전치사 'to' 로 복원한다 ("오늘 새 API를" 의 'API를' 은 여기에 없으므로 보호됨).
*/
const TO_FOLLOWING_ENGLISH_WORDS: ReadonlySet<string> = new Set([
'be',
'do',
'go',
'have',
'get',
'make',
'see',
'know',
'take',
'find',
'give',
'tell',
'try',
'ask',
'need',
'feel',
'leave',
'put',
'keep',
'let',
'begin',
'seem',
'help',
'talk',
'start',
'hear',
'run',
'move',
'live',
'hold',
'bring',
'write',
'sit',
'stand',
'lose',
'pay',
'meet',
'learn',
'lead',
'watch',
'follow',
'stop',
'speak',
'read',
'allow',
'add',
'spend',
'grow',
'open',
'walk',
'win',
'offer',
'remember',
'buy',
'wait',
'serve',
'send',
'expect',
'build',
'stay',
'reach',
'remain',
'use',
'work',
'check',
'fix',
'test',
'deploy',
'the',
'me',
'you',
'us',
'them',
'him',
'her',
'it',
'this',
'that',
'my',
'your',
'our',
'their',
'his'
])
const SURROUNDED_SINGLE_SYLLABLE_ENGLISH: Readonly<Record<string, string>> = {
새: 'to',
무: 'an',
@ -1329,7 +1561,19 @@ export function detectHangulTypedLatin(
if (
clauseSingle &&
((options.afterEnglishWord && (options.hasWordBoundaryAfter || options.nextIsEnglish || context.latin >= 3)) ||
(options.nextIsEnglish && context.latin >= 4 && context.latin > context.hangul * 2))
(options.nextIsEnglish && context.latin >= 4 && context.latin > context.hangul * 2) ||
(word === '새' &&
!options.afterKoreanWord &&
options.nextEnglishWord != null &&
TO_FOLLOWING_ENGLISH_WORDS.has(options.nextEnglishWord)) ||
(word === '애' &&
!options.afterKoreanWord &&
options.nextEnglishWord != null &&
(options.nextEnglishWord === 'this' ||
options.nextEnglishWord === 'that' ||
options.nextEnglishWord === 'it' ||
options.nextEnglishWord === 'not' ||
options.nextEnglishWord === 'you')))
) {
return clauseSingle
}
@ -1570,6 +1814,12 @@ const KOREAN_MISSPELLINGS: readonly KoreanMisspellingRule[] = [
{ pattern: /되요$/u, replace: '돼요' },
{ pattern: /되서$/u, replace: '돼서' },
{ pattern: /뵈요$/u, replace: '봬요' },
{ pattern: /^괜찬(?=[아았으은다네지고])/u, replace: '괜찮' },
{ pattern: /있읍니다$/u, replace: '있습니다' },
{ pattern: /없읍니다$/u, replace: '없습니다' },
{ pattern: /했읍니다$/u, replace: '했습니다' },
{ pattern: /됐읍니다$/u, replace: '됐습니다' },
{ pattern: /^금새$/u, replace: '금세' },
{ pattern: /^몇일/u, replace: '며칠' },
{ pattern: /^어떻해/u, replace: '어떡해' },
{ pattern: /^왠만/u, replace: '웬만' },
@ -1702,33 +1952,40 @@ function isIgnored(word: string, ignored: readonly string[] | undefined): boolea
return ignored.some((entry) => entry.toLowerCase() === lower)
}
function looksLikeEnglishOrHangulTypedEnglish(tokenText: string): boolean {
if (/^[A-Za-z']{2,}$/u.test(tokenText) && (isCommonEnglishWord(tokenText) || resolveContraction(tokenText) !== null)) {
return true
function resolveTokenAsEnglishPreview(tokenText: string): string | null {
if (/^[A-Za-z']{2,}$/u.test(tokenText)) {
const resolved = resolveContraction(tokenText)
if (resolved) return resolved.toLowerCase()
if (isCommonEnglishWord(tokenText)) return tokenText.toLowerCase()
}
if (LOOSE_JAMO_TWO_LETTER_ENGLISH[tokenText]) return true
if (
STANDALONE_SYLLABLE_ENGLISH[tokenText] ||
CONTEXTUAL_SYLLABLE_ENGLISH[tokenText] ||
const twoLetter = LOOSE_JAMO_TWO_LETTER_ENGLISH[tokenText]
if (twoLetter) return twoLetter.toLowerCase()
const syllableMapped =
STANDALONE_SYLLABLE_ENGLISH[tokenText] ??
CONTEXTUAL_SYLLABLE_ENGLISH[tokenText] ??
ENGLISH_CLAUSE_SINGLE_SYLLABLE[tokenText]
) {
return true
}
if (syllableMapped) return syllableMapped.toLowerCase()
if (/[ㄱ-ㆎ]/u.test(tokenText)) {
const latin = hangulToLatin(tokenText).replace(/’/gu, "'")
if (latin.includes("'") && (resolveContraction(latin) !== null || findFuzzyEnglishForLooseJamo(latin) !== null)) {
return true
if (latin.includes("'")) {
const resolved = resolveContraction(latin) ?? findFuzzyEnglishForLooseJamo(latin)
if (resolved) return resolved.toLowerCase()
}
if (
/^[A-Za-z]{3,}$/u.test(latin) &&
(isCommonEnglishWord(latin) ||
fixKnownMisspelling(latin) !== null ||
findFuzzyEnglishForLooseJamo(latin) !== null)
) {
return true
if (/^[A-Za-z]{3,}$/u.test(latin)) {
if (isCommonEnglishWord(latin)) return latin.toLowerCase()
const known = fixKnownMisspelling(latin) ?? findFuzzyEnglishForLooseJamo(latin)
if (known) return known.toLowerCase()
}
} else if (/^[가-힣]+$/u.test(tokenText)) {
const hasRare = Array.from(tokenText).some((ch) => !isCommonHangulSyllable(ch))
if (hasRare) {
const latin = hangulToLatin(tokenText)
if (/^[A-Za-z]{3,}$/u.test(latin) && isCommonEnglishWord(latin)) {
return latin.toLowerCase()
}
}
}
return false
return null
}
/**
@ -1813,7 +2070,8 @@ export function detectTypos(prefix: string, options: TypoDetectOptions = {}): Ty
break
}
}
const nextIsEnglish = nextWordToken ? looksLikeEnglishOrHangulTypedEnglish(nextWordToken.text) : false
const nextEnglishWord = nextWordToken ? resolveTokenAsEnglishPreview(nextWordToken.text) : null
const nextIsEnglish = nextEnglishWord !== null
const layout =
detectLatinTypedHangul(token.text, context, {
@ -1825,6 +2083,7 @@ export function detectTypos(prefix: string, options: TypoDetectOptions = {}): Ty
afterKoreanWord,
afterEnglishWord,
nextIsEnglish,
nextEnglishWord,
hasWordBoundaryAfter: /[ \t.,!?;:]/u.test(after),
vocabulary: options.vocabulary
})
@ -2328,6 +2587,33 @@ export function planTypoReplacement(prefix: string, corrections: readonly TypoCo
return { deleteCount: originalText.length, insertText, originalText, corrections: sorted }
}
/**
* 비동기 모델 오타 검사가 도는 동안 사용자가 공백·문장부호나 짧은 후속 어절(1~6자)을 더 쳤을 때
* 원래 교정 계획을 새 접두(currentPrefix) 위로 안전하게 리베이스(rebase)한다.
*/
export function rebaseTypoReplacementPlan(
plan: TypoReplacementPlan,
checkedPrefix: string,
currentPrefix: string,
maxDeleteChars: number = TYPO_REGION_MAX_CHARS
): TypoReplacementPlan | null {
if (currentPrefix === checkedPrefix) {
return plan.deleteCount <= maxDeleteChars ? plan : null
}
if (!currentPrefix.startsWith(checkedPrefix)) return null
const delta = currentPrefix.slice(checkedPrefix.length)
if (delta.length === 0 || delta.length > 6 || !isSafeToBackspace(delta)) return null
const newOriginalText = plan.originalText + delta
const newInsertText = plan.insertText + delta
if (newOriginalText.length > maxDeleteChars) return null
return {
deleteCount: newOriginalText.length,
insertText: newInsertText,
originalText: newOriginalText,
corrections: plan.corrections
}
}
/**
* 교정 뒤 사용자가 원래 단어로 되돌렸는가.
*