189 lines
No EOL
7.3 KiB
TypeScript
189 lines
No EOL
7.3 KiB
TypeScript
// packages/core/__tests__/personal-graph.test.ts
|
|
// 개인 그래프 순수 함수 테스트 — 용어 추출/문장 분해/관계/순위.
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
SHARES_TERMS_MIN_SIMILARITY,
|
|
buildQueryTerms,
|
|
buildSequenceEdges,
|
|
continuationsFrom,
|
|
extractTerms,
|
|
jaccard,
|
|
rankRelated,
|
|
rankTwoHopGraphTargets,
|
|
scoreCandidatePersonalization,
|
|
splitSentences,
|
|
stemContinuationsFrom
|
|
} from '../src/personal-graph'
|
|
|
|
describe('extractTerms', () => {
|
|
it('소문자화하고 조사/불용어를 걷어낸다', () => {
|
|
const terms = extractTerms('오늘 회의에서 논의한 내용을 정리해서 공유드립니다')
|
|
// 조사·'하다' 활용을 뗀 어간으로 남는다 — "회의에서" 와 "회의는" 이 같은 용어가 된다
|
|
expect(terms).toContain('회의')
|
|
expect(terms).toContain('정리')
|
|
expect(terms).toContain('논의')
|
|
expect(terms).not.toContain('회의에서')
|
|
expect(terms).not.toContain('그리고')
|
|
})
|
|
|
|
it('조사가 달라도 같은 주제의 문장은 용어를 공유한다', () => {
|
|
const a = extractTerms('배포 일정은 금요일로 잡았습니다')
|
|
const b = extractTerms('금요일 배포를 위해 테스트를 끝내야 합니다')
|
|
expect(a).toContain('배포')
|
|
expect(b).toContain('배포')
|
|
expect(a).toContain('금요일')
|
|
expect(b).toContain('금요일')
|
|
})
|
|
|
|
it('남는 어간이 1자면 떼지 않는다 (회의·결과)', () => {
|
|
expect(extractTerms('회의 결과')).toEqual(expect.arrayContaining(['회의', '결과']))
|
|
})
|
|
|
|
it('영어 불용어를 제거한다', () => {
|
|
const terms = extractTerms('Thanks for the quick turnaround on the report')
|
|
expect(terms).toContain('quick')
|
|
expect(terms).toContain('turnaround')
|
|
expect(terms).toContain('report')
|
|
expect(terms).not.toContain('the')
|
|
})
|
|
|
|
it('빈도 순으로 상위 limit 개만 돌려준다', () => {
|
|
const terms = extractTerms('배포 배포 배포 점검 점검 테스트', 2)
|
|
expect(terms).toEqual(['배포', '점검'])
|
|
})
|
|
|
|
it('1자 토큰은 버린다', () => {
|
|
expect(extractTerms('a b c 배포')).toEqual(['배포'])
|
|
})
|
|
})
|
|
|
|
describe('splitSentences', () => {
|
|
it('종결 부호와 개행으로 나누고 짧은 조각은 버린다', () => {
|
|
const sentences = splitSentences('오늘 회의는 여기서 끝. 짧음. 내일 일정을 공유드릴게요!')
|
|
expect(sentences).toContain('오늘 회의는 여기서 끝')
|
|
expect(sentences).toContain('내일 일정을 공유드릴게요')
|
|
expect(sentences).not.toContain('짧음')
|
|
})
|
|
})
|
|
|
|
describe('buildSequenceEdges', () => {
|
|
it('인접한 문장 쌍만 만든다', () => {
|
|
const edges = buildSequenceEdges(['A 문장입니다', 'B 문장입니다', 'C 문장입니다'])
|
|
expect(edges).toEqual([
|
|
{ from: 'A 문장입니다', to: 'B 문장입니다' },
|
|
{ from: 'B 문장입니다', to: 'C 문장입니다' }
|
|
])
|
|
})
|
|
|
|
it('같은 문장이 반복되면 만들지 않는다', () => {
|
|
expect(buildSequenceEdges(['같은 문장입니다', '같은 문장입니다'])).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('jaccard', () => {
|
|
it('겹침 비율을 계산한다', () => {
|
|
expect(jaccard(['a', 'b'], ['a', 'b'])).toBe(1)
|
|
expect(jaccard(['a', 'b'], ['c', 'd'])).toBe(0)
|
|
expect(jaccard(['a', 'b', 'c'], ['a', 'b', 'd'])).toBeCloseTo(0.5, 5)
|
|
})
|
|
|
|
it('빈 집합은 0 이다', () => {
|
|
expect(jaccard([], ['a'])).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('continuationsFrom', () => {
|
|
it('꼬리 뒤에 실제로 이어 쓴 텍스트만 뽑는다', () => {
|
|
const out = continuationsFrom(
|
|
['오늘 회의에서 논의한 내용을 정리해서 공유드립니다', '관계 없는 문장입니다'],
|
|
'논의한 내용을'
|
|
)
|
|
expect(out).toEqual(['정리해서 공유드립니다'])
|
|
})
|
|
|
|
it('꼬리가 너무 짧으면 아무것도 만들지 않는다', () => {
|
|
expect(continuationsFrom(['아무 문장'], '아무')).toEqual([])
|
|
})
|
|
|
|
it('길이를 제한하고 중복을 없앤다', () => {
|
|
const out = continuationsFrom(
|
|
['접두 뒤에 이어지는 아주 긴 문장이 계속 이어집니다 그리고 더 이어집니다', '접두 뒤에 이어지는 아주 긴 문장이 계속 이어집니다'],
|
|
'접두 뒤에',
|
|
2,
|
|
20
|
|
)
|
|
expect(out).toHaveLength(1)
|
|
expect(out[0].length).toBeLessThanOrEqual(20)
|
|
})
|
|
})
|
|
|
|
describe('rankRelated', () => {
|
|
it('관계 가중치와 용어 유사도, 최근성을 함께 본다', () => {
|
|
const now = Date.now()
|
|
const ranked = rankRelated(
|
|
['회의', '일정'],
|
|
[
|
|
{ text: '회의 일정을 공유드립니다', terms: ['회의', '일정'], weight: 0, lastUsedAt: now },
|
|
{ text: '전혀 상관 없는 문장입니다', terms: ['바다', '산'], weight: 0, lastUsedAt: now },
|
|
{ text: '강하게 이어지는 문장입니다', terms: [], weight: 10, lastUsedAt: null }
|
|
],
|
|
3
|
|
)
|
|
// 관련도 순: 강한 엣지 → 용어 유사 + 최근성 → 무관(마지막)
|
|
expect(ranked[0]).toBe('강하게 이어지는 문장입니다')
|
|
expect(ranked[1]).toBe('회의 일정을 공유드립니다')
|
|
expect(ranked[2]).toBe('전혀 상관 없는 문장입니다')
|
|
})
|
|
|
|
it('용어 유사도 임계값 상수는 0과 1 사이이다', () => {
|
|
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)
|
|
})
|
|
}) |