Skip to content

Commit 688fddb

Browse files
fix(knowledge): the keyword leg keeps 々, 〆 and 〇 inside an unspaced run (#38498)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1XJFhX5XfWT8anspjCw9gfjz)_ Part of #38411. This PR is the follow-up to #38469: the separator marks found in its review. Still open on the card: the BrightRule model-graded eval and Ask 3. Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:中文、日文提问里的「々」「〆」「〇」不再把词切断,「人々」「二〇二四年」这类写法在知识检索的关键词一路能被匹配到;其他提问不变。 - 为什么做:知识平台专题,上一个中日韩关键词改动在审核中发现的遗漏。 - 出错会怎样:最坏情况是含这三个符号的提问关键词排序变差,向量一路照常作答;不改索引、不需重建,可直接撤回。 - 要你做什么:无。 ## What changes - `keywordTerms` in `knowledge-turso/src/sql.ts` and `knowledge-pgvector/src/sql.ts` (one rule, kept identical) split a query at the whole CJK symbols block (` -〿`). That block also holds three marks written inside a word: 々 (U+3005), 〆 (U+3006) and 〇 (U+3007). A mark split the run it sat in, and a run of one character is then dropped, so 「人々」 gave no term and 「二〇二四年」 lost its first pieces. The split class is now ` -〄〈-〿`, and the three marks are named in `UNSEGMENTED_RUN`, whatever their script: 々 and 〇 are Han, 〆 is Common. 「、」, 「。」 and the brackets still split. - pgvector's `UNSEGMENTED` (which keeps a term off the `tsvector`) gains the same three marks, so a piece made only of 〆 is matched as a substring, not as a word. - New changeset `.changeset/38411-cjk-separator-marks.md` (patch, both stores). The #38469 changeset is left as main has it. ## Tests <details><summary>Pins, ablation, type check</summary> - New pin in both stores: 「人々」 gives `['人々']`; 「二〇二四年」 gives `['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']`; 「〆切日」 gives `['〆切日', '〆切', '切日']`; 「出差、住宿。审批」 gives `['出差', '住宿', '审批']`. pgvector also pins `isWordTerm('〆〆〆') === false`. - knowledge-turso: `Test Files 4 passed (4)`, `Tests 286 passed (286)`. knowledge-pgvector without a database: `Test Files 4 passed | 7 skipped (11)`, `Tests 20 passed | 91 skipped (111)`. Type check of both: green. One run under the verify lock: `VERDICT command-exit 0`. - Ablation: both `sql.ts` set to the merge base (blob hashes checked), the pins run, then both restored and their hashes checked again. turso: `Tests 2 failed` (the new pin, on `:memory:` and `file:`). pgvector: `Tests 2 failed | 4 passed (6)` (the new pin and the `〆〆〆` pin). - NOT MEASURED: the live pgvector suite, reason: no PostgreSQL in this container for this follow-up. CI covers it. </details> ## Verification - Read first: `packages/plugins/knowledge-turso/src/sql.ts` (`UNSEGMENTED_RUN` and the split class in `keywordTerms`). - Two-minute check: `pnpm --filter @objectstack/knowledge-turso exec vitest run --maxWorkers=2 src/__tests__/acceptance.test.ts -t 'keeps 々'` should show the pin passing twice. <details><summary>Local gate subset at 86a724b (CI runs the full farm)</summary> - `node scripts/pm/dispatch-gates.mjs --commands` derived 66 families for the 5 changed files. Each was run with its exit code captured before any pipe: **62 exit 0**. `--ran` reconciliation: `66 derived, 62 run, 2 NOT-MEASURED, 2 UNRUN`. - NOT MEASURED: `check-plugin-teardown-shape.mjs --self-test` and `check:dual-build-cjs-loads`, reason: exit 3, PREREQUISITE NOT MET (shallow clone; not every package has a `dist/`). - NOT MEASURED: `check:query-options-erasure` and `check:type-check-debt`, reason: repo-wide scans stopped by the 240 s per-gate limit (exit 124). CI's Lint & Repo Gates and TypeScript Type Check run them. - `overlap-check`: `VERDICT clean exit 0` against origin/main 430bbbf (no open PR shares a file). - Self-scan of the changed files for control bytes: no hits. </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01FEkQ5pfJqySEaVD3CKvevm <!-- signed: repo:objectstack#5 · os-project-manager · session_01FEkQ5pfJqySEaVD3CKvevm --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 97fad97 commit 688fddb

5 files changed

Lines changed: 43 additions & 9 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 々, 〆 and 〇 inside an unspaced run
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
`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.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,20 @@ describe('knowledge-pgvector keywordTerms', () => {
4040
expect(keywordTerms('휴가는')).toEqual(['휴가는', '휴가', '가는']);
4141
});
4242

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.
45+
expect(keywordTerms('人々')).toEqual(['人々']);
46+
expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']);
47+
expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']);
48+
expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']);
49+
});
50+
4351
it('matches a piece as a substring and a figure or a code of three characters or more on the tsvector', () => {
4452
expect(keywordTerms('住宿费2000元以下').map((t) => [t, isWordTerm(t)])).toEqual([
4553
['2000', true], ['住宿费', false], ['元以下', false], ['住宿', false], ['宿费', false], ['元以', false], ['以下', false],
4654
]);
55+
// A piece holding only 〆, which belongs to no script, is still a substring term, never a tsvector word.
56+
expect(isWordTerm('〆〆〆')).toBe(false);
4757
});
4858

4959
it('matches a substring term without a cased letter by LIKE, and one holding a cased letter by ILIKE', () => {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,20 @@ export const WORD_MIN_CHARS = 3;
136136

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

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

151154
/**
152155
* The terms of a query for the keyword leg: split at white space and at
@@ -174,7 +177,7 @@ export function keywordTerms(query: string): string[] {
174177
const stripped = text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
175178
if (stripped !== '') words.push(stripped);
176179
};
177-
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
180+
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
178181
let at = 0;
179182
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
180183
word(term.slice(at, run.index));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,14 @@ for (const kind of ['memory', 'file'] as StoreKind[]) {
10101010
expect(keywordTerms('휴가는')).toEqual(['휴가는', '휴가', '가는']);
10111011
});
10121012

1013+
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.
1015+
expect(keywordTerms('人々')).toEqual(['人々']);
1016+
expect(keywordTerms('二〇二四年')).toEqual(['二〇二', '〇二四', '二四年', '二〇', '〇二', '二四', '四年']);
1017+
expect(keywordTerms('〆切日')).toEqual(['〆切日', '〆切', '切日']);
1018+
expect(keywordTerms('出差、住宿。审批')).toEqual(['出差', '住宿', '审批']);
1019+
});
1020+
10131021
it('the organization filter, the record scope and the metadata filter hold in the keyword leg too', async () => {
10141022
const adapter = store.adapter();
10151023
const s = source('kw_scope');

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,11 @@ export const TRIGRAM_MIN_CHARS = 3;
394394
/**
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
397-
* Hangul.
397+
* 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.
398400
*/
399-
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u30fc\uff70]+/gu;
401+
const UNSEGMENTED_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3005-\u3007\u30fc\uff70]+/gu;
400402

401403
/**
402404
* The terms of a query for the keyword leg: split at white space and at
@@ -423,7 +425,7 @@ export function keywordTerms(query: string): string[] {
423425
const stripped = text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
424426
if (stripped !== '') words.push(stripped);
425427
};
426-
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
428+
for (const term of query.split(/[\s,;:!?()[\]{}<>"'`|\\=*~^$%&\u3000-\u3004\u3008-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2018-\u201f\u2026]+/u)) {
427429
let at = 0;
428430
for (const run of term.matchAll(UNSEGMENTED_RUN)) {
429431
word(term.slice(at, run.index));

0 commit comments

Comments
 (0)