Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/38411-cjk-symbol-block-letters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@objectstack/knowledge-turso': patch
'@objectstack/knowledge-pgvector': patch
---

fix(knowledge): the keyword leg keeps the letters and numbers of the CJK symbols block inside a term

Clause-②: no
Metadata-protocol: none

`keywordTerms` in `@objectstack/knowledge-turso` and `@objectstack/knowledge-pgvector` split a query at every character of the CJK symbols block (U+3000–303F) except 々, 〆 and 〇. The block also holds other letters and numbers: the vertical iteration mark 〻 (U+303B), the kana repeat marks 〱–〵 (U+3031–3035), the mark 〼 (U+303C) and the Hangzhou numerals 〡–〩 and 〸–〺 (U+3021–3029, U+3038–303A). Each of them cut the term it sat in, and a run of one character is dropped, so 「人〻」 gave no keyword term. Now only the members of the block that are neither a letter nor a number split terms: 「人〻」 is one piece, and 「第〢〣号」 gives 「第〢〣」, 「〢〣号」 and the pieces of two. 「、」, 「。」 and the brackets still split terms. The two stores keep one identical split class. Matching is unchanged. No index or fingerprint change, no reindex.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { describe, expect, it } from 'vitest';
import type { KnowledgeSource } from '@objectstack/spec/ai';
import { PgvectorKnowledgeAdapter, type PgPoolLike } from '../index.js';
import { containsOperator, isWordTerm, keywordTerms } from '../sql.js';
import { KEYWORD_SEPARATORS, containsOperator, isWordTerm, keywordTerms } from '../sql.js';
import { FakeEmbedder } from './live-pg.testkit.js';

describe('knowledge-pgvector keywordTerms', () => {
Expand Down Expand Up @@ -41,13 +41,36 @@ describe('knowledge-pgvector keywordTerms', () => {
});

it('keeps 々, 〆 and 〇 inside a run, and still splits at 、 and 。', () => {
// The three marks sit in the CJK symbols block, whose other characters are punctuation.
// The three marks sit in the CJK symbols block, beside its punctuation.
expect(keywordTerms('人々')).toEqual(['人々']);
expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']);
expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']);
expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']);
});

it('splits at the punctuation of the CJK symbols block and at none of its letters or numbers', () => {
// Derived from the runtime's own Unicode tables, not from a list: a member of
// U+3000–303F separates terms exactly when it is neither a letter nor a number.
const block = Array.from({ length: 0x40 }, (_, i) => String.fromCodePoint(0x3000 + i));
const letterOrNumber = (c: string): boolean => /[\p{L}\p{N}]/u.test(c);
const separates = (c: string): boolean => `a${c}b`.split(KEYWORD_SEPARATORS).length === 2;
expect(block.filter(separates)).toEqual(block.filter((c) => !letterOrNumber(c)));
const letters = block.filter(letterOrNumber);
expect(letters).toEqual(expect.arrayContaining(['々', '〆', '〇', '〡', '〩', '〱', '〵', '〸', '〺', '〻', '〼']));
// Each of them, three in a row, is a term or yields pieces: none is dropped as punctuation.
for (const c of letters) expect(keywordTerms(c.repeat(3)).join('')).toContain(c);
// The vertical iteration mark and the Hangzhou numerals are Han: they stay inside a run.
expect(keywordTerms('人〻')).toEqual(['人〻']);
expect(keywordTerms('第〢〣号')).toEqual(['第〢〣', '〢〣号', '第〢', '〢〣', '〣号']);
// 、, 。 and the corner brackets still separate.
expect(keywordTerms('出差、住宿。「审批」')).toEqual(['出差', '住宿', '审批']);
});

it('splits with the same class as the libSQL store', () => {
// The libSQL store's test pins the same literal against its own class: keep the two pins, and so the two classes, identical.
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);
});

it('matches a piece as a substring and a figure or a code of three characters or more on the tsvector', () => {
expect(keywordTerms('住宿费2000元以下').map((t) => [t, isWordTerm(t)])).toEqual([
['2000', true], ['住宿费', false], ['元以下', false], ['住宿', false], ['宿费', false], ['元以', false], ['以下', false],
Expand Down
18 changes: 15 additions & 3 deletions packages/plugins/knowledge-pgvector/src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,23 @@ export const UNSEGMENTED = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana
* A run of characters written without spaces between words: Han, Hiragana,
* Katakana (with the prolonged sound mark, which belongs to no script) and
* Hangul, and the three marks written inside a word in the CJK symbols block,
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script, since
* the split below takes the rest of that block as punctuation.
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script (〆
* belongs to none).
*/
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;

/**
* Where the keyword leg splits a query into terms: white space, punctuation
* that never sits inside a code (see {@link keywordTerms}), the general
* punctuation quotes and the ellipsis, the punctuation of the fullwidth forms,
* and the members of the CJK symbols block (U+3000–303F) that are neither a
* letter nor a number (`\p{L}`, `\p{N}`): 、, 。, the brackets, the tone marks.
* The block's letters and numbers stay inside a term: 々, 〆, 〇, 〻, the kana
* repeat marks 〱–〵, 〼 and the Hangzhou numerals 〡–〩 and 〸–〺.
* The same class as the libSQL store's.
*/
export const KEYWORD_SEPARATORS = /[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u3020\u302a-\u3030\u3036\u3037\u303d-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u;

/**
* The terms of a query for the keyword leg: split at white space and at
* punctuation that never sits inside a code (a hyphen, a dot, a slash, an
Expand All @@ -177,7 +189,7 @@ export function keywordTerms(query: string): string[] {
const stripped = text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
if (stripped !== '') words.push(stripped);
};
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
for (const term of query.split(KEYWORD_SEPARATORS)) {
let at = 0;
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
word(term.slice(at, run.index));
Expand Down
27 changes: 25 additions & 2 deletions packages/plugins/knowledge-turso/src/__tests__/acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { AdapterContext, IDataEngine, IEmbedder } from '@objectstack/spec/c
import type { ExecutionContext } from '@objectstack/spec/kernel';
import { KnowledgeService } from '@objectstack/service-knowledge';
import { KNOWLEDGE_REBUILD_REQUIRED, TursoKnowledgeAdapter } from '../index.js';
import { containsPattern, ftsPhrase, keywordTerms } from '../sql.js';
import { KEYWORD_SEPARATORS, containsPattern, ftsPhrase, keywordTerms } from '../sql.js';
import { FakeEmbedder, storeOf, type StoreKind } from './turso.testkit.js';

const source = (id: string, extra: Record<string, unknown> = {}): KnowledgeSource =>
Expand Down Expand Up @@ -1011,13 +1011,36 @@ for (const kind of ['memory', 'file'] as StoreKind[]) {
});

it('keeps 々, 〆 and 〇 inside a run, and still splits at 、 and 。', () => {
// The three marks sit in the CJK symbols block, whose other characters are punctuation.
// The three marks sit in the CJK symbols block, beside its punctuation.
expect(keywordTerms('人々')).toEqual(['人々']);
expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']);
expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']);
expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']);
});

it('splits at the punctuation of the CJK symbols block and at none of its letters or numbers', () => {
// Derived from the runtime's own Unicode tables, not from a list: a member of
// U+3000–303F separates terms exactly when it is neither a letter nor a number.
const block = Array.from({ length: 0x40 }, (_, i) => String.fromCodePoint(0x3000 + i));
const letterOrNumber = (c: string): boolean => /[\p{L}\p{N}]/u.test(c);
const separates = (c: string): boolean => `a${c}b`.split(KEYWORD_SEPARATORS).length === 2;
expect(block.filter(separates)).toEqual(block.filter((c) => !letterOrNumber(c)));
const letters = block.filter(letterOrNumber);
expect(letters).toEqual(expect.arrayContaining(['々', '〆', '〇', '〡', '〩', '〱', '〵', '〸', '〺', '〻', '〼']));
// Each of them, three in a row, is a term or yields pieces: none is dropped as punctuation.
for (const c of letters) expect(keywordTerms(c.repeat(3)).join('')).toContain(c);
// The vertical iteration mark and the Hangzhou numerals are Han: they stay inside a run.
expect(keywordTerms('人〻')).toEqual(['人〻']);
expect(keywordTerms('第〢〣号')).toEqual(['第〢〣', '〢〣号', '第〢', '〢〣', '〣号']);
// 、, 。 and the corner brackets still separate.
expect(keywordTerms('出差、住宿。「审批」')).toEqual(['出差', '住宿', '审批']);
});

it('splits with the same class as the PostgreSQL store', () => {
// The PostgreSQL store's test pins the same literal against its own class: keep the two pins, and so the two classes, identical.
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);
});

it('the organization filter, the record scope and the metadata filter hold in the keyword leg too', async () => {
const adapter = store.adapter();
const s = source('kw_scope');
Expand Down
18 changes: 15 additions & 3 deletions packages/plugins/knowledge-turso/src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,23 @@ export const TRIGRAM_MIN_CHARS = 3;
* A run of characters written without spaces between words: Han, Hiragana,
* Katakana (with the prolonged sound mark, which belongs to no script) and
* Hangul, and the three marks written inside a word in the CJK symbols block,
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script, since
* the split below takes the rest of that block as punctuation.
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script (〆
* belongs to none).
*/
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;

/**
* Where the keyword leg splits a query into terms: white space, punctuation
* that never sits inside a code (see {@link keywordTerms}), the general
* punctuation quotes and the ellipsis, the punctuation of the fullwidth forms,
* and the members of the CJK symbols block (U+3000–303F) that are neither a
* letter nor a number (`\p{L}`, `\p{N}`): 、, 。, the brackets, the tone marks.
* The block's letters and numbers stay inside a term: 々, 〆, 〇, 〻, the kana
* repeat marks 〱–〵, 〼 and the Hangzhou numerals 〡–〩 and 〸–〺.
* The same class as the PostgreSQL store's.
*/
export const KEYWORD_SEPARATORS = /[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u3020\u302a-\u3030\u3036\u3037\u303d-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u;

/**
* The terms of a query for the keyword leg: split at white space and at
* punctuation that never sits inside a code (a hyphen, a dot, a slash, an
Expand All @@ -425,7 +437,7 @@ export function keywordTerms(query: string): string[] {
const stripped = text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
if (stripped !== '') words.push(stripped);
};
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
for (const term of query.split(KEYWORD_SEPARATORS)) {
let at = 0;
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
word(term.slice(at, run.index));
Expand Down
Loading