feat(input): upgrade graph personalization, english morphology detection, and atomic SendInput replacement
This commit is contained in:
parent
52eef21bab
commit
cd4a452148
21 changed files with 1098 additions and 110 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue