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
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue