Skip to content

Commit a133c8a

Browse files
fix(knowledge): the keyword leg keeps the letters and numbers of the CJK symbols block inside a term (#38566)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1XDXvJPPtrTGfdDYhWWpihq5)_ Part of #38411. This PR takes item 4 of the card's remaining list (the U+3000–303F nit from the review of the previous slice). Items 2 and 3 (the BrightRule model-graded eval and the larger per-leg pool) stay open on the card. Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:知识检索里,中日文提问中的「〻」、假名重复符「〱」到「〵」、「〼」和苏州码子「〡」到「〩」「〸」到「〺」不再把词切断,比如「人〻」现在能被关键词一路匹配到;其他提问的切词结果不变。 - 为什么做:知识平台专题,上一轮中日韩关键词修复审核时留下的小尾巴,两种存储(本地 libSQL、PostgreSQL)一起改。 - 出错会怎样:最坏情况是含这些罕见符号的提问关键词排序变差,向量一路照常作答;不改索引、不需重建,可直接撤回。 - 要你做什么:无。 ## What changes - `keywordTerms` in `packages/plugins/knowledge-turso/src/sql.ts` and `packages/plugins/knowledge-pgvector/src/sql.ts` split a query at every member of the CJK symbols block except the three marks 々 〆 〇 (class `U+3000–3004, U+3008–303F`). That block also holds 19 more letters and numbers: the Hangzhou numerals U+3021–3029 and U+3038–303A, the kana repeat marks U+3031–3035, U+303B (the vertical iteration mark) and U+303C. Each of them cut the term it sat in, and a run of one character is dropped, so 「人〻」 gave no term. - The split class now takes only the members of the block that are neither a letter nor a number: `U+3000–3004, U+3008–3020, U+302A–3030, U+3036–3037, U+303D–303F`. Everything else in the class is unchanged. - The class moves out of the `split` call into an exported constant `KEYWORD_SEPARATORS`, byte-identical in both files, so the tests can read it. `sql.ts` is not re-exported by either package's entry (`index.ts` names its exports), so the published surface does not change. - The `UNSEGMENTED_RUN` doc comment said the split takes the rest of the block as punctuation. That is no longer true, so the comment now says only why the three marks are named (〆 belongs to no script). - Changeset `.changeset/38411-cjk-symbol-block-letters.md`: patch on both store packages. ## Tests The same two new cases in each store, beside the existing CJK cases (pgvector `keyword-terms.test.ts`; turso `acceptance.test.ts`, which runs on `:memory:` and on a `file:` URL): - **Block probe, from Unicode properties.** For each code point of U+3000–303F, it checks whether `KEYWORD_SEPARATORS` splits `a`, then the code point, then `b`. The code points that split must equal those that fail the Unicode letter-or-number property test in the runtime. The test computes this from the runtime's Unicode tables, with no hand list. Each letter or number, three in a row, must yield a term. Behaviour pins: 「人〻」 gives `人〻`, 「第〢〣号」 gives `第〢〣, 〢〣号, 第〢, 〢〣, 〣号`, and 「出差、住宿。「审批」」 gives `出差, 住宿, 审批`. - **Parity.** Each store pins `KEYWORD_SEPARATORS` against the same regex literal, so the two classes are pinned to one text. <details><summary>Local runs at 082af27</summary> - `pnpm --filter @objectstack/knowledge-pgvector test` (no database): Test Files 5 passed, 8 skipped (live files); Tests 24 passed, 93 skipped. - `pnpm --filter @objectstack/knowledge-pgvector test` on a local PostgreSQL 16.15 with pgvector, `OS_TEST_POSTGRES_URL` set and `OS_EXPECT_LIVE_DIALECT_MATRIX=1`: Test Files 13 passed (13); Tests 117 passed (117). The cluster was stopped and removed afterwards. - `pnpm --filter @objectstack/knowledge-turso test` (libSQL `:memory:` and a local `file:` database): Test Files 4 passed (4); Tests 290 passed (290). - `pnpm --filter @objectstack/knowledge-pgvector typecheck` and the same for turso: exit 0, with `check:test-typecheck` OK for both. - Lock verdict on each run: `VERDICT command-exit 0`. </details> <details><summary>Ablation: the old class restored, the new pins red</summary> `scripts/ablation-replace.mjs` replaced the range `U+3008–3020, U+302A–3030, U+3036–3037, U+303D–303F` with the old `U+3008–303F`, one store at a time (anchor 1 → 0; blob moved), then restored (blob equals `HEAD`, `git diff HEAD` empty): - pgvector `keyword-terms.test.ts`: 2 failed, 6 passed. Failing: the block probe (61 separators against the 42 the Unicode tables name) and the parity pin. - turso `acceptance.test.ts`: 4 failed, 134 passed. Failing: the same two cases, on `:memory:` and on `file:`. </details> ## Acceptance notes - **Count.** The card says 18 code points. The runtime's Unicode tables count 19 letters and numbers in U+3000–303F beyond 々 〆 〇: 9 + 3 Hangzhou numerals, 5 kana repeat marks, U+303B and U+303C. The ablation shows the same count (61 − 42). - **Tone marks.** U+302A–302F are combining tone marks, not letters or numbers, so they still split, as the fix shape names. - **The kana repeat marks and U+303C have no script (Common).** The split no longer cuts at them, but `UNSEGMENTED_RUN` does not name them, so they are not pieced into the run beside them. 「いろ〳〵」 gives `〳〵` (a word) and `いろ` (a piece); before it gave `いろ`. Adding them to the run is outside this PR's file surface (the split class only). This is rare in a typed question, so it is not filed. - **Parity is pinned twice, not compared directly.** A pin that reads the other package's `sql.ts` would need a declaration in `scripts/cross-package-test-inputs.mjs` and in `turbo.json`, and both are outside the claimed file surface. Each store's test therefore pins the same literal. ## Verification - Files to read first, by risk: `packages/plugins/knowledge-turso/src/sql.ts` (the `KEYWORD_SEPARATORS` hunk: the class ranges), `packages/plugins/knowledge-pgvector/src/sql.ts` (the same hunk, byte-identical apart from the store name in the comment), `packages/plugins/knowledge-pgvector/src/__tests__/keyword-terms.test.ts` (the block probe). - Two-minute check: `pnpm --filter @objectstack/knowledge-pgvector exec vitest run --maxWorkers=2 src/__tests__/keyword-terms.test.ts`. Expected result: 8 passed, including "splits at the punctuation of the CJK symbols block and at none of its letters or numbers". - Derived gates (`node scripts/pm/dispatch-gates.mjs --commands`, at 082af27): 67 derived, 66 run with exit 0. `--ran` reconciles: 67 accounted, 0 unrun. NOT MEASURED: `check:dual-build-cjs-loads`, reason: exit 3, PREREQUISITE NOT MET. It needs every package's `dist/`, and this container built only the two stores' closure. CI runs it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent d08d2ae commit a133c8a

5 files changed

Lines changed: 91 additions & 10 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@objectstack/knowledge-turso': patch
3+
'@objectstack/knowledge-pgvector': patch
4+
---
5+
6+
fix(knowledge): the keyword leg keeps the letters and numbers of the CJK symbols block inside a term
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
`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.

‎packages/plugins/knowledge-pgvector/src/__tests__/keyword-terms.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { describe, expect, it } from 'vitest';
88
import type { KnowledgeSource } from '@objectstack/spec/ai';
99
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';
1111
import { FakeEmbedder } from './live-pg.testkit.js';
1212

1313
describe('knowledge-pgvector keywordTerms', () => {
@@ -41,13 +41,36 @@ describe('knowledge-pgvector keywordTerms', () => {
4141
});
4242

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

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+
5174
it('matches a piece as a substring and a figure or a code of three characters or more on the tsvector', () => {
5275
expect(keywordTerms('住宿费2000元以下').map((t) => [t, isWordTerm(t)])).toEqual([
5376
['2000', true], ['住宿费', false], ['元以下', false], ['住宿', false], ['宿费', false], ['元以', false], ['以下', false],

‎packages/plugins/knowledge-pgvector/src/sql.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,23 @@ export const UNSEGMENTED = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana
146146
* A run of characters written without spaces between words: Han, Hiragana,
147147
* Katakana (with the prolonged sound mark, which belongs to no script) and
148148
* Hangul, and the three marks written inside a word in the CJK symbols block,
149-
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script, since
150-
* the split below takes the rest of that block as punctuation.
149+
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script (〆
150+
* belongs to none).
151151
*/
152152
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;
153153

154+
/**
155+
* Where the keyword leg splits a query into terms: white space, punctuation
156+
* that never sits inside a code (see {@link keywordTerms}), the general
157+
* punctuation quotes and the ellipsis, the punctuation of the fullwidth forms,
158+
* and the members of the CJK symbols block (U+3000–303F) that are neither a
159+
* letter nor a number (`\p{L}`, `\p{N}`): 、, 。, the brackets, the tone marks.
160+
* The block's letters and numbers stay inside a term: 々, 〆, 〇, 〻, the kana
161+
* repeat marks 〱–〵, 〼 and the Hangzhou numerals 〡–〩 and 〸–〺.
162+
* The same class as the libSQL store's.
163+
*/
164+
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;
165+
154166
/**
155167
* The terms of a query for the keyword leg: split at white space and at
156168
* punctuation that never sits inside a code (a hyphen, a dot, a slash, an
@@ -177,7 +189,7 @@ export function keywordTerms(query: string): string[] {
177189
const stripped = text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
178190
if (stripped !== '') words.push(stripped);
179191
};
180-
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
192+
for (const term of query.split(KEYWORD_SEPARATORS)) {
181193
let at = 0;
182194
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
183195
word(term.slice(at, run.index));

‎packages/plugins/knowledge-turso/src/__tests__/acceptance.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { AdapterContext, IDataEngine, IEmbedder } from '@objectstack/spec/c
1818
import type { ExecutionContext } from '@objectstack/spec/kernel';
1919
import { KnowledgeService } from '@objectstack/service-knowledge';
2020
import { KNOWLEDGE_REBUILD_REQUIRED, TursoKnowledgeAdapter } from '../index.js';
21-
import { containsPattern, ftsPhrase, keywordTerms } from '../sql.js';
21+
import { KEYWORD_SEPARATORS, containsPattern, ftsPhrase, keywordTerms } from '../sql.js';
2222
import { FakeEmbedder, storeOf, type StoreKind } from './turso.testkit.js';
2323

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

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

1021+
it('splits at the punctuation of the CJK symbols block and at none of its letters or numbers', () => {
1022+
// Derived from the runtime's own Unicode tables, not from a list: a member of
1023+
// U+3000–303F separates terms exactly when it is neither a letter nor a number.
1024+
const block = Array.from({ length: 0x40 }, (_, i) => String.fromCodePoint(0x3000 + i));
1025+
const letterOrNumber = (c: string): boolean => /[\p{L}\p{N}]/u.test(c);
1026+
const separates = (c: string): boolean => `a${c}b`.split(KEYWORD_SEPARATORS).length === 2;
1027+
expect(block.filter(separates)).toEqual(block.filter((c) => !letterOrNumber(c)));
1028+
const letters = block.filter(letterOrNumber);
1029+
expect(letters).toEqual(expect.arrayContaining(['々', '〆', '〇', '〡', '〩', '〱', '〵', '〸', '〺', '〻', '〼']));
1030+
// Each of them, three in a row, is a term or yields pieces: none is dropped as punctuation.
1031+
for (const c of letters) expect(keywordTerms(c.repeat(3)).join('')).toContain(c);
1032+
// The vertical iteration mark and the Hangzhou numerals are Han: they stay inside a run.
1033+
expect(keywordTerms('人〻')).toEqual(['人〻']);
1034+
expect(keywordTerms('第〢〣号')).toEqual(['第〢〣', '〢〣号', '第〢', '〢〣', '〣号']);
1035+
// 、, 。 and the corner brackets still separate.
1036+
expect(keywordTerms('出差、住宿。「审批」')).toEqual(['出差', '住宿', '审批']);
1037+
});
1038+
1039+
it('splits with the same class as the PostgreSQL store', () => {
1040+
// The PostgreSQL store's test pins the same literal against its own class: keep the two pins, and so the two classes, identical.
1041+
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);
1042+
});
1043+
10211044
it('the organization filter, the record scope and the metadata filter hold in the keyword leg too', async () => {
10221045
const adapter = store.adapter();
10231046
const s = source('kw_scope');

‎packages/plugins/knowledge-turso/src/sql.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,23 @@ export const TRIGRAM_MIN_CHARS = 3;
395395
* A run of characters written without spaces between words: Han, Hiragana,
396396
* Katakana (with the prolonged sound mark, which belongs to no script) and
397397
* Hangul, and the three marks written inside a word in the CJK symbols block,
398-
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script, since
399-
* the split below takes the rest of that block as punctuation.
398+
* 々 (U+3005), 〆 (U+3006) and 〇 (U+3007), named whatever their script (〆
399+
* belongs to none).
400400
*/
401401
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;
402402

403+
/**
404+
* Where the keyword leg splits a query into terms: white space, punctuation
405+
* that never sits inside a code (see {@link keywordTerms}), the general
406+
* punctuation quotes and the ellipsis, the punctuation of the fullwidth forms,
407+
* and the members of the CJK symbols block (U+3000–303F) that are neither a
408+
* letter nor a number (`\p{L}`, `\p{N}`): 、, 。, the brackets, the tone marks.
409+
* The block's letters and numbers stay inside a term: 々, 〆, 〇, 〻, the kana
410+
* repeat marks 〱–〵, 〼 and the Hangzhou numerals 〡–〩 and 〸–〺.
411+
* The same class as the PostgreSQL store's.
412+
*/
413+
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;
414+
403415
/**
404416
* The terms of a query for the keyword leg: split at white space and at
405417
* punctuation that never sits inside a code (a hyphen, a dot, a slash, an
@@ -425,7 +437,7 @@ export function keywordTerms(query: string): string[] {
425437
const stripped = text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
426438
if (stripped !== '') words.push(stripped);
427439
};
428-
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
440+
for (const term of query.split(KEYWORD_SEPARATORS)) {
429441
let at = 0;
430442
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
431443
word(term.slice(at, run.index));

0 commit comments

Comments
 (0)