Skip to content
Merged
11 changes: 11 additions & 0 deletions .changeset/38411-cjk-separator-marks.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 々, 〆 and 〇 inside an unspaced run

Clause-②: no
Metadata-protocol: none

`keywordTerms` in `@objectstack/knowledge-turso` and `@objectstack/knowledge-pgvector` split a query at the whole CJK symbols block, so the iteration mark 々 (U+3005), the closing mark 〆 (U+3006) and the ideographic zero 〇 (U+3007) cut a run of Han text. With a run of one character dropped, 「人々」 gave no keyword term, and 「二〇二四年」 lost the pieces holding its first figure. The three marks now belong to an unspaced run: 「人々」 is one piece, 「二〇二四年」 gives 「二〇二」, 「〇二四」 and the rest. 「、」, 「。」 and the brackets still split terms. Matching is unchanged. On libSQL a piece is matched by the trigram index (three characters) or `LIKE` (two). On PostgreSQL a piece is matched as a substring, by `LIKE` when it holds no cased letter and by `ILIKE` otherwise. A piece made only of 〆 also counts as a substring term there, never as a `tsvector` word. No index or fingerprint change, no reindex.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ describe('knowledge-pgvector keywordTerms', () => {
expect(keywordTerms('휴가는')).toEqual(['휴가는', '휴가', '가는']);
});

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.
expect(keywordTerms('人々')).toEqual(['人々']);
expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']);
expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']);
expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']);
});

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],
]);
// A piece holding only 〆, which belongs to no script, is still a substring term, never a tsvector word.
expect(isWordTerm('〆〆〆')).toBe(false);
});

it('matches a substring term without a cased letter by LIKE, and one holding a cased letter by ILIKE', () => {
Expand Down
15 changes: 9 additions & 6 deletions packages/plugins/knowledge-pgvector/src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,20 @@ export const WORD_MIN_CHARS = 3;

/**
* A character the `simple` parser does not split a run of text at: Han,
* Hiragana, Katakana and Hangul. A run of them is one `tsvector` word, so a
* term holding one is matched as a substring ({@link containsOperator}), never as a word.
* Hiragana, Katakana and Hangul, and the marks 々, 〆 and 〇 a run holds. A run
* of them is one `tsvector` word, so a term holding one is matched as a
* substring ({@link containsOperator}), never as a word.
*/
export const UNSEGMENTED = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
export const UNSEGMENTED = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007]/u;

/**
* A run of characters written without spaces between words: Han, Hiragana,
* Katakana (with the prolonged sound mark, which belongs to no script) and
* Hangul.
* 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.
*/
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u30fc\uff70]+/gu;
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;

/**
* The terms of a query for the keyword leg: split at white space and at
Expand Down Expand Up @@ -174,7 +177,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-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
let at = 0;
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
word(term.slice(at, run.index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,14 @@ for (const kind of ['memory', 'file'] as StoreKind[]) {
expect(keywordTerms('휴가는')).toEqual(['휴가는', '휴가', '가는']);
});

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.
expect(keywordTerms('人々')).toEqual(['人々']);
expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']);
expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']);
expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']);
});

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
8 changes: 5 additions & 3 deletions packages/plugins/knowledge-turso/src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,11 @@ 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.
* 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.
*/
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u30fc\uff70]+/gu;
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;

/**
* The terms of a query for the keyword leg: split at white space and at
Expand All @@ -423,7 +425,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-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
let at = 0;
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
word(term.slice(at, run.index));
Expand Down
Loading