From 482ebf57b5e66b69ac413ea0b87edf11188c4c0e Mon Sep 17 00:00:00 2001 From: yunyingk Date: Fri, 2 Oct 2026 13:02:54 +0800 Subject: [PATCH] Improve Chinese enterprise search segmentation and ranking --- src/cli/commands/corps.test.ts | 26 ++++++++++++++++++++++++ src/core/search/fuzzy.test.ts | 16 +++++++++++++++ src/core/search/fuzzy.ts | 37 +++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/cli/commands/corps.test.ts b/src/cli/commands/corps.test.ts index 269f267..7a0ad7d 100644 --- a/src/cli/commands/corps.test.ts +++ b/src/cli/commands/corps.test.ts @@ -349,6 +349,32 @@ describe("corps get and search", () => { expect(data.source.tokenMasked).toContain("..."); }); + test("corps search ranks a Chinese name typo above industry-only matches", async () => { + const enterprises = setupConfigEnv(); + mkdirSync(enterprises, { recursive: true }); + for (const [id, name] of [ + ["corp-yutai", "钰泰半导体股份有限公司"], + ["corp-other", "华芯半导体股份有限公司"] + ]) { + writeFileSync(join(enterprises, `${id}.json`), JSON.stringify({ + id, + name, + seal: { url: `https://${id}.seal.test` }, + source: { type: "hose", appKey: "fixture-key", appSecurity: "fixture-secret", corpId: id, staffId: "fixture-staff" }, + auth: { refreshTtl: 300 } + })); + } + + for (const query of ["瑞泰半导体", "瑞泰 半导体"]) { + const result = await runCli(["corps", "search", query]); + expect(result.exitCode).toBe(0); + expect(JSON.parse(result.stdout).map((corp: { id: string }) => corp.id)).toEqual(["corp-yutai"]); + } + const strict = await runCli(["corps", "search", "瑞泰半导体", "--strict"]); + expect(strict.exitCode).toBe(0); + expect(JSON.parse(strict.stdout)).toEqual([]); + }); + test("corps search filters across name, staffId, environment, and keywords", async () => { const enterprises = setupConfigEnv(); mkdirSync(enterprises, { recursive: true }); diff --git a/src/core/search/fuzzy.test.ts b/src/core/search/fuzzy.test.ts index 9e630c1..b3d3e97 100644 --- a/src/core/search/fuzzy.test.ts +++ b/src/core/search/fuzzy.test.ts @@ -238,5 +238,21 @@ describe("fuzzy search utilities", () => { expect(results).toEqual([]); }); + + test("matches Chinese name and industry words despite a contextual name typo", () => { + const target = makeCorp("corp-yutai", "钰泰半导体股份有限公司"); + const sameIndustry = makeCorp("corp-other", "华芯半导体股份有限公司"); + for (const query of ["瑞泰半导体", "瑞泰 半导体", "半导体 瑞泰", "钰泰 半导体"]) { + expect(filterCorpsFuzzy([sameIndustry, target], query).map((corp) => corp.id)) + .toEqual([target.id]); + } + }); + + test("requires another matching word before tolerating a typo in a short Chinese name", () => { + const target = makeCorp("corp-yutai", "钰泰半导体股份有限公司"); + expect(filterCorpsFuzzy([target], "瑞泰")).toEqual([]); + expect(filterCorpsFuzzy([target], "瑞泰 半导体", { strict: true })).toEqual([]); + expect(filterCorpsFuzzy([target], "钰泰半导体", { strict: true })).toEqual([target]); + }); }); }); diff --git a/src/core/search/fuzzy.ts b/src/core/search/fuzzy.ts index e8d5528..5a3eb7e 100644 --- a/src/core/search/fuzzy.ts +++ b/src/core/search/fuzzy.ts @@ -148,11 +148,46 @@ function searchTerms(value: string): string[] { }); } +let wordSegmenter: Intl.Segmenter | undefined; + +function searchWords(value: string): string[] { + wordSegmenter ??= new Intl.Segmenter("zh", { granularity: "word" }); + const words: string[] = []; + let nameChars = ""; + const flushName = () => { + if (nameChars) words.push(nameChars); + nameChars = ""; + }; + + for (const part of wordSegmenter.segment(value.trim().toLowerCase())) { + // Uncommon enterprise names are often segmented into individual characters. + if (part.isWordLike && /^[\u4e00-\u9fa5]$/.test(part.segment)) { + nameChars += part.segment; + } else { + flushName(); + if (part.isWordLike) words.push(part.segment); + } + } + flushName(); + return words; +} + function matchTokenizedQuery(query: string, target: string): MatchScoreResult { if (!/[\u4e00-\u9fa5]/.test(query)) { return { matched: false, score: 0, reason: "none" }; } + const queryWords = searchWords(query); + const wordMatches = queryWords.map((word) => fuzzyMatchString(word, target).matched); + if (queryWords.length >= 2 && wordMatches.filter(Boolean).length >= queryWords.length - 1) { + const allWordsMatch = queryWords.every((word, index) => wordMatches[index] || ( + // A second matching word supplies context for a typo in a two-character + // name, e.g. "瑞泰 半导体" -> "钰泰半导体". Single-word searches stay conservative. + /^[\u4e00-\u9fa5]{2}$/.test(word) && minSubstringLevenshtein(word, target) <= 1 + )); + if (allWordsMatch) return { matched: true, score: 0.76, reason: "tokens" }; + } + const queryTerms = searchTerms(query); if (queryTerms.length < 2) return { matched: false, score: 0, reason: "none" }; @@ -161,7 +196,7 @@ function matchTokenizedQuery(query: string, target: string): MatchScoreResult { const coverage = matchedTerms.length / queryTerms.length; return { matched: true, - score: 0.56 + coverage * 0.2, + score: queryWords.length >= 2 ? Math.min(0.64, 0.56 + coverage * 0.2) : 0.56 + coverage * 0.2, reason: "tokens" }; }