Improve Chinese enterprise search segmentation and ranking

This commit is contained in:
gitea committed 2026-10-02 13:02:54 +08:00
1 parent ada91c6c76
commit 482ebf57b5
3 files changed
+78 -1

No files matched your search

+26
View File
@@ -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 });
+16
View File
@@ -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]);
});
});
});
+36 -1
View File
@@ -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"
};
}