-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplify_loader.cjs
More file actions
66 lines (56 loc) · 1.85 KB
/
Copy pathsimplify_loader.cjs
File metadata and controls
66 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const fs = require('fs');
const path = 'backend/src/services/graph/loader.ts';
let code = fs.readFileSync(path, 'utf8');
const calcTokenScoreStr = `
function calcTokenScore(label: string, labelTokenSet: Set<string>, tokens: readonly string[], node: Node): number {
if (tokens.length === 0) return 0;
let score = 0;
let matched = 0;
for (const t of tokens) {
if (STOPWORDS.has(t)) {
matched += 1;
continue;
}
if (labelTokenSet.has(t)) matched += 1;
else if (label.includes(t)) matched += 0.5;
}
const ratio = matched / tokens.length;
score += ratio * 300;
// Whole-word numeric match is decisive for seating sections / gates.
for (const t of tokens) {
if (/^\\d+$/.test(t) && labelTokenSet.has(t)) {
score += node.type === 'seating_section' ? 500 : 250;
}
}
return score;
}
`;
const searchStr = ` if (tokens.length > 0) {
const labelTokens = label.split(' ');
const labelTokenSet = new Set(labelTokens);
let matched = 0;
for (const t of tokens) {
if (STOPWORDS.has(t)) {
matched += 1;
continue;
}
if (labelTokenSet.has(t)) matched += 1;
else if (label.includes(t)) matched += 0.5;
}
const ratio = matched / tokens.length;
score += ratio * 300;
// Whole-word numeric match is decisive for seating sections / gates.
for (const t of tokens) {
if (/^\\d+$/.test(t) && labelTokenSet.has(t)) {
score += node.type === 'seating_section' ? 500 : 250;
}
}
}`;
const replaceStr = ` if (tokens.length > 0) {
const labelTokens = label.split(' ');
const labelTokenSet = new Set(labelTokens);
score += calcTokenScore(label, labelTokenSet, tokens, node);
}`;
code = code.replace(searchStr, replaceStr);
code = code.replace('function scoreNode', calcTokenScoreStr + '\nfunction scoreNode');
fs.writeFileSync(path, code);