// packages/core/__tests__/personal-ngram.test.ts import { describe, expect, it } from 'vitest' import { PersonalNgramModel, predictPersonalContinuation } from '../src/personal-ngram' function model(...texts: string[]): PersonalNgramModel { const built = new PersonalNgramModel() for (const text of texts) built.addText(text) return built } describe('PersonalNgramModel', () => { it('단어를 끝냈으면 자주 이어 쓴 다음 어절과 짧은 구를 낸다', () => { const m = model('링크에서 확인할 수 있어요.', '앱에서 확인할 수 있어요.', '여기서 확인할 수 있어요') expect(m.predict('설정에서 확인할 ')).toMatchObject({ text: '수 있어요', joint: 'separate', support: 3 }) }) it('단어 중간이면 그 어절의 나머지를 붙여 낸다', () => { const m = model('아기를 토닥여 줘요', '조금 토닥여 줬어요', '다시 토닥여 줘요') expect(m.predict('살살 토닥')).toMatchObject({ text: '여 줘요', joint: 'attach' }) }) it('지지 횟수가 모자라거나 갈리면 내지 않는다', () => { expect(model('확인할 수 있어요').predict('확인할 ')).toBeNull() const split = model('그럼 이따가 봐', '그럼 내일 봐', '그럼 다음에 봐', '그럼 이따가 연락할게') expect(split.predict('그럼 ', { minConfidence: 0.6 })).toBeNull() expect(split.predict('그럼 ')).toMatchObject({ text: '이따가', support: 2 }) }) it('문장 경계를 넘지 않고, 새 문장의 첫 어절은 예측하지 않는다', () => { const m = model('좋아요. 내일 봐요', '좋아요. 내일 봐요') expect(m.predict('좋아요. ')).toBeNull() expect(m.predict('좋아요 ')).toBeNull() }) it('따옴표·쉼표는 비교에서 떼고 대소문자를 가리지 않는다', () => { const m = model('Thank you so much', 'thank you, so much') expect(m.predict('Thank ')).toMatchObject({ text: 'you so much' }) }) it('앱 예측: 공백 없이 멈췄어도 늘일 어절이 없으면 다음 어절을 띄어 쓰기로 낸다 ("그리고")', () => { const m = model('그리고 다음에 보자', '그리고 다음에 해', '그리고 오늘은 쉬자') expect(predictPersonalContinuation(m, '아 그리고')).toMatchObject({ text: '다음에', joint: 'separate' }) }) it('앱 예측: 다음 어절이 갈려도 가장 흔한 것 하나는 내고, 둘째 어절은 확신할 때만 붙인다', () => { const m = model('그리고 다음에 보자', '그리고 다음에 해', '그리고 오늘은 쉬자', '그리고 내일 가자') expect(predictPersonalContinuation(m, '그리고 ')?.text).toBe('다음에') }) it('maxWords 까지만 늘인다', () => { const m = model('하나 둘 셋 넷 다섯', '하나 둘 셋 넷 다섯') expect(m.predict('하나 ', { maxWords: 2 })?.text).toBe('둘 셋') }) })