fix(hldp): rank Chinese causal branch queries

This commit is contained in:
冰朔 2026-08-15 15:18:10 +08:00
commit fb1096c5ec
2 changed files with 18 additions and 1 deletions

View file

@ -56,7 +56,18 @@ export function verifyTree(tree) {
}
function terms(query) {
return [...new Set(query.toLowerCase().match(/[\p{L}\p{N}_-]+/gu) ?? [])];
const raw = query.toLowerCase().match(/[\p{L}\p{N}_-]+/gu) ?? [];
const expanded = [];
for (const token of raw) {
expanded.push(token);
const characters = [...token];
if (/\p{Script=Han}/u.test(token) && characters.length > 1) {
for (let i = 0; i < characters.length - 1; i += 1) {
expanded.push(characters.slice(i, i + 2).join(""));
}
}
}
return [...new Set(expanded)];
}
export function route(tree, query, max = 3, from = tree.root) {

View file

@ -48,6 +48,12 @@ test("route returns paths and summaries only, capped at three", () => {
assert(!JSON.stringify(candidates).includes("A失败"));
});
test("route ranks a matching Chinese branch instead of treating a sentence as one token", () => {
const candidates = route(validTree(), "为什么最终方案发生变化", 3);
assert.equal(candidates[0].path, "root/decision");
assert(candidates[0].score > 0);
});
test("read-node reads exactly one bounded node", () => {
const node = readNode(validTree(), "root/decision");
assert.equal(node.path, "root/decision");