|
7 | 7 | import { describe, expect, it } from 'vitest'; |
8 | 8 | import type { KnowledgeSource } from '@objectstack/spec/ai'; |
9 | 9 | import { PgvectorKnowledgeAdapter, type PgPoolLike } from '../index.js'; |
10 | | -import { containsOperator, isWordTerm, keywordTerms } from '../sql.js'; |
| 10 | +import { KEYWORD_SEPARATORS, containsOperator, isWordTerm, keywordTerms } from '../sql.js'; |
11 | 11 | import { FakeEmbedder } from './live-pg.testkit.js'; |
12 | 12 |
|
13 | 13 | describe('knowledge-pgvector keywordTerms', () => { |
@@ -41,13 +41,36 @@ describe('knowledge-pgvector keywordTerms', () => { |
41 | 41 | }); |
42 | 42 |
|
43 | 43 | it('keeps 々, 〆 and 〇 inside a run, and still splits at 、 and 。', () => { |
44 | | - // The three marks sit in the CJK symbols block, whose other characters are punctuation. |
| 44 | + // The three marks sit in the CJK symbols block, beside its punctuation. |
45 | 45 | expect(keywordTerms('人々')).toEqual(['人々']); |
46 | 46 | expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']); |
47 | 47 | expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']); |
48 | 48 | expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']); |
49 | 49 | }); |
50 | 50 |
|
| 51 | + it('splits at the punctuation of the CJK symbols block and at none of its letters or numbers', () => { |
| 52 | + // Derived from the runtime's own Unicode tables, not from a list: a member of |
| 53 | + // U+3000–303F separates terms exactly when it is neither a letter nor a number. |
| 54 | + const block = Array.from({ length: 0x40 }, (_, i) => String.fromCodePoint(0x3000 + i)); |
| 55 | + const letterOrNumber = (c: string): boolean => /[\p{L}\p{N}]/u.test(c); |
| 56 | + const separates = (c: string): boolean => `a${c}b`.split(KEYWORD_SEPARATORS).length === 2; |
| 57 | + expect(block.filter(separates)).toEqual(block.filter((c) => !letterOrNumber(c))); |
| 58 | + const letters = block.filter(letterOrNumber); |
| 59 | + expect(letters).toEqual(expect.arrayContaining(['々', '〆', '〇', '〡', '〩', '〱', '〵', '〸', '〺', '〻', '〼'])); |
| 60 | + // Each of them, three in a row, is a term or yields pieces: none is dropped as punctuation. |
| 61 | + for (const c of letters) expect(keywordTerms(c.repeat(3)).join('')).toContain(c); |
| 62 | + // The vertical iteration mark and the Hangzhou numerals are Han: they stay inside a run. |
| 63 | + expect(keywordTerms('人〻')).toEqual(['人〻']); |
| 64 | + expect(keywordTerms('第〢〣号')).toEqual(['第〢〣', '〢〣号', '第〢', '〢〣', '〣号']); |
| 65 | + // 、, 。 and the corner brackets still separate. |
| 66 | + expect(keywordTerms('出差、住宿。「审批」')).toEqual(['出差', '住宿', '审批']); |
| 67 | + }); |
| 68 | + |
| 69 | + it('splits with the same class as the libSQL store', () => { |
| 70 | + // The libSQL store's test pins the same literal against its own class: keep the two pins, and so the two classes, identical. |
| 71 | + expect(KEYWORD_SEPARATORS).toEqual(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u3020\u302a-\u3030\u3036\u3037\u303d-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u); |
| 72 | + }); |
| 73 | + |
51 | 74 | it('matches a piece as a substring and a figure or a code of three characters or more on the tsvector', () => { |
52 | 75 | expect(keywordTerms('住宿费2000元以下').map((t) => [t, isWordTerm(t)])).toEqual([ |
53 | 76 | ['2000', true], ['住宿费', false], ['元以下', false], ['住宿', false], ['宿费', false], ['元以', false], ['以下', false], |
|
0 commit comments