@open-livex-wiki/core 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.d.ts +3 -3
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/coverage.d.ts +8 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +425 -0
- package/dist/coverage.js.map +1 -0
- package/dist/diff-impact.d.ts.map +1 -1
- package/dist/diff-impact.js +109 -45
- package/dist/diff-impact.js.map +1 -1
- package/dist/discover.d.ts +35 -3
- package/dist/discover.d.ts.map +1 -1
- package/dist/discover.js +1800 -293
- package/dist/discover.js.map +1 -1
- package/dist/frontmatter.d.ts.map +1 -1
- package/dist/frontmatter.js +96 -6
- package/dist/frontmatter.js.map +1 -1
- package/dist/gate.d.ts.map +1 -1
- package/dist/gate.js +2 -0
- package/dist/gate.js.map +1 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +3 -0
- package/dist/graph.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +2 -1
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +427 -178
- package/dist/loader.js.map +1 -1
- package/dist/schemas.d.ts +79 -3
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +49 -4
- package/dist/schemas.js.map +1 -1
- package/dist/types.d.ts +71 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +35 -2
- package/dist/utils.js.map +1 -1
- package/dist/validator.d.ts +32 -1
- package/dist/validator.d.ts.map +1 -1
- package/dist/validator.js +346 -26
- package/dist/validator.js.map +1 -1
- package/package.json +1 -1
package/dist/discover.js
CHANGED
|
@@ -28,7 +28,7 @@ const GENERIC_CJK_SUFFIX_BIGRAMS = new Set([
|
|
|
28
28
|
"组件",
|
|
29
29
|
"引擎",
|
|
30
30
|
]);
|
|
31
|
-
const SOURCE_FILE_EXT_REGEX = /\.(?:py|ts|tsx|js|jsx|mjs|cjs|go|java|rs|rb|php|cs|cpp|cc|c|h|hpp|sql|json|yaml|yml|toml|md)$/i;
|
|
31
|
+
const SOURCE_FILE_EXT_REGEX = /\.(?:py|ts|tsx|js|jsx|mjs|cjs|go|java|rs|rb|php|cs|cpp|cc|c|h|hpp|sql|json|yaml|yml|toml|md|pdf|png|jpg|jpeg|webp|svg|csv|parquet)$/i;
|
|
32
32
|
const IDENTIFIER_NOISE_SUBTOKENS = new Set([
|
|
33
33
|
"py",
|
|
34
34
|
"ts",
|
|
@@ -44,6 +44,350 @@ const IDENTIFIER_NOISE_SUBTOKENS = new Set([
|
|
|
44
44
|
"src",
|
|
45
45
|
"lib",
|
|
46
46
|
]);
|
|
47
|
+
const NON_DESIGNATOR_PREFIX_WORDS = new Set([
|
|
48
|
+
// Document / UI structural nouns & citation handle prefixes
|
|
49
|
+
"figure",
|
|
50
|
+
"figures",
|
|
51
|
+
"fig",
|
|
52
|
+
"figs",
|
|
53
|
+
"table",
|
|
54
|
+
"tables",
|
|
55
|
+
"tab",
|
|
56
|
+
"tbl",
|
|
57
|
+
"section",
|
|
58
|
+
"sections",
|
|
59
|
+
"sec",
|
|
60
|
+
"secs",
|
|
61
|
+
"chapter",
|
|
62
|
+
"chapters",
|
|
63
|
+
"chap",
|
|
64
|
+
"chaps",
|
|
65
|
+
"page",
|
|
66
|
+
"pages",
|
|
67
|
+
"pg",
|
|
68
|
+
"pp",
|
|
69
|
+
"part",
|
|
70
|
+
"parts",
|
|
71
|
+
"appendix",
|
|
72
|
+
"appendices",
|
|
73
|
+
"item",
|
|
74
|
+
"items",
|
|
75
|
+
"step",
|
|
76
|
+
"steps",
|
|
77
|
+
"stage",
|
|
78
|
+
"stages",
|
|
79
|
+
"phase",
|
|
80
|
+
"phases",
|
|
81
|
+
"tier",
|
|
82
|
+
"tiers",
|
|
83
|
+
"level",
|
|
84
|
+
"levels",
|
|
85
|
+
"version",
|
|
86
|
+
"versions",
|
|
87
|
+
"ver",
|
|
88
|
+
"vol",
|
|
89
|
+
"volume",
|
|
90
|
+
"issue",
|
|
91
|
+
"line",
|
|
92
|
+
"lines",
|
|
93
|
+
"row",
|
|
94
|
+
"rows",
|
|
95
|
+
"col",
|
|
96
|
+
"cols",
|
|
97
|
+
"column",
|
|
98
|
+
"columns",
|
|
99
|
+
"chart",
|
|
100
|
+
"charts",
|
|
101
|
+
"graph",
|
|
102
|
+
"diagram",
|
|
103
|
+
"anc",
|
|
104
|
+
"anchor",
|
|
105
|
+
"scene",
|
|
106
|
+
"scenes",
|
|
107
|
+
"domain",
|
|
108
|
+
"domains",
|
|
109
|
+
"chain",
|
|
110
|
+
"chains",
|
|
111
|
+
"doc",
|
|
112
|
+
"docs",
|
|
113
|
+
"file",
|
|
114
|
+
"files",
|
|
115
|
+
"test",
|
|
116
|
+
"tests",
|
|
117
|
+
"task",
|
|
118
|
+
"tasks",
|
|
119
|
+
// Time / fiscal / calendar / common units
|
|
120
|
+
"fy",
|
|
121
|
+
"cy",
|
|
122
|
+
"ty",
|
|
123
|
+
"year",
|
|
124
|
+
"years",
|
|
125
|
+
"yr",
|
|
126
|
+
"yrs",
|
|
127
|
+
"month",
|
|
128
|
+
"months",
|
|
129
|
+
"mo",
|
|
130
|
+
"week",
|
|
131
|
+
"weeks",
|
|
132
|
+
"wk",
|
|
133
|
+
"day",
|
|
134
|
+
"days",
|
|
135
|
+
"hour",
|
|
136
|
+
"hours",
|
|
137
|
+
"hr",
|
|
138
|
+
"hrs",
|
|
139
|
+
"min",
|
|
140
|
+
"mins",
|
|
141
|
+
"minute",
|
|
142
|
+
"minutes",
|
|
143
|
+
"second",
|
|
144
|
+
"seconds",
|
|
145
|
+
"jan",
|
|
146
|
+
"feb",
|
|
147
|
+
"mar",
|
|
148
|
+
"apr",
|
|
149
|
+
"jun",
|
|
150
|
+
"jul",
|
|
151
|
+
"aug",
|
|
152
|
+
"sep",
|
|
153
|
+
"sept",
|
|
154
|
+
"oct",
|
|
155
|
+
"nov",
|
|
156
|
+
"dec",
|
|
157
|
+
// Common English prepositions, conjunctions, verbs, articles, tech tokens
|
|
158
|
+
"the",
|
|
159
|
+
"and",
|
|
160
|
+
"for",
|
|
161
|
+
"with",
|
|
162
|
+
"from",
|
|
163
|
+
"into",
|
|
164
|
+
"over",
|
|
165
|
+
"under",
|
|
166
|
+
"between",
|
|
167
|
+
"about",
|
|
168
|
+
"after",
|
|
169
|
+
"before",
|
|
170
|
+
"during",
|
|
171
|
+
"within",
|
|
172
|
+
"through",
|
|
173
|
+
"across",
|
|
174
|
+
"among",
|
|
175
|
+
"than",
|
|
176
|
+
"per",
|
|
177
|
+
"via",
|
|
178
|
+
"see",
|
|
179
|
+
"ref",
|
|
180
|
+
"note",
|
|
181
|
+
"notes",
|
|
182
|
+
"report",
|
|
183
|
+
"reports",
|
|
184
|
+
"compare",
|
|
185
|
+
"versus",
|
|
186
|
+
"top",
|
|
187
|
+
"max",
|
|
188
|
+
"limit",
|
|
189
|
+
"port",
|
|
190
|
+
"code",
|
|
191
|
+
"type",
|
|
192
|
+
"mode",
|
|
193
|
+
"node",
|
|
194
|
+
"edge",
|
|
195
|
+
"utf",
|
|
196
|
+
"sha",
|
|
197
|
+
"int",
|
|
198
|
+
"uint",
|
|
199
|
+
"float",
|
|
200
|
+
"win",
|
|
201
|
+
"ipv",
|
|
202
|
+
"http",
|
|
203
|
+
"https",
|
|
204
|
+
"tcp",
|
|
205
|
+
"udp",
|
|
206
|
+
]);
|
|
207
|
+
const SPACE_STOP_PREFIXES = new Set([
|
|
208
|
+
"in",
|
|
209
|
+
"on",
|
|
210
|
+
"at",
|
|
211
|
+
"to",
|
|
212
|
+
"of",
|
|
213
|
+
"by",
|
|
214
|
+
"or",
|
|
215
|
+
"as",
|
|
216
|
+
"is",
|
|
217
|
+
"be",
|
|
218
|
+
"an",
|
|
219
|
+
"no",
|
|
220
|
+
"so",
|
|
221
|
+
"up",
|
|
222
|
+
"if",
|
|
223
|
+
"do",
|
|
224
|
+
"go",
|
|
225
|
+
"we",
|
|
226
|
+
"it",
|
|
227
|
+
"he",
|
|
228
|
+
"me",
|
|
229
|
+
"my",
|
|
230
|
+
"us",
|
|
231
|
+
"vs",
|
|
232
|
+
]);
|
|
233
|
+
/**
|
|
234
|
+
* Normalizes a single alphanumeric model, weapon, report, or standard designator
|
|
235
|
+
* (e.g. `"FFG-62"`, `"FFG 62"`, `"FFG62"` -> `"ffg62"`; `"ATP 7-100.1"` -> `"atp71001"`).
|
|
236
|
+
* Returns `undefined` for ordinary prose words, pure numbers, commit SHAs, or structural labels.
|
|
237
|
+
*/
|
|
238
|
+
export function normalizeDesignatorToken(text) {
|
|
239
|
+
if (typeof text !== "string") {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
const cleaned = text
|
|
243
|
+
.trim()
|
|
244
|
+
.replace(/^[([{"'`]+|[)\]}"'`,.;:!?]+$/g, "")
|
|
245
|
+
.trim();
|
|
246
|
+
if (!cleaned) {
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
// Must start with 1..10 ASCII letters and contain only ASCII letters, digits, spaces, and ./_- separators
|
|
250
|
+
if (!/^[A-Za-z]{1,10}(?:[\s./_-]+[A-Za-z0-9]+|[0-9][A-Za-z0-9./_-]*)+$/.test(cleaned)) {
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
if (!/[A-Za-z]/.test(cleaned) || !/[0-9]/.test(cleaned)) {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
// Reject unseparated hex SHA-1/SHA-256/short-SHA digests and UUIDs
|
|
257
|
+
if (!/[\s./_-]/.test(cleaned) && /^[0-9a-f]{7,64}$/i.test(cleaned)) {
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
if (/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(cleaned)) {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
// Reject semver / version tags like v1, v1.2, v0.2.0 (while keeping hyphenated V-22!)
|
|
264
|
+
if (/^v\s*\d+(?:\.\d+)*$/i.test(cleaned)) {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
// Reject source filenames with extensions (e.g. `v2.ts`, `route_v1.py`)
|
|
268
|
+
if (SOURCE_FILE_EXT_REGEX.test(cleaned)) {
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
const collapsed = cleaned.replace(/[^A-Za-z0-9]+/g, "").toLowerCase();
|
|
272
|
+
if (collapsed.length < 3 || collapsed.length > 32) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
const prefixMatch = /^([A-Za-z]+)/.exec(cleaned);
|
|
276
|
+
const rawPrefix = prefixMatch?.[1] ?? "";
|
|
277
|
+
const lowerPrefix = rawPrefix.toLowerCase();
|
|
278
|
+
if (!rawPrefix || NON_DESIGNATOR_PREFIX_WORDS.has(lowerPrefix)) {
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
// Guard space-separated prefixes against lowercase articles/prepositions ("a 10", "in 2025", "if 2")
|
|
282
|
+
const afterPrefix = cleaned.slice(rawPrefix.length);
|
|
283
|
+
if (/^\s/.test(afterPrefix)) {
|
|
284
|
+
if (rawPrefix.length === 1 && rawPrefix === lowerPrefix) {
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
if (SPACE_STOP_PREFIXES.has(lowerPrefix)) {
|
|
288
|
+
const isUppercaseCrsReport = rawPrefix === rawPrefix.toUpperCase() && /^[A-Z]{2}\s+\d{4,5}[A-Z]?$/.test(cleaned);
|
|
289
|
+
if (!isUppercaseCrsReport) {
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return collapsed;
|
|
295
|
+
}
|
|
296
|
+
function buildDesignatorVariants(rawText, primaryCanonical) {
|
|
297
|
+
const variants = [primaryCanonical];
|
|
298
|
+
const parts = rawText
|
|
299
|
+
.trim()
|
|
300
|
+
.split(/[\s/_-]+/)
|
|
301
|
+
.filter(Boolean);
|
|
302
|
+
if (parts.length >= 2 && /^[A-Za-z]+$/.test(parts[0] ?? "")) {
|
|
303
|
+
const suffixRaw = parts.slice(1).join("-");
|
|
304
|
+
if (/^[A-Za-z]/.test(suffixRaw) && /[0-9]/.test(suffixRaw)) {
|
|
305
|
+
const subNorm = normalizeDesignatorToken(suffixRaw);
|
|
306
|
+
if (subNorm && !variants.includes(subNorm)) {
|
|
307
|
+
variants.push(subNorm);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return variants;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Scans arbitrary text and returns structured designator mentions (preserving original span + canonical variants).
|
|
315
|
+
*/
|
|
316
|
+
export function extractDesignatorMentions(text) {
|
|
317
|
+
if (typeof text !== "string" || !text || !/[0-9]/.test(text) || !/[A-Za-z]/.test(text)) {
|
|
318
|
+
return [];
|
|
319
|
+
}
|
|
320
|
+
// Mask URLs and SCP remotes with spaces only when `://` or `@` is actually present
|
|
321
|
+
let masked = text;
|
|
322
|
+
if (masked.includes("://")) {
|
|
323
|
+
masked = masked.replace(/(?:git\+)?(?:https?|ssh|git):\/\/[^\s)>\]"']+/gi, " ");
|
|
324
|
+
}
|
|
325
|
+
if (masked.includes("@")) {
|
|
326
|
+
masked = masked.replace(/[A-Za-z0-9._+-]+@[A-Za-z0-9.-]+:[^\s)>\]"']+/g, " ");
|
|
327
|
+
}
|
|
328
|
+
const spans = [];
|
|
329
|
+
for (const m of masked.matchAll(/[A-Za-z0-9]+(?:[-/.][A-Za-z0-9]+)*/g)) {
|
|
330
|
+
const tokenText = m[0];
|
|
331
|
+
const start = m.index ?? 0;
|
|
332
|
+
if (tokenText) {
|
|
333
|
+
spans.push({ text: tokenText, start, end: start + tokenText.length });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const mentions = [];
|
|
337
|
+
const seenPrimary = new Set();
|
|
338
|
+
for (let i = 0; i < spans.length; i++) {
|
|
339
|
+
const cur = spans[i];
|
|
340
|
+
const next = spans[i + 1];
|
|
341
|
+
if (next && /^[A-Za-z]{1,8}(?:[-/][A-Za-z]{1,8})?$/.test(cur.text)) {
|
|
342
|
+
const between = masked.slice(cur.end, next.start);
|
|
343
|
+
const isSpaceOnly = /^[ \t]+$/.test(between);
|
|
344
|
+
const isSpacedContinuation = /^[0-9][0-9A-Za-z]*(?:[-/.][0-9A-Za-z]+)*$/.test(next.text) ||
|
|
345
|
+
/^[A-Za-z]{1,2}[0-9]{3,}[A-Za-z]?$/.test(next.text);
|
|
346
|
+
if (isSpaceOnly && isSpacedContinuation) {
|
|
347
|
+
const combinedRaw = `${cur.text} ${next.text}`;
|
|
348
|
+
const combinedNorm = normalizeDesignatorToken(combinedRaw);
|
|
349
|
+
if (combinedNorm) {
|
|
350
|
+
if (!seenPrimary.has(combinedNorm)) {
|
|
351
|
+
seenPrimary.add(combinedNorm);
|
|
352
|
+
mentions.push({
|
|
353
|
+
raw: combinedRaw,
|
|
354
|
+
canonical: combinedNorm,
|
|
355
|
+
variants: buildDesignatorVariants(combinedRaw, combinedNorm),
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
i += 1;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (!/[0-9]/.test(cur.text)) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const singleNorm = normalizeDesignatorToken(cur.text);
|
|
367
|
+
if (singleNorm && !seenPrimary.has(singleNorm)) {
|
|
368
|
+
seenPrimary.add(singleNorm);
|
|
369
|
+
mentions.push({
|
|
370
|
+
raw: cur.text,
|
|
371
|
+
canonical: singleNorm,
|
|
372
|
+
variants: buildDesignatorVariants(cur.text, singleNorm),
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return mentions;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Extracts deduplicated canonical alphanumeric designator tokens from arbitrary text
|
|
380
|
+
* (e.g. `"CRS-R44972"` -> `["crsr44972", "r44972"]`, `"FFG 62"` -> `["ffg62"]`, `"ATP 7-100.1"` -> `["atp71001"]`).
|
|
381
|
+
*/
|
|
382
|
+
export function extractDesignatorTokens(text) {
|
|
383
|
+
const out = new Set();
|
|
384
|
+
for (const mention of extractDesignatorMentions(text)) {
|
|
385
|
+
for (const v of mention.variants) {
|
|
386
|
+
out.add(v);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return Array.from(out);
|
|
390
|
+
}
|
|
47
391
|
function isPureCjk(text) {
|
|
48
392
|
return text.length > 0 && /^[\u3400-\u9fff\uf900-\ufaff]+$/.test(text);
|
|
49
393
|
}
|
|
@@ -155,9 +499,13 @@ function extractIdentifierSubTokens(rawAsciiToken, lowerPrimary) {
|
|
|
155
499
|
const deduped = Array.from(new Set(rawParts));
|
|
156
500
|
return deduped.length >= 2 ? deduped : [];
|
|
157
501
|
}
|
|
502
|
+
function escapeRegExpLiteral(str) {
|
|
503
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
504
|
+
}
|
|
158
505
|
/**
|
|
159
|
-
* Smart CJK + Mixed-Script tokenizer for progressive discovery queries.
|
|
506
|
+
* Smart CJK + Mixed-Script + Designator-Aware tokenizer for progressive discovery queries.
|
|
160
507
|
* - Splits on whitespace and punctuation while preserving `.`, `-`, `_`, `#` inside identifiers/filenames.
|
|
508
|
+
* - Canonicalizes alphanumeric designators (`"FFG-62"` / `"FFG 62"` -> `"ffg62"`, `"CRS-R44972"` -> `["crsr44972", "r44972"]`).
|
|
161
509
|
* - Splits CJK <-> ASCII/number transitions (`"iOS内购"` -> `["ios", "内购"]`, `"LLM网关"` -> `["llm", "网关"]`).
|
|
162
510
|
* - Strips Chinese filler/stop particles when remaining CJK length >= 2.
|
|
163
511
|
* - Emits overlapping CJK 2-grams and 3-grams for CJK compound tokens of length >= 3.
|
|
@@ -191,14 +539,48 @@ export function tokenizeDiscoveryQuery(query) {
|
|
|
191
539
|
}
|
|
192
540
|
seenPrimary.add(t);
|
|
193
541
|
const subTokens = extractIdentifierSubTokens(cleanedOriginal, t);
|
|
542
|
+
const normDesig = normalizeDesignatorToken(cleanedOriginal);
|
|
543
|
+
const designators = normDesig ? buildDesignatorVariants(cleanedOriginal, normDesig) : [];
|
|
194
544
|
groups.push({
|
|
195
545
|
primary: t,
|
|
196
546
|
threeGrams: [],
|
|
197
547
|
twoGrams: [],
|
|
198
548
|
...(subTokens.length > 0 ? { subTokens } : {}),
|
|
549
|
+
...(designators.length > 0 ? { designators } : {}),
|
|
199
550
|
});
|
|
200
551
|
}
|
|
201
|
-
for (
|
|
552
|
+
for (let idx = 0; idx < rawSegments.length; idx++) {
|
|
553
|
+
const segment = rawSegments[idx];
|
|
554
|
+
const nextSeg = rawSegments[idx + 1];
|
|
555
|
+
// Coalesce adjacent space-separated designators (e.g. "FFG 62", "SSBN 826", "CRS R44972", "ATP 7-100.1")
|
|
556
|
+
if (nextSeg &&
|
|
557
|
+
!CJK_CHAR_REGEX.test(segment) &&
|
|
558
|
+
!CJK_CHAR_REGEX.test(nextSeg) &&
|
|
559
|
+
/^[A-Za-z]{1,8}(?:-[A-Za-z]{1,8})?$/.test(segment) &&
|
|
560
|
+
(/^[0-9][0-9A-Za-z._-]*$/.test(nextSeg) || /^[A-Za-z]{1,2}[0-9]{3,}[A-Za-z]?$/.test(nextSeg))) {
|
|
561
|
+
const spacedRegex = new RegExp(`(?<![A-Za-z0-9_])${escapeRegExpLiteral(segment)}[ \\t]+${escapeRegExpLiteral(nextSeg)}(?![A-Za-z0-9_])`);
|
|
562
|
+
const combinedRaw = `${segment} ${nextSeg}`;
|
|
563
|
+
const combinedNorm = spacedRegex.test(trimmedOriginal)
|
|
564
|
+
? normalizeDesignatorToken(combinedRaw)
|
|
565
|
+
: undefined;
|
|
566
|
+
if (combinedNorm) {
|
|
567
|
+
const primaryLower = combinedRaw.toLowerCase();
|
|
568
|
+
if (!seenPrimary.has(primaryLower)) {
|
|
569
|
+
seenPrimary.add(primaryLower);
|
|
570
|
+
const subTokens = extractIdentifierSubTokens(`${segment}-${nextSeg}`, primaryLower);
|
|
571
|
+
const designators = buildDesignatorVariants(combinedRaw, combinedNorm);
|
|
572
|
+
groups.push({
|
|
573
|
+
primary: primaryLower,
|
|
574
|
+
threeGrams: [],
|
|
575
|
+
twoGrams: [],
|
|
576
|
+
...(subTokens.length > 0 ? { subTokens } : {}),
|
|
577
|
+
designators,
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
idx += 1;
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
202
584
|
const hasMixedScript = CJK_CHAR_REGEX.test(segment) && /[^\u3400-\u9fff\uf900-\ufaff]/.test(segment);
|
|
203
585
|
if (!hasMixedScript) {
|
|
204
586
|
if (isPureCjk(segment)) {
|
|
@@ -309,6 +691,561 @@ function extractMatchSnippet(sources, matchedTerms, targetLength = 80) {
|
|
|
309
691
|
}
|
|
310
692
|
return undefined;
|
|
311
693
|
}
|
|
694
|
+
const WIKI_INDEX_CACHE = new WeakMap();
|
|
695
|
+
function toPathTerm(raw) {
|
|
696
|
+
const lower = toPosixPath(raw).toLowerCase();
|
|
697
|
+
const base = lower.split("/").pop() ?? lower;
|
|
698
|
+
return { lower, base };
|
|
699
|
+
}
|
|
700
|
+
function collectDesignatorsFromStrings(strings, rawMap) {
|
|
701
|
+
const out = new Set();
|
|
702
|
+
for (const s of strings) {
|
|
703
|
+
if (!s) {
|
|
704
|
+
continue;
|
|
705
|
+
}
|
|
706
|
+
for (const mention of extractDesignatorMentions(s)) {
|
|
707
|
+
for (const v of mention.variants) {
|
|
708
|
+
out.add(v);
|
|
709
|
+
if (rawMap && !rawMap.has(v)) {
|
|
710
|
+
rawMap.set(v, mention.raw);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
export function formatAnchorSummaryLabel(anchor) {
|
|
718
|
+
const owner = anchor.source ?? anchor.repo ?? "anchor";
|
|
719
|
+
const pagePart = anchor.pageRange ? `#p${anchor.pageRange[0]}-${anchor.pageRange[1]}` : "";
|
|
720
|
+
const symbolPart = anchor.symbol ? `#${anchor.symbol}` : "";
|
|
721
|
+
const extraPart = anchor.assetId
|
|
722
|
+
? ` (${anchor.assetId})`
|
|
723
|
+
: anchor.sectionTitle
|
|
724
|
+
? ` (${anchor.sectionTitle})`
|
|
725
|
+
: "";
|
|
726
|
+
const handlePrefix = anchor.handle ? `[${anchor.handle}] ` : "";
|
|
727
|
+
return `${handlePrefix}${owner}:${toPosixPath(anchor.file)}${pagePart}${symbolPart}${extraPart}`;
|
|
728
|
+
}
|
|
729
|
+
export function toMatchedAnchorSummary(anchor) {
|
|
730
|
+
return {
|
|
731
|
+
label: formatAnchorSummaryLabel(anchor),
|
|
732
|
+
...(anchor.repo ? { repo: anchor.repo } : {}),
|
|
733
|
+
...(anchor.source ? { source: anchor.source } : {}),
|
|
734
|
+
file: anchor.file,
|
|
735
|
+
...(anchor.symbol ? { symbol: anchor.symbol } : {}),
|
|
736
|
+
...(anchor.kind ? { kind: anchor.kind } : {}),
|
|
737
|
+
...(anchor.pageRange
|
|
738
|
+
? { pageRange: [anchor.pageRange[0], anchor.pageRange[1]] }
|
|
739
|
+
: {}),
|
|
740
|
+
...(anchor.sectionTitle ? { sectionTitle: anchor.sectionTitle } : {}),
|
|
741
|
+
...(anchor.assetId ? { assetId: anchor.assetId } : {}),
|
|
742
|
+
...(anchor.handle ? { handle: anchor.handle } : {}),
|
|
743
|
+
...(anchor.scopeNote ? { scopeNote: anchor.scopeNote } : {}),
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
function collectAnchorSearchStrings(anchor) {
|
|
747
|
+
const raw = [anchor.file];
|
|
748
|
+
if (anchor.symbol) {
|
|
749
|
+
raw.push(anchor.symbol);
|
|
750
|
+
}
|
|
751
|
+
if (anchor.sectionTitle) {
|
|
752
|
+
raw.push(anchor.sectionTitle);
|
|
753
|
+
}
|
|
754
|
+
if (anchor.assetId) {
|
|
755
|
+
raw.push(anchor.assetId);
|
|
756
|
+
}
|
|
757
|
+
if (anchor.handle) {
|
|
758
|
+
raw.push(anchor.handle);
|
|
759
|
+
}
|
|
760
|
+
if (anchor.scopeNote) {
|
|
761
|
+
raw.push(anchor.scopeNote);
|
|
762
|
+
}
|
|
763
|
+
if (anchor.pageRange) {
|
|
764
|
+
raw.push(`p.${anchor.pageRange[0]}-${anchor.pageRange[1]}`);
|
|
765
|
+
raw.push(`p${anchor.pageRange[0]}-${anchor.pageRange[1]}`);
|
|
766
|
+
}
|
|
767
|
+
return raw;
|
|
768
|
+
}
|
|
769
|
+
function buildSceneIndexedDoc(scene) {
|
|
770
|
+
const repoNames = scene.frontmatter.repositories.map((r) => r.repo);
|
|
771
|
+
const sourceRefs = scene.frontmatter.sources ?? [];
|
|
772
|
+
const sourceNames = sourceRefs.map((s) => s.source);
|
|
773
|
+
const sourceSearchRaw = sourceRefs.flatMap((s) => [s.source, ...(s.path ? [s.path] : [])]);
|
|
774
|
+
const chainNames = scene.chains.map((c) => c.title);
|
|
775
|
+
const rawByDesignator = new Map();
|
|
776
|
+
const chainEntries = scene.chains.map((c) => {
|
|
777
|
+
const terms = [c.id, c.title, ...(c.entryPoint ? [c.entryPoint] : [])];
|
|
778
|
+
return {
|
|
779
|
+
title: c.title,
|
|
780
|
+
termsLower: terms.map((t) => t.toLowerCase()),
|
|
781
|
+
bodyLower: c.markdown.toLowerCase(),
|
|
782
|
+
designators: collectDesignatorsFromStrings([...terms, c.markdown], rawByDesignator),
|
|
783
|
+
};
|
|
784
|
+
});
|
|
785
|
+
const detailEntries = scene.details.map((d) => ({
|
|
786
|
+
name: d.name,
|
|
787
|
+
nameLower: d.name.toLowerCase(),
|
|
788
|
+
titleLower: d.title.toLowerCase(),
|
|
789
|
+
contentLower: d.content.toLowerCase(),
|
|
790
|
+
designators: collectDesignatorsFromStrings([d.name, d.title, d.content], rawByDesignator),
|
|
791
|
+
}));
|
|
792
|
+
const anchorEntries = (scene.frontmatter.anchors ?? []).map((a) => {
|
|
793
|
+
const rawStrings = collectAnchorSearchStrings(a);
|
|
794
|
+
return {
|
|
795
|
+
anchor: a,
|
|
796
|
+
summary: toMatchedAnchorSummary(a),
|
|
797
|
+
terms: rawStrings.map(toPathTerm),
|
|
798
|
+
designators: collectDesignatorsFromStrings(rawStrings, rawByDesignator),
|
|
799
|
+
};
|
|
800
|
+
});
|
|
801
|
+
const chainTermsRaw = scene.chains.flatMap((c) => [
|
|
802
|
+
c.id,
|
|
803
|
+
c.title,
|
|
804
|
+
...(c.entryPoint ? [c.entryPoint] : []),
|
|
805
|
+
]);
|
|
806
|
+
const anchorTerms = anchorEntries.flatMap((ae) => ae.terms);
|
|
807
|
+
const repoTermsRaw = [
|
|
808
|
+
...repoNames,
|
|
809
|
+
...(scene.frontmatter.anchors ?? []).flatMap((a) => (a.repo ? [a.repo] : [])),
|
|
810
|
+
];
|
|
811
|
+
const sourceTermsAllRaw = [
|
|
812
|
+
...sourceSearchRaw,
|
|
813
|
+
...(scene.frontmatter.anchors ?? []).flatMap((a) => (a.source ? [a.source] : [])),
|
|
814
|
+
];
|
|
815
|
+
const repoTerms = Array.from(new Set(repoTermsRaw)).map(toPathTerm);
|
|
816
|
+
const sourceTerms = Array.from(new Set(sourceTermsAllRaw)).map(toPathTerm);
|
|
817
|
+
const detailTitlesRaw = scene.details.flatMap((d) => [d.name, d.title]);
|
|
818
|
+
const rawCodeSpans = [
|
|
819
|
+
...extractInlineCodeSpans(scene.content),
|
|
820
|
+
...scene.details.flatMap((d) => extractInlineCodeSpans(d.content)),
|
|
821
|
+
];
|
|
822
|
+
const codeSpans = rawCodeSpans.map((span) => {
|
|
823
|
+
const lower = toPosixPath(span).toLowerCase();
|
|
824
|
+
return {
|
|
825
|
+
lower,
|
|
826
|
+
parts: lower.split(/[/:.#\s]/).filter(Boolean),
|
|
827
|
+
};
|
|
828
|
+
});
|
|
829
|
+
const idLower = scene.id.toLowerCase();
|
|
830
|
+
const shortIdLower = scene.sceneId.toLowerCase();
|
|
831
|
+
const titleLower = scene.frontmatter.title.toLowerCase();
|
|
832
|
+
const aliasesLower = scene.frontmatter.aliases.map((a) => a.toLowerCase());
|
|
833
|
+
const chainTermsLower = chainTermsRaw.map((c) => c.toLowerCase());
|
|
834
|
+
const summaryLower = scene.frontmatter.summary.toLowerCase();
|
|
835
|
+
const headingsLower = scene.headings.map((h) => h.toLowerCase());
|
|
836
|
+
const detailTitlesLower = detailTitlesRaw.map((t) => t.toLowerCase());
|
|
837
|
+
const bodyLower = scene.content.toLowerCase();
|
|
838
|
+
const detailBodySourcesLower = detailEntries.map((d) => d.contentLower);
|
|
839
|
+
const tier34GateLower = [
|
|
840
|
+
summaryLower,
|
|
841
|
+
...headingsLower,
|
|
842
|
+
...detailTitlesLower,
|
|
843
|
+
...codeSpans.map((c) => c.lower),
|
|
844
|
+
bodyLower,
|
|
845
|
+
...detailBodySourcesLower,
|
|
846
|
+
].join("\n");
|
|
847
|
+
const allTextLower = [
|
|
848
|
+
idLower,
|
|
849
|
+
shortIdLower,
|
|
850
|
+
titleLower,
|
|
851
|
+
...aliasesLower,
|
|
852
|
+
...chainTermsLower,
|
|
853
|
+
...anchorTerms.map((a) => a.lower),
|
|
854
|
+
...repoTerms.map((r) => r.lower),
|
|
855
|
+
...sourceTerms.map((s) => s.lower),
|
|
856
|
+
tier34GateLower,
|
|
857
|
+
].join("\n");
|
|
858
|
+
const idDesig = collectDesignatorsFromStrings([scene.id, scene.sceneId], rawByDesignator);
|
|
859
|
+
const titleDesig = collectDesignatorsFromStrings([scene.frontmatter.title], rawByDesignator);
|
|
860
|
+
const aliasDesig = collectDesignatorsFromStrings(scene.frontmatter.aliases, rawByDesignator);
|
|
861
|
+
const chainDesig = collectDesignatorsFromStrings(chainTermsRaw, rawByDesignator);
|
|
862
|
+
const anchorDesig = new Set();
|
|
863
|
+
for (const ae of anchorEntries) {
|
|
864
|
+
for (const d of ae.designators) {
|
|
865
|
+
anchorDesig.add(d);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
const repoDesig = collectDesignatorsFromStrings(repoTermsRaw, rawByDesignator);
|
|
869
|
+
const sourceDesig = collectDesignatorsFromStrings(sourceTermsAllRaw, rawByDesignator);
|
|
870
|
+
const summaryDesig = collectDesignatorsFromStrings([scene.frontmatter.summary], rawByDesignator);
|
|
871
|
+
const headingDesig = collectDesignatorsFromStrings(scene.headings, rawByDesignator);
|
|
872
|
+
const detailTitleDesig = collectDesignatorsFromStrings(detailTitlesRaw, rawByDesignator);
|
|
873
|
+
const codeDesig = collectDesignatorsFromStrings(rawCodeSpans, rawByDesignator);
|
|
874
|
+
const bodyDesig = collectDesignatorsFromStrings([scene.content, ...scene.details.map((d) => d.content)], rawByDesignator);
|
|
875
|
+
const allDesig = new Set([
|
|
876
|
+
...idDesig,
|
|
877
|
+
...titleDesig,
|
|
878
|
+
...aliasDesig,
|
|
879
|
+
...chainDesig,
|
|
880
|
+
...anchorDesig,
|
|
881
|
+
...repoDesig,
|
|
882
|
+
...sourceDesig,
|
|
883
|
+
...summaryDesig,
|
|
884
|
+
...headingDesig,
|
|
885
|
+
...detailTitleDesig,
|
|
886
|
+
...codeDesig,
|
|
887
|
+
...bodyDesig,
|
|
888
|
+
]);
|
|
889
|
+
const emptySet = new Set();
|
|
890
|
+
const reverseCandidatesLower = Array.from(new Set([
|
|
891
|
+
...(shortIdLower.length >= 3 ? [shortIdLower] : []),
|
|
892
|
+
...(titleLower.length >= 2 ? [titleLower] : []),
|
|
893
|
+
...aliasesLower.filter((a) => a.length >= 2),
|
|
894
|
+
...chainTermsLower.filter((c) => c.length >= 3),
|
|
895
|
+
...anchorTerms.map((a) => a.base).filter((b) => b.length >= 3),
|
|
896
|
+
...repoTerms.map((r) => r.base).filter((b) => b.length >= 3),
|
|
897
|
+
...sourceTerms.map((s) => s.base).filter((b) => b.length >= 3),
|
|
898
|
+
]));
|
|
899
|
+
return {
|
|
900
|
+
scene,
|
|
901
|
+
repoNames,
|
|
902
|
+
sourceNames,
|
|
903
|
+
chainNames,
|
|
904
|
+
doc: {
|
|
905
|
+
idLower,
|
|
906
|
+
idCollapsed: idLower.replace(/[^a-z0-9]+/g, ""),
|
|
907
|
+
shortIdLower,
|
|
908
|
+
shortIdCollapsed: shortIdLower.replace(/[^a-z0-9]+/g, ""),
|
|
909
|
+
titleLower,
|
|
910
|
+
titleCollapsed: titleLower.replace(/[^a-z0-9]+/g, ""),
|
|
911
|
+
aliasesLower,
|
|
912
|
+
chainTermsLower,
|
|
913
|
+
anchorTerms,
|
|
914
|
+
repoTerms,
|
|
915
|
+
sourceTerms,
|
|
916
|
+
summaryLower,
|
|
917
|
+
headingsLower,
|
|
918
|
+
detailTitlesLower,
|
|
919
|
+
childHighTermsLower: [],
|
|
920
|
+
childAnchorTerms: [],
|
|
921
|
+
childRepoTerms: [],
|
|
922
|
+
childSourceTerms: [],
|
|
923
|
+
childBodySourcesLower: [],
|
|
924
|
+
codeSpans,
|
|
925
|
+
bodySourcesLower: [bodyLower],
|
|
926
|
+
detailBodySourcesLower,
|
|
927
|
+
tier34GateLower,
|
|
928
|
+
allTextLower,
|
|
929
|
+
reverseCandidatesLower,
|
|
930
|
+
rawSnippetSources: [scene.content, ...scene.details.map((d) => d.content)],
|
|
931
|
+
designators: {
|
|
932
|
+
id: idDesig,
|
|
933
|
+
title: titleDesig,
|
|
934
|
+
alias: aliasDesig,
|
|
935
|
+
chain: chainDesig,
|
|
936
|
+
anchor: anchorDesig,
|
|
937
|
+
repo: repoDesig,
|
|
938
|
+
source: sourceDesig,
|
|
939
|
+
summary: summaryDesig,
|
|
940
|
+
heading: headingDesig,
|
|
941
|
+
detail: detailTitleDesig,
|
|
942
|
+
scene: emptySet,
|
|
943
|
+
code: codeDesig,
|
|
944
|
+
body: bodyDesig,
|
|
945
|
+
all: allDesig,
|
|
946
|
+
rawByDesignator,
|
|
947
|
+
},
|
|
948
|
+
chainEntries,
|
|
949
|
+
detailEntries,
|
|
950
|
+
anchorEntries,
|
|
951
|
+
},
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
function buildDomainIndexedDoc(domain, sceneById) {
|
|
955
|
+
const rawByDesignator = new Map();
|
|
956
|
+
const childHighTermsLower = [];
|
|
957
|
+
const childAnchorTerms = [];
|
|
958
|
+
const childRepoTerms = [];
|
|
959
|
+
const childSourceTerms = [];
|
|
960
|
+
const childBodySourcesLower = [];
|
|
961
|
+
const rawSnippetSources = [domain.content];
|
|
962
|
+
const domainRawCodeSpans = extractInlineCodeSpans(domain.content);
|
|
963
|
+
const codeSpans = domainRawCodeSpans.map((span) => {
|
|
964
|
+
const lower = toPosixPath(span).toLowerCase();
|
|
965
|
+
return {
|
|
966
|
+
lower,
|
|
967
|
+
parts: lower.split(/[/:.#\s]/).filter(Boolean),
|
|
968
|
+
};
|
|
969
|
+
});
|
|
970
|
+
const sceneDesig = new Set();
|
|
971
|
+
const childAnchorDesig = new Set();
|
|
972
|
+
const childRepoDesig = new Set();
|
|
973
|
+
const childSourceDesig = new Set();
|
|
974
|
+
const childBodyDesig = new Set();
|
|
975
|
+
const codeDesig = collectDesignatorsFromStrings(domainRawCodeSpans, rawByDesignator);
|
|
976
|
+
for (const sceneId of domain.sceneIds) {
|
|
977
|
+
const prepScene = sceneById.get(sceneId);
|
|
978
|
+
if (!prepScene) {
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
const sDoc = prepScene.doc;
|
|
982
|
+
if (sDoc.shortIdLower) {
|
|
983
|
+
childHighTermsLower.push(sDoc.shortIdLower);
|
|
984
|
+
}
|
|
985
|
+
childHighTermsLower.push(sDoc.titleLower, ...sDoc.aliasesLower, sDoc.summaryLower, ...sDoc.headingsLower, ...sDoc.chainTermsLower, ...sDoc.detailTitlesLower);
|
|
986
|
+
childAnchorTerms.push(...sDoc.anchorTerms);
|
|
987
|
+
childRepoTerms.push(...sDoc.repoTerms);
|
|
988
|
+
childSourceTerms.push(...sDoc.sourceTerms);
|
|
989
|
+
codeSpans.push(...sDoc.codeSpans);
|
|
990
|
+
childBodySourcesLower.push(...sDoc.bodySourcesLower, ...sDoc.detailBodySourcesLower);
|
|
991
|
+
rawSnippetSources.push(...sDoc.rawSnippetSources);
|
|
992
|
+
for (const d of [
|
|
993
|
+
...sDoc.designators.id,
|
|
994
|
+
...sDoc.designators.title,
|
|
995
|
+
...sDoc.designators.alias,
|
|
996
|
+
...sDoc.designators.summary,
|
|
997
|
+
...sDoc.designators.heading,
|
|
998
|
+
...sDoc.designators.chain,
|
|
999
|
+
...sDoc.designators.detail,
|
|
1000
|
+
]) {
|
|
1001
|
+
sceneDesig.add(d);
|
|
1002
|
+
const raw = sDoc.designators.rawByDesignator.get(d);
|
|
1003
|
+
if (raw && !rawByDesignator.has(d)) {
|
|
1004
|
+
rawByDesignator.set(d, raw);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
for (const d of sDoc.designators.anchor) {
|
|
1008
|
+
childAnchorDesig.add(d);
|
|
1009
|
+
const raw = sDoc.designators.rawByDesignator.get(d);
|
|
1010
|
+
if (raw && !rawByDesignator.has(d)) {
|
|
1011
|
+
rawByDesignator.set(d, raw);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
for (const d of sDoc.designators.repo) {
|
|
1015
|
+
childRepoDesig.add(d);
|
|
1016
|
+
}
|
|
1017
|
+
for (const d of sDoc.designators.source) {
|
|
1018
|
+
childSourceDesig.add(d);
|
|
1019
|
+
}
|
|
1020
|
+
for (const d of sDoc.designators.code) {
|
|
1021
|
+
codeDesig.add(d);
|
|
1022
|
+
const raw = sDoc.designators.rawByDesignator.get(d);
|
|
1023
|
+
if (raw && !rawByDesignator.has(d)) {
|
|
1024
|
+
rawByDesignator.set(d, raw);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
for (const d of sDoc.designators.body) {
|
|
1028
|
+
childBodyDesig.add(d);
|
|
1029
|
+
const raw = sDoc.designators.rawByDesignator.get(d);
|
|
1030
|
+
if (raw && !rawByDesignator.has(d)) {
|
|
1031
|
+
rawByDesignator.set(d, raw);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
const idLower = domain.domainId.toLowerCase();
|
|
1036
|
+
const titleLower = domain.frontmatter.title.toLowerCase();
|
|
1037
|
+
const aliasesLower = domain.frontmatter.aliases.map((a) => a.toLowerCase());
|
|
1038
|
+
const summaryLower = domain.frontmatter.summary.toLowerCase();
|
|
1039
|
+
const headingsLower = domain.headings.map((h) => h.toLowerCase());
|
|
1040
|
+
const domainBodyLower = domain.content.toLowerCase();
|
|
1041
|
+
const tier34GateLower = [
|
|
1042
|
+
summaryLower,
|
|
1043
|
+
...headingsLower,
|
|
1044
|
+
...codeSpans.map((c) => c.lower),
|
|
1045
|
+
domainBodyLower,
|
|
1046
|
+
...childBodySourcesLower,
|
|
1047
|
+
].join("\n");
|
|
1048
|
+
const allTextLower = [
|
|
1049
|
+
idLower,
|
|
1050
|
+
titleLower,
|
|
1051
|
+
...aliasesLower,
|
|
1052
|
+
...childHighTermsLower,
|
|
1053
|
+
...childAnchorTerms.map((a) => a.lower),
|
|
1054
|
+
...childRepoTerms.map((r) => r.lower),
|
|
1055
|
+
...childSourceTerms.map((s) => s.lower),
|
|
1056
|
+
tier34GateLower,
|
|
1057
|
+
].join("\n");
|
|
1058
|
+
const idDesig = collectDesignatorsFromStrings([domain.domainId], rawByDesignator);
|
|
1059
|
+
const titleDesig = collectDesignatorsFromStrings([domain.frontmatter.title], rawByDesignator);
|
|
1060
|
+
const aliasDesig = collectDesignatorsFromStrings(domain.frontmatter.aliases, rawByDesignator);
|
|
1061
|
+
const summaryDesig = collectDesignatorsFromStrings([domain.frontmatter.summary], rawByDesignator);
|
|
1062
|
+
const headingDesig = collectDesignatorsFromStrings(domain.headings, rawByDesignator);
|
|
1063
|
+
const domainBodyDesig = collectDesignatorsFromStrings([domain.content], rawByDesignator);
|
|
1064
|
+
for (const d of domainBodyDesig) {
|
|
1065
|
+
childBodyDesig.add(d);
|
|
1066
|
+
}
|
|
1067
|
+
const emptySet = new Set();
|
|
1068
|
+
const allDesig = new Set([
|
|
1069
|
+
...idDesig,
|
|
1070
|
+
...titleDesig,
|
|
1071
|
+
...aliasDesig,
|
|
1072
|
+
...summaryDesig,
|
|
1073
|
+
...headingDesig,
|
|
1074
|
+
...sceneDesig,
|
|
1075
|
+
...childAnchorDesig,
|
|
1076
|
+
...childRepoDesig,
|
|
1077
|
+
...childSourceDesig,
|
|
1078
|
+
...codeDesig,
|
|
1079
|
+
...childBodyDesig,
|
|
1080
|
+
]);
|
|
1081
|
+
const reverseCandidatesLower = Array.from(new Set([
|
|
1082
|
+
...(titleLower.length >= 2 ? [titleLower] : []),
|
|
1083
|
+
...aliasesLower.filter((a) => a.length >= 2),
|
|
1084
|
+
...childHighTermsLower.filter((c) => c.length >= 2),
|
|
1085
|
+
]));
|
|
1086
|
+
return {
|
|
1087
|
+
domain,
|
|
1088
|
+
doc: {
|
|
1089
|
+
idLower,
|
|
1090
|
+
idCollapsed: idLower.replace(/[^a-z0-9]+/g, ""),
|
|
1091
|
+
titleLower,
|
|
1092
|
+
titleCollapsed: titleLower.replace(/[^a-z0-9]+/g, ""),
|
|
1093
|
+
aliasesLower,
|
|
1094
|
+
chainTermsLower: [],
|
|
1095
|
+
anchorTerms: [],
|
|
1096
|
+
repoTerms: [],
|
|
1097
|
+
sourceTerms: [],
|
|
1098
|
+
summaryLower,
|
|
1099
|
+
headingsLower,
|
|
1100
|
+
detailTitlesLower: [],
|
|
1101
|
+
childHighTermsLower,
|
|
1102
|
+
childAnchorTerms,
|
|
1103
|
+
childRepoTerms,
|
|
1104
|
+
childSourceTerms,
|
|
1105
|
+
childBodySourcesLower,
|
|
1106
|
+
codeSpans,
|
|
1107
|
+
bodySourcesLower: [domainBodyLower],
|
|
1108
|
+
detailBodySourcesLower: [],
|
|
1109
|
+
tier34GateLower,
|
|
1110
|
+
allTextLower,
|
|
1111
|
+
reverseCandidatesLower,
|
|
1112
|
+
rawSnippetSources,
|
|
1113
|
+
designators: {
|
|
1114
|
+
id: idDesig,
|
|
1115
|
+
title: titleDesig,
|
|
1116
|
+
alias: aliasDesig,
|
|
1117
|
+
chain: emptySet,
|
|
1118
|
+
anchor: childAnchorDesig,
|
|
1119
|
+
repo: childRepoDesig,
|
|
1120
|
+
source: childSourceDesig,
|
|
1121
|
+
summary: summaryDesig,
|
|
1122
|
+
heading: headingDesig,
|
|
1123
|
+
detail: emptySet,
|
|
1124
|
+
scene: sceneDesig,
|
|
1125
|
+
code: codeDesig,
|
|
1126
|
+
body: childBodyDesig,
|
|
1127
|
+
all: allDesig,
|
|
1128
|
+
rawByDesignator,
|
|
1129
|
+
},
|
|
1130
|
+
},
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
function getPrecomputedWikiIndex(wiki) {
|
|
1134
|
+
const cached = WIKI_INDEX_CACHE.get(wiki);
|
|
1135
|
+
if (cached &&
|
|
1136
|
+
cached.domainCount === wiki.domains.size &&
|
|
1137
|
+
cached.sceneCount === wiki.scenes.size) {
|
|
1138
|
+
return cached;
|
|
1139
|
+
}
|
|
1140
|
+
const sceneById = new Map();
|
|
1141
|
+
const scenes = [];
|
|
1142
|
+
for (const scene of wiki.scenes.values()) {
|
|
1143
|
+
const prep = buildSceneIndexedDoc(scene);
|
|
1144
|
+
scenes.push(prep);
|
|
1145
|
+
sceneById.set(scene.id, prep);
|
|
1146
|
+
}
|
|
1147
|
+
const index = {
|
|
1148
|
+
domainCount: wiki.domains.size,
|
|
1149
|
+
sceneCount: wiki.scenes.size,
|
|
1150
|
+
scenes,
|
|
1151
|
+
sceneById,
|
|
1152
|
+
termDfCache: new Map(),
|
|
1153
|
+
designatorDfCache: new Map(),
|
|
1154
|
+
};
|
|
1155
|
+
WIKI_INDEX_CACHE.set(wiki, index);
|
|
1156
|
+
return index;
|
|
1157
|
+
}
|
|
1158
|
+
function getPrecomputedDomains(wiki, index) {
|
|
1159
|
+
if (index.domains) {
|
|
1160
|
+
return index.domains;
|
|
1161
|
+
}
|
|
1162
|
+
const domains = [];
|
|
1163
|
+
for (const domain of wiki.domains.values()) {
|
|
1164
|
+
domains.push(buildDomainIndexedDoc(domain, index.sceneById));
|
|
1165
|
+
}
|
|
1166
|
+
index.domains = domains;
|
|
1167
|
+
return domains;
|
|
1168
|
+
}
|
|
1169
|
+
function getSceneTermDf(index, term) {
|
|
1170
|
+
const cached = index.termDfCache.get(term);
|
|
1171
|
+
if (cached !== undefined) {
|
|
1172
|
+
return cached;
|
|
1173
|
+
}
|
|
1174
|
+
let df = 0;
|
|
1175
|
+
for (const entry of index.scenes) {
|
|
1176
|
+
if (entry.doc.allTextLower.includes(term)) {
|
|
1177
|
+
df += 1;
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
index.termDfCache.set(term, df);
|
|
1181
|
+
return df;
|
|
1182
|
+
}
|
|
1183
|
+
function getSceneDesignatorDf(index, designator) {
|
|
1184
|
+
const cached = index.designatorDfCache.get(designator);
|
|
1185
|
+
if (cached !== undefined) {
|
|
1186
|
+
return cached;
|
|
1187
|
+
}
|
|
1188
|
+
let df = 0;
|
|
1189
|
+
for (const entry of index.scenes) {
|
|
1190
|
+
if (entry.doc.designators.all.has(designator)) {
|
|
1191
|
+
df += 1;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
index.designatorDfCache.set(designator, df);
|
|
1195
|
+
return df;
|
|
1196
|
+
}
|
|
1197
|
+
const IDF_SCALING_MIN_SCENES = 8;
|
|
1198
|
+
function computeIdfProfileForGroup(index, group) {
|
|
1199
|
+
const totalScenes = index.sceneCount;
|
|
1200
|
+
if (totalScenes < IDF_SCALING_MIN_SCENES) {
|
|
1201
|
+
return { multiplier: 1, docRatio: 0 };
|
|
1202
|
+
}
|
|
1203
|
+
let effectiveDf = getSceneTermDf(index, group.primary);
|
|
1204
|
+
if (group.designators && group.designators.length > 0) {
|
|
1205
|
+
let minDesigDf = Number.POSITIVE_INFINITY;
|
|
1206
|
+
for (const d of group.designators) {
|
|
1207
|
+
const dDf = getSceneDesignatorDf(index, d);
|
|
1208
|
+
if (dDf > 0 && dDf < minDesigDf) {
|
|
1209
|
+
minDesigDf = dDf;
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
if (Number.isFinite(minDesigDf)) {
|
|
1213
|
+
effectiveDf = effectiveDf > 0 ? Math.min(effectiveDf, minDesigDf) : minDesigDf;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
else if (effectiveDf === 0 && group.subTokens && group.subTokens.length >= 2) {
|
|
1217
|
+
// Upper-bound co-occurrence DF by the rarest required sub-token
|
|
1218
|
+
let minSubDf = Number.POSITIVE_INFINITY;
|
|
1219
|
+
for (const st of group.subTokens) {
|
|
1220
|
+
const sDf = getSceneTermDf(index, st);
|
|
1221
|
+
if (sDf < minSubDf) {
|
|
1222
|
+
minSubDf = sDf;
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
if (Number.isFinite(minSubDf)) {
|
|
1226
|
+
effectiveDf = minSubDf;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
const clampedDf = Math.max(1, effectiveDf);
|
|
1230
|
+
const docRatio = clampedDf / totalScenes;
|
|
1231
|
+
const normIdf = Math.log((totalScenes + 1) / (clampedDf + 0.5)) / Math.log(totalScenes + 1);
|
|
1232
|
+
let mult;
|
|
1233
|
+
if (docRatio > 0.4) {
|
|
1234
|
+
const t = Math.min(1, (docRatio - 0.4) / 0.6);
|
|
1235
|
+
mult = 0.62 - t * 0.27;
|
|
1236
|
+
}
|
|
1237
|
+
else {
|
|
1238
|
+
const scarcity = 1 - docRatio / 0.4;
|
|
1239
|
+
mult = 0.62 + 0.58 * scarcity + 1.0 * normIdf ** 1.35;
|
|
1240
|
+
}
|
|
1241
|
+
if (group.designators && group.designators.length > 0 && docRatio <= 0.25) {
|
|
1242
|
+
mult = Math.max(mult, 1.45);
|
|
1243
|
+
}
|
|
1244
|
+
return {
|
|
1245
|
+
multiplier: Math.max(0.35, Math.min(2.2, mult)),
|
|
1246
|
+
docRatio,
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
312
1249
|
const FIELD_DISPLAY_ORDER = [
|
|
313
1250
|
"id",
|
|
314
1251
|
"title",
|
|
@@ -316,6 +1253,7 @@ const FIELD_DISPLAY_ORDER = [
|
|
|
316
1253
|
"chain",
|
|
317
1254
|
"anchor",
|
|
318
1255
|
"repo",
|
|
1256
|
+
"source",
|
|
319
1257
|
"summary",
|
|
320
1258
|
"heading",
|
|
321
1259
|
"detail",
|
|
@@ -323,7 +1261,18 @@ const FIELD_DISPLAY_ORDER = [
|
|
|
323
1261
|
"code",
|
|
324
1262
|
"body",
|
|
325
1263
|
];
|
|
1264
|
+
const EMPTY_FIELD_SET = new Set();
|
|
326
1265
|
function scoreTermAgainstDoc(term, doc) {
|
|
1266
|
+
if (!doc.allTextLower.includes(term) &&
|
|
1267
|
+
!doc.reverseCandidatesLower.some((rc) => term.length > rc.length && term.includes(rc))) {
|
|
1268
|
+
return {
|
|
1269
|
+
bestScore: 0,
|
|
1270
|
+
fields: EMPTY_FIELD_SET,
|
|
1271
|
+
matchedHigherThanBody: false,
|
|
1272
|
+
matchedCodeOrDetail: false,
|
|
1273
|
+
matchedBodyText: false,
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
327
1276
|
let best = 0;
|
|
328
1277
|
const fields = new Set();
|
|
329
1278
|
let matchedHigherThanBody = false;
|
|
@@ -346,8 +1295,8 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
346
1295
|
}
|
|
347
1296
|
}
|
|
348
1297
|
// Tier 1: id / shortId, title, aliases
|
|
349
|
-
const idLower = doc.
|
|
350
|
-
const shortIdLower = doc.
|
|
1298
|
+
const idLower = doc.idLower;
|
|
1299
|
+
const shortIdLower = doc.shortIdLower;
|
|
351
1300
|
if (idLower === term || shortIdLower === term) {
|
|
352
1301
|
record(120, "id", "high");
|
|
353
1302
|
}
|
|
@@ -360,7 +1309,7 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
360
1309
|
else if (shortIdLower && shortIdLower.length >= 3 && term.includes(shortIdLower)) {
|
|
361
1310
|
record(58, "id", "high");
|
|
362
1311
|
}
|
|
363
|
-
const titleLower = doc.
|
|
1312
|
+
const titleLower = doc.titleLower;
|
|
364
1313
|
if (titleLower === term) {
|
|
365
1314
|
record(115, "title", "high");
|
|
366
1315
|
}
|
|
@@ -373,8 +1322,7 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
373
1322
|
else if (titleLower.length >= 2 && term.length >= 2 && term.includes(titleLower)) {
|
|
374
1323
|
record(58, "title", "high");
|
|
375
1324
|
}
|
|
376
|
-
for (const
|
|
377
|
-
const aliasLower = alias.toLowerCase();
|
|
1325
|
+
for (const aliasLower of doc.aliasesLower) {
|
|
378
1326
|
if (aliasLower === term) {
|
|
379
1327
|
record(110, "alias", "high");
|
|
380
1328
|
}
|
|
@@ -388,9 +1336,8 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
388
1336
|
record(56, "alias", "high");
|
|
389
1337
|
}
|
|
390
1338
|
}
|
|
391
|
-
// Tier 2: chainNames, anchors
|
|
392
|
-
for (const
|
|
393
|
-
const cLower = chainTerm.toLowerCase();
|
|
1339
|
+
// Tier 2: chainNames, anchors, repositories, sources
|
|
1340
|
+
for (const cLower of doc.chainTermsLower) {
|
|
394
1341
|
if (cLower === term) {
|
|
395
1342
|
record(95, "chain", "high");
|
|
396
1343
|
}
|
|
@@ -404,9 +1351,9 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
404
1351
|
record(46, "chain", "high");
|
|
405
1352
|
}
|
|
406
1353
|
}
|
|
407
|
-
for (const anchorTerm of doc.anchorTerms
|
|
408
|
-
const aLower =
|
|
409
|
-
const baseName =
|
|
1354
|
+
for (const anchorTerm of doc.anchorTerms) {
|
|
1355
|
+
const aLower = anchorTerm.lower;
|
|
1356
|
+
const baseName = anchorTerm.base;
|
|
410
1357
|
if (aLower === term || baseName === term) {
|
|
411
1358
|
record(92, "anchor", "high");
|
|
412
1359
|
}
|
|
@@ -420,9 +1367,9 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
420
1367
|
record(44, "anchor", "high");
|
|
421
1368
|
}
|
|
422
1369
|
}
|
|
423
|
-
for (const repoTerm of doc.repoTerms
|
|
424
|
-
const rLower =
|
|
425
|
-
const repoBase =
|
|
1370
|
+
for (const repoTerm of doc.repoTerms) {
|
|
1371
|
+
const rLower = repoTerm.lower;
|
|
1372
|
+
const repoBase = repoTerm.base;
|
|
426
1373
|
if (rLower === term || repoBase === term) {
|
|
427
1374
|
record(82, "repo", "high");
|
|
428
1375
|
}
|
|
@@ -433,34 +1380,21 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
433
1380
|
record(38, "repo", "high");
|
|
434
1381
|
}
|
|
435
1382
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
else if (summaryLower.includes(term)) {
|
|
442
|
-
record(36, "summary", "high");
|
|
443
|
-
}
|
|
444
|
-
for (const heading of doc.headings ?? []) {
|
|
445
|
-
const hLower = heading.toLowerCase();
|
|
446
|
-
if (hLower === term) {
|
|
447
|
-
record(56, "heading", "high");
|
|
448
|
-
}
|
|
449
|
-
else if (hLower.includes(term)) {
|
|
450
|
-
record(34, "heading", "high");
|
|
1383
|
+
for (const srcTerm of doc.sourceTerms) {
|
|
1384
|
+
const sLower = srcTerm.lower;
|
|
1385
|
+
const srcBase = srcTerm.base;
|
|
1386
|
+
if (sLower === term || srcBase === term) {
|
|
1387
|
+
record(82, "source", "high");
|
|
451
1388
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const dLower = dTitle.toLowerCase();
|
|
455
|
-
if (dLower === term) {
|
|
456
|
-
record(62, "detail", "high");
|
|
1389
|
+
else if (sLower.includes(term)) {
|
|
1390
|
+
record(44, "source", "high");
|
|
457
1391
|
}
|
|
458
|
-
else if (
|
|
459
|
-
record(38, "
|
|
1392
|
+
else if (srcBase.length >= 3 && term.includes(srcBase)) {
|
|
1393
|
+
record(38, "source", "high");
|
|
460
1394
|
}
|
|
461
1395
|
}
|
|
462
|
-
|
|
463
|
-
|
|
1396
|
+
// Domain child high-tier terms (include reverse substring check)
|
|
1397
|
+
for (const chLower of doc.childHighTermsLower) {
|
|
464
1398
|
if (chLower === term) {
|
|
465
1399
|
record(72, "scene", "high");
|
|
466
1400
|
}
|
|
@@ -474,54 +1408,81 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
474
1408
|
record(38, "scene", "high");
|
|
475
1409
|
}
|
|
476
1410
|
}
|
|
477
|
-
for (const chAnchor of doc.childAnchorTerms
|
|
478
|
-
|
|
479
|
-
const baseName = aLower.split("/").pop() ?? aLower;
|
|
480
|
-
if (aLower === term || baseName === term) {
|
|
1411
|
+
for (const chAnchor of doc.childAnchorTerms) {
|
|
1412
|
+
if (chAnchor.lower === term || chAnchor.base === term) {
|
|
481
1413
|
record(64, "anchor", "high");
|
|
482
1414
|
}
|
|
483
|
-
else if (
|
|
1415
|
+
else if (chAnchor.lower.includes(term)) {
|
|
484
1416
|
record(38, "anchor", "high");
|
|
485
1417
|
}
|
|
486
1418
|
}
|
|
487
|
-
for (const chRepo of doc.childRepoTerms
|
|
488
|
-
|
|
489
|
-
const repoBase = rLower.split("/").pop() ?? rLower;
|
|
490
|
-
if (rLower === term || repoBase === term) {
|
|
1419
|
+
for (const chRepo of doc.childRepoTerms) {
|
|
1420
|
+
if (chRepo.lower === term || chRepo.base === term) {
|
|
491
1421
|
record(58, "repo", "high");
|
|
492
1422
|
}
|
|
493
|
-
else if (
|
|
1423
|
+
else if (chRepo.lower.includes(term)) {
|
|
494
1424
|
record(34, "repo", "high");
|
|
495
1425
|
}
|
|
496
1426
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
1427
|
+
for (const chSrc of doc.childSourceTerms) {
|
|
1428
|
+
if (chSrc.lower === term || chSrc.base === term) {
|
|
1429
|
+
record(58, "source", "high");
|
|
1430
|
+
}
|
|
1431
|
+
else if (chSrc.lower.includes(term)) {
|
|
1432
|
+
record(34, "source", "high");
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
// Tier 3 & Tier 4: gated by single O(1) pre-concatenated lowercase check
|
|
1436
|
+
if (doc.tier34GateLower.includes(term)) {
|
|
1437
|
+
const summaryLower = doc.summaryLower;
|
|
1438
|
+
if (summaryLower === term) {
|
|
1439
|
+
record(60, "summary", "high");
|
|
1440
|
+
}
|
|
1441
|
+
else if (summaryLower.includes(term)) {
|
|
1442
|
+
record(36, "summary", "high");
|
|
1443
|
+
}
|
|
1444
|
+
for (const hLower of doc.headingsLower) {
|
|
1445
|
+
if (hLower === term) {
|
|
1446
|
+
record(56, "heading", "high");
|
|
504
1447
|
}
|
|
505
|
-
else if (
|
|
506
|
-
record(
|
|
1448
|
+
else if (hLower.includes(term)) {
|
|
1449
|
+
record(34, "heading", "high");
|
|
507
1450
|
}
|
|
508
1451
|
}
|
|
509
|
-
for (const
|
|
510
|
-
if (
|
|
511
|
-
record(
|
|
512
|
-
break;
|
|
1452
|
+
for (const dLower of doc.detailTitlesLower) {
|
|
1453
|
+
if (dLower === term) {
|
|
1454
|
+
record(62, "detail", "high");
|
|
513
1455
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
if (dBody.toLowerCase().includes(term)) {
|
|
517
|
-
record(14, "detail", "code");
|
|
518
|
-
break;
|
|
1456
|
+
else if (dLower.includes(term)) {
|
|
1457
|
+
record(38, "detail", "high");
|
|
519
1458
|
}
|
|
520
1459
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
1460
|
+
if (term.length >= 2) {
|
|
1461
|
+
for (const span of doc.codeSpans) {
|
|
1462
|
+
if (span.lower === term || span.parts.includes(term)) {
|
|
1463
|
+
record(28, "code", "code");
|
|
1464
|
+
}
|
|
1465
|
+
else if (span.lower.includes(term)) {
|
|
1466
|
+
record(22, "code", "code");
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
for (const bodyLower of doc.bodySourcesLower) {
|
|
1470
|
+
if (bodyLower.includes(term)) {
|
|
1471
|
+
record(16, "body", "body");
|
|
1472
|
+
break;
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
for (const dBodyLower of doc.detailBodySourcesLower) {
|
|
1476
|
+
if (dBodyLower.includes(term)) {
|
|
1477
|
+
record(14, "detail", "code");
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
for (const cBodyLower of doc.childBodySourcesLower) {
|
|
1482
|
+
if (cBodyLower.includes(term)) {
|
|
1483
|
+
record(12, "body", "body");
|
|
1484
|
+
break;
|
|
1485
|
+
}
|
|
525
1486
|
}
|
|
526
1487
|
}
|
|
527
1488
|
}
|
|
@@ -533,25 +1494,268 @@ function scoreTermAgainstDoc(term, doc) {
|
|
|
533
1494
|
matchedBodyText,
|
|
534
1495
|
};
|
|
535
1496
|
}
|
|
536
|
-
function
|
|
1497
|
+
function scoreDesignatorsAgainstDoc(designators, doc) {
|
|
1498
|
+
let best = 0;
|
|
1499
|
+
const fields = new Set();
|
|
1500
|
+
let matchedHigherThanBody = false;
|
|
1501
|
+
let matchedCodeOrDetail = false;
|
|
1502
|
+
let matchedBodyText = false;
|
|
1503
|
+
let matchedSnippetTerm;
|
|
1504
|
+
function record(score, field, tier, d) {
|
|
1505
|
+
best = Math.max(best, score);
|
|
1506
|
+
fields.add(field);
|
|
1507
|
+
if (tier === "high") {
|
|
1508
|
+
matchedHigherThanBody = true;
|
|
1509
|
+
}
|
|
1510
|
+
else if (tier === "code") {
|
|
1511
|
+
matchedCodeOrDetail = true;
|
|
1512
|
+
matchedSnippetTerm ??= doc.designators.rawByDesignator.get(d);
|
|
1513
|
+
}
|
|
1514
|
+
else {
|
|
1515
|
+
matchedBodyText = true;
|
|
1516
|
+
matchedSnippetTerm ??= doc.designators.rawByDesignator.get(d);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
for (let idx = 0; idx < designators.length; idx++) {
|
|
1520
|
+
const d = designators[idx];
|
|
1521
|
+
if (!doc.designators.all.has(d)) {
|
|
1522
|
+
continue;
|
|
1523
|
+
}
|
|
1524
|
+
// Slight discount only when matching a stripped sub-designator (e.g. `r44972` from `CRS-R44972`)
|
|
1525
|
+
const weight = idx === 0 ? 1 : 0.92;
|
|
1526
|
+
const tiers = doc.designators;
|
|
1527
|
+
if (tiers.id.has(d)) {
|
|
1528
|
+
const idExact = doc.idCollapsed === d || doc.shortIdCollapsed === d;
|
|
1529
|
+
record(Math.round((idExact ? 115 : 66) * weight), "id", "high", d);
|
|
1530
|
+
}
|
|
1531
|
+
if (tiers.title.has(d)) {
|
|
1532
|
+
const titleExact = doc.titleCollapsed === d;
|
|
1533
|
+
record(Math.round((titleExact ? 112 : 78) * weight), "title", "high", d);
|
|
1534
|
+
}
|
|
1535
|
+
if (tiers.alias.has(d)) {
|
|
1536
|
+
record(Math.round(108 * weight), "alias", "high", d);
|
|
1537
|
+
}
|
|
1538
|
+
if (tiers.chain.has(d)) {
|
|
1539
|
+
record(Math.round(94 * weight), "chain", "high", d);
|
|
1540
|
+
}
|
|
1541
|
+
if (tiers.anchor.has(d)) {
|
|
1542
|
+
record(Math.round(92 * weight), "anchor", "high", d);
|
|
1543
|
+
}
|
|
1544
|
+
if (tiers.repo.has(d)) {
|
|
1545
|
+
record(Math.round(80 * weight), "repo", "high", d);
|
|
1546
|
+
}
|
|
1547
|
+
if (tiers.source.has(d)) {
|
|
1548
|
+
record(Math.round(80 * weight), "source", "high", d);
|
|
1549
|
+
}
|
|
1550
|
+
if (tiers.scene.has(d)) {
|
|
1551
|
+
record(Math.round(70 * weight), "scene", "high", d);
|
|
1552
|
+
}
|
|
1553
|
+
if (tiers.detail.has(d)) {
|
|
1554
|
+
record(Math.round(60 * weight), "detail", "high", d);
|
|
1555
|
+
}
|
|
1556
|
+
if (tiers.summary.has(d)) {
|
|
1557
|
+
record(Math.round(56 * weight), "summary", "high", d);
|
|
1558
|
+
}
|
|
1559
|
+
if (tiers.heading.has(d)) {
|
|
1560
|
+
record(Math.round(52 * weight), "heading", "high", d);
|
|
1561
|
+
}
|
|
1562
|
+
if (tiers.code.has(d)) {
|
|
1563
|
+
record(Math.round(48 * weight), "code", "code", d);
|
|
1564
|
+
}
|
|
1565
|
+
if (tiers.body.has(d)) {
|
|
1566
|
+
// Formal model/report designators in body tables/prose carry much stronger identity signal than casual words
|
|
1567
|
+
record(Math.round(46 * weight), "body", "body", d);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return {
|
|
1571
|
+
hit: {
|
|
1572
|
+
bestScore: best,
|
|
1573
|
+
fields,
|
|
1574
|
+
matchedHigherThanBody,
|
|
1575
|
+
matchedCodeOrDetail,
|
|
1576
|
+
matchedBodyText,
|
|
1577
|
+
},
|
|
1578
|
+
...(matchedSnippetTerm ? { matchedSnippetTerm } : {}),
|
|
1579
|
+
};
|
|
1580
|
+
}
|
|
1581
|
+
function mergeTermHits(a, b) {
|
|
1582
|
+
if (b.bestScore === 0) {
|
|
1583
|
+
return a;
|
|
1584
|
+
}
|
|
1585
|
+
if (a.bestScore === 0) {
|
|
1586
|
+
return b;
|
|
1587
|
+
}
|
|
1588
|
+
const mergedFields = new Set([...a.fields, ...b.fields]);
|
|
1589
|
+
return {
|
|
1590
|
+
bestScore: Math.max(a.bestScore, b.bestScore),
|
|
1591
|
+
fields: mergedFields,
|
|
1592
|
+
matchedHigherThanBody: a.matchedHigherThanBody || b.matchedHigherThanBody,
|
|
1593
|
+
matchedCodeOrDetail: a.matchedCodeOrDetail || b.matchedCodeOrDetail,
|
|
1594
|
+
matchedBodyText: a.matchedBodyText || b.matchedBodyText,
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
function pinpointSubSceneMatches(doc, pinpointTerms, pinpointDesignators) {
|
|
1598
|
+
const matchedChains = [];
|
|
1599
|
+
const matchedDetails = [];
|
|
1600
|
+
const matchedAnchors = [];
|
|
1601
|
+
if (pinpointTerms.length === 0 && pinpointDesignators.size === 0) {
|
|
1602
|
+
return { matchedChains, matchedDetails, matchedAnchors };
|
|
1603
|
+
}
|
|
1604
|
+
const usableTerms = pinpointTerms.filter((t) => t.length >= 2);
|
|
1605
|
+
for (const chain of doc.chainEntries ?? []) {
|
|
1606
|
+
let hit = false;
|
|
1607
|
+
for (const d of pinpointDesignators) {
|
|
1608
|
+
if (chain.designators.has(d)) {
|
|
1609
|
+
hit = true;
|
|
1610
|
+
break;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (!hit) {
|
|
1614
|
+
for (const term of usableTerms) {
|
|
1615
|
+
if (chain.termsLower.some((ct) => ct.includes(term) || (ct.length >= 3 && term.includes(ct))) ||
|
|
1616
|
+
chain.bodyLower.includes(term)) {
|
|
1617
|
+
hit = true;
|
|
1618
|
+
break;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
if (hit) {
|
|
1623
|
+
matchedChains.push(chain.title);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
for (const detail of doc.detailEntries ?? []) {
|
|
1627
|
+
let hit = false;
|
|
1628
|
+
for (const d of pinpointDesignators) {
|
|
1629
|
+
if (detail.designators.has(d)) {
|
|
1630
|
+
hit = true;
|
|
1631
|
+
break;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
if (!hit) {
|
|
1635
|
+
for (const term of usableTerms) {
|
|
1636
|
+
if (detail.nameLower.includes(term) ||
|
|
1637
|
+
detail.titleLower.includes(term) ||
|
|
1638
|
+
detail.contentLower.includes(term)) {
|
|
1639
|
+
hit = true;
|
|
1640
|
+
break;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
if (hit) {
|
|
1645
|
+
matchedDetails.push(detail.name);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
for (const ae of doc.anchorEntries ?? []) {
|
|
1649
|
+
let hit = false;
|
|
1650
|
+
for (const d of pinpointDesignators) {
|
|
1651
|
+
if (ae.designators.has(d)) {
|
|
1652
|
+
hit = true;
|
|
1653
|
+
break;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
if (!hit) {
|
|
1657
|
+
for (const term of usableTerms) {
|
|
1658
|
+
if (ae.terms.some((at) => at.lower.includes(term) ||
|
|
1659
|
+
at.base === term ||
|
|
1660
|
+
(at.base.length >= 3 && term.includes(at.base)))) {
|
|
1661
|
+
hit = true;
|
|
1662
|
+
break;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
if (hit) {
|
|
1667
|
+
matchedAnchors.push(ae.summary);
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
return { matchedChains, matchedDetails, matchedAnchors };
|
|
1671
|
+
}
|
|
1672
|
+
function buildPreparedQueryPlan(query, index) {
|
|
537
1673
|
const tokenized = tokenizeDiscoveryQuery(query);
|
|
1674
|
+
const useCorpusIdf = index.sceneCount >= IDF_SCALING_MIN_SCENES;
|
|
1675
|
+
const groupIdf = tokenized.groups.map((g) => computeIdfProfileForGroup(index, g));
|
|
1676
|
+
let phraseIdfMultiplier = 1;
|
|
1677
|
+
const selectiveBigrams = [];
|
|
1678
|
+
if (useCorpusIdf && tokenized.normalizedPhrase) {
|
|
1679
|
+
const phraseDf = Math.max(1, getSceneTermDf(index, tokenized.normalizedPhrase));
|
|
1680
|
+
const phraseRatio = phraseDf / index.sceneCount;
|
|
1681
|
+
if (phraseRatio > 0.4) {
|
|
1682
|
+
phraseIdfMultiplier = Math.max(0.35, 0.62 - ((phraseRatio - 0.4) / 0.6) * 0.27);
|
|
1683
|
+
}
|
|
1684
|
+
else if (tokenized.groups.length === 1 && groupIdf[0]) {
|
|
1685
|
+
phraseIdfMultiplier = groupIdf[0].multiplier;
|
|
1686
|
+
}
|
|
1687
|
+
else {
|
|
1688
|
+
const avgMult = groupIdf.reduce((acc, g) => acc + g.multiplier, 0) / Math.max(1, groupIdf.length);
|
|
1689
|
+
phraseIdfMultiplier = Math.max(0.6, Math.min(2.2, avgMult));
|
|
1690
|
+
}
|
|
1691
|
+
// Extract selective adjacent non-designator 2-word concept phrases (e.g. "program performance", "bar charts")
|
|
1692
|
+
const seenPhrases = new Set();
|
|
1693
|
+
for (let i = 0; i < tokenized.groups.length - 1; i++) {
|
|
1694
|
+
const g1 = tokenized.groups[i];
|
|
1695
|
+
const g2 = tokenized.groups[i + 1];
|
|
1696
|
+
if (g1.designators?.length || g2.designators?.length) {
|
|
1697
|
+
continue;
|
|
1698
|
+
}
|
|
1699
|
+
const w1 = g1.primary;
|
|
1700
|
+
const w2 = g2.primary;
|
|
1701
|
+
if (w1.length < 2 || w2.length < 2 || isPureCjk(w1) || isPureCjk(w2)) {
|
|
1702
|
+
continue;
|
|
1703
|
+
}
|
|
1704
|
+
const phrase = `${w1} ${w2}`;
|
|
1705
|
+
if (seenPhrases.has(phrase)) {
|
|
1706
|
+
continue;
|
|
1707
|
+
}
|
|
1708
|
+
seenPhrases.add(phrase);
|
|
1709
|
+
const biDf = getSceneTermDf(index, phrase);
|
|
1710
|
+
if (biDf > 0 && biDf / index.sceneCount <= 0.25) {
|
|
1711
|
+
const biScarcity = 1 - biDf / (index.sceneCount * 0.3);
|
|
1712
|
+
const bodyBoost = Math.max(32, Math.round(78 * biScarcity));
|
|
1713
|
+
selectiveBigrams.push({
|
|
1714
|
+
phrase,
|
|
1715
|
+
bodyBoost,
|
|
1716
|
+
highBoost: bodyBoost + 26,
|
|
1717
|
+
});
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
return {
|
|
1722
|
+
tokenized,
|
|
1723
|
+
groupIdf,
|
|
1724
|
+
phraseIdfMultiplier,
|
|
1725
|
+
useCorpusIdf,
|
|
1726
|
+
selectiveBigrams,
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
function evaluatePreparedQuery(plan, doc, includePinpoint) {
|
|
1730
|
+
const { tokenized, groupIdf, phraseIdfMultiplier, useCorpusIdf } = plan;
|
|
538
1731
|
if (!tokenized.normalizedPhrase) {
|
|
539
1732
|
return { score: 1, matchedFields: [] };
|
|
540
1733
|
}
|
|
541
1734
|
const allMatchedFields = new Set();
|
|
542
1735
|
const bodySnippetTerms = [];
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
1736
|
+
const specificPinpointTerms = [];
|
|
1737
|
+
const fallbackPinpointTerms = [];
|
|
1738
|
+
const specificPinpointDesignators = new Set();
|
|
1739
|
+
const fallbackPinpointDesignators = new Set();
|
|
1740
|
+
const rawPhraseHit = scoreTermAgainstDoc(tokenized.normalizedPhrase, doc);
|
|
1741
|
+
const weightedPhraseScore = rawPhraseHit.bestScore > 0
|
|
1742
|
+
? Math.max(1, Math.round(rawPhraseHit.bestScore * phraseIdfMultiplier))
|
|
1743
|
+
: 0;
|
|
1744
|
+
if (rawPhraseHit.bestScore > 0) {
|
|
1745
|
+
for (const f of rawPhraseHit.fields) {
|
|
546
1746
|
allMatchedFields.add(f);
|
|
547
1747
|
}
|
|
548
|
-
if (
|
|
1748
|
+
if (rawPhraseHit.matchedBodyText || rawPhraseHit.matchedCodeOrDetail) {
|
|
549
1749
|
bodySnippetTerms.push(tokenized.normalizedPhrase);
|
|
550
1750
|
}
|
|
551
1751
|
}
|
|
552
1752
|
const groupScores = [];
|
|
553
1753
|
let matchedGroupCount = 0;
|
|
554
|
-
|
|
1754
|
+
let specificMatchedGroupCount = 0;
|
|
1755
|
+
for (let gIdx = 0; gIdx < tokenized.groups.length; gIdx++) {
|
|
1756
|
+
const group = tokenized.groups[gIdx];
|
|
1757
|
+
const idfProfile = groupIdf[gIdx] ?? { multiplier: 1, docRatio: 0 };
|
|
1758
|
+
const isSpecificGroup = !useCorpusIdf || idfProfile.docRatio <= 0.35;
|
|
555
1759
|
let primaryHit;
|
|
556
1760
|
let effectivePrimaryTerm = group.primary;
|
|
557
1761
|
if (group.isStrippedStem && group.rawToken) {
|
|
@@ -572,6 +1776,25 @@ function evaluateDiscoveryQuery(query, doc, allowSubTokenFallback = false) {
|
|
|
572
1776
|
else {
|
|
573
1777
|
primaryHit = scoreTermAgainstDoc(group.primary, doc);
|
|
574
1778
|
}
|
|
1779
|
+
// Match canonical designator variants ("FFG-62" <-> "FFG 62" <-> "FFG62", "CRS-R44972" <-> "R44972") at full tier strength
|
|
1780
|
+
if (group.designators && group.designators.length > 0) {
|
|
1781
|
+
const desigEval = scoreDesignatorsAgainstDoc(group.designators, doc);
|
|
1782
|
+
if (desigEval.hit.bestScore > 0) {
|
|
1783
|
+
primaryHit = mergeTermHits(primaryHit, desigEval.hit);
|
|
1784
|
+
if (desigEval.matchedSnippetTerm) {
|
|
1785
|
+
effectivePrimaryTerm = desigEval.matchedSnippetTerm;
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
// Also normalize single-letter hyphenated acronyms ("M-SHORAD" <-> "M SHORAD")
|
|
1790
|
+
if (primaryHit.bestScore === 0 && /^[a-z]-[a-z]{4,}$/.test(group.primary)) {
|
|
1791
|
+
const spacedAcronym = group.primary.replace(/-/g, " ");
|
|
1792
|
+
const spacedHit = scoreTermAgainstDoc(spacedAcronym, doc);
|
|
1793
|
+
if (spacedHit.bestScore > 0) {
|
|
1794
|
+
primaryHit = spacedHit;
|
|
1795
|
+
effectivePrimaryTerm = spacedAcronym;
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
575
1798
|
let bestTriScore = 0;
|
|
576
1799
|
let secondTriScore = 0;
|
|
577
1800
|
const triFields = new Set();
|
|
@@ -620,50 +1843,61 @@ function evaluateDiscoveryQuery(query, doc, allowSubTokenFallback = false) {
|
|
|
620
1843
|
const coBiBonus = usableBiHits.length >= 2 ? (usableBiHits.length - 1) * 12 : 0;
|
|
621
1844
|
const rawBi = Math.round((topBiSum * 0.38 + coBiBonus) * stemDiscount);
|
|
622
1845
|
if (usableBiHits.length === 1) {
|
|
623
|
-
// Require raw token or >= 2 CJK bigrams for high-score hits; single bigram is low-weight fallback
|
|
624
1846
|
weightedBi = Math.min(rawBi, 28);
|
|
625
1847
|
}
|
|
626
1848
|
else {
|
|
627
1849
|
weightedBi = rawBi;
|
|
628
1850
|
}
|
|
629
1851
|
}
|
|
630
|
-
//
|
|
1852
|
+
// Per-group identifier sub-token co-occurrence fallback whenever primaryHit.bestScore === 0
|
|
631
1853
|
const subTokHits = [];
|
|
632
|
-
if (
|
|
633
|
-
primaryHit.bestScore === 0 &&
|
|
634
|
-
group.subTokens &&
|
|
635
|
-
group.subTokens.length >= 2) {
|
|
1854
|
+
if (primaryHit.bestScore === 0 && group.subTokens && group.subTokens.length >= 2) {
|
|
636
1855
|
for (const st of group.subTokens) {
|
|
637
1856
|
const h = scoreTermAgainstDoc(st, doc);
|
|
638
1857
|
if (h.bestScore > 0) {
|
|
639
|
-
|
|
1858
|
+
// Do not treat a parent domainId prefix match as an 86-pt sceneId prefix during subToken decomposition
|
|
1859
|
+
const onlyDomainPrefixId = Boolean(doc.shortIdLower) &&
|
|
1860
|
+
!st.includes("/") &&
|
|
1861
|
+
!doc.shortIdLower.includes(st) &&
|
|
1862
|
+
h.fields.size === 1 &&
|
|
1863
|
+
h.fields.has("id");
|
|
1864
|
+
const adjustedHit = onlyDomainPrefixId
|
|
1865
|
+
? { ...h, bestScore: Math.min(h.bestScore, 42) }
|
|
1866
|
+
: h;
|
|
1867
|
+
subTokHits.push({ term: st, hit: adjustedHit });
|
|
640
1868
|
}
|
|
641
1869
|
}
|
|
642
1870
|
}
|
|
643
1871
|
let weightedSubTok = 0;
|
|
644
1872
|
if (subTokHits.length >= 2) {
|
|
645
1873
|
subTokHits.sort((a, b) => b.hit.bestScore - a.hit.bestScore);
|
|
646
|
-
const
|
|
647
|
-
const
|
|
648
|
-
|
|
1874
|
+
const leadSub = subTokHits[0]?.hit.bestScore ?? 0;
|
|
1875
|
+
const restSubSum = subTokHits.slice(1, 3).reduce((acc, cur) => acc + cur.hit.bestScore, 0);
|
|
1876
|
+
const allPartsMatched = Boolean(group.subTokens && subTokHits.length === group.subTokens.length);
|
|
1877
|
+
const coSubBonus = (subTokHits.length - 1) * 14 + (allPartsMatched ? 6 : 0);
|
|
1878
|
+
// Cap below verbatim anchor/chain/alias/title/id matches (>= 92) while beating single noisy words
|
|
1879
|
+
weightedSubTok = Math.min(88, Math.round(leadSub * 0.62 + restSubSum * 0.45) + coSubBonus);
|
|
649
1880
|
}
|
|
650
1881
|
const subGramScore = Math.max(weightedTri, weightedBi, weightedSubTok);
|
|
651
|
-
let
|
|
1882
|
+
let rawGroupScore = 0;
|
|
1883
|
+
const matchedTermsForGroup = [];
|
|
652
1884
|
if (primaryHit.bestScore > 0) {
|
|
653
|
-
|
|
1885
|
+
rawGroupScore = primaryHit.bestScore + (subGramScore > primaryHit.bestScore ? 8 : 0);
|
|
654
1886
|
for (const f of primaryHit.fields) {
|
|
655
1887
|
allMatchedFields.add(f);
|
|
656
1888
|
}
|
|
1889
|
+
matchedTermsForGroup.push(effectivePrimaryTerm);
|
|
657
1890
|
if (primaryHit.matchedBodyText || primaryHit.matchedCodeOrDetail) {
|
|
658
1891
|
bodySnippetTerms.push(effectivePrimaryTerm);
|
|
659
1892
|
}
|
|
660
1893
|
}
|
|
661
1894
|
else if (subGramScore > 0) {
|
|
662
|
-
|
|
1895
|
+
rawGroupScore = subGramScore;
|
|
663
1896
|
if (weightedTri >= weightedBi && weightedTri >= weightedSubTok) {
|
|
664
1897
|
for (const f of triFields) {
|
|
665
1898
|
allMatchedFields.add(f);
|
|
666
1899
|
}
|
|
1900
|
+
matchedTermsForGroup.push(...triBodyTerms, ...group.threeGrams);
|
|
667
1901
|
bodySnippetTerms.push(...triBodyTerms);
|
|
668
1902
|
}
|
|
669
1903
|
else if (weightedSubTok > weightedBi) {
|
|
@@ -671,6 +1905,7 @@ function evaluateDiscoveryQuery(query, doc, allowSubTokenFallback = false) {
|
|
|
671
1905
|
for (const f of st.hit.fields) {
|
|
672
1906
|
allMatchedFields.add(f);
|
|
673
1907
|
}
|
|
1908
|
+
matchedTermsForGroup.push(st.term);
|
|
674
1909
|
if (st.hit.matchedBodyText || st.hit.matchedCodeOrDetail) {
|
|
675
1910
|
bodySnippetTerms.push(st.term);
|
|
676
1911
|
}
|
|
@@ -681,115 +1916,173 @@ function evaluateDiscoveryQuery(query, doc, allowSubTokenFallback = false) {
|
|
|
681
1916
|
for (const f of b.hit.fields) {
|
|
682
1917
|
allMatchedFields.add(f);
|
|
683
1918
|
}
|
|
1919
|
+
matchedTermsForGroup.push(b.term);
|
|
684
1920
|
if (b.hit.matchedBodyText || b.hit.matchedCodeOrDetail) {
|
|
685
1921
|
bodySnippetTerms.push(b.term);
|
|
686
1922
|
}
|
|
687
1923
|
}
|
|
688
1924
|
}
|
|
689
1925
|
}
|
|
690
|
-
if (
|
|
1926
|
+
if (rawGroupScore > 0) {
|
|
691
1927
|
matchedGroupCount += 1;
|
|
692
|
-
|
|
1928
|
+
if (isSpecificGroup) {
|
|
1929
|
+
specificMatchedGroupCount += 1;
|
|
1930
|
+
specificPinpointTerms.push(...matchedTermsForGroup);
|
|
1931
|
+
for (const d of group.designators ?? []) {
|
|
1932
|
+
specificPinpointDesignators.add(d);
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
else {
|
|
1936
|
+
fallbackPinpointTerms.push(...matchedTermsForGroup);
|
|
1937
|
+
for (const d of group.designators ?? []) {
|
|
1938
|
+
fallbackPinpointDesignators.add(d);
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
const scaledScore = Math.max(1, Math.round(rawGroupScore * idfProfile.multiplier));
|
|
1942
|
+
groupScores.push(scaledScore);
|
|
693
1943
|
}
|
|
694
1944
|
}
|
|
695
|
-
if (matchedGroupCount === 0 &&
|
|
1945
|
+
if (matchedGroupCount === 0 && weightedPhraseScore === 0) {
|
|
696
1946
|
return { score: 0, matchedFields: [] };
|
|
697
1947
|
}
|
|
698
|
-
// Sum top-K matched token groups without linear division by tokens.length (no multi-token dilution!)
|
|
699
1948
|
groupScores.sort((a, b) => b - a);
|
|
700
1949
|
const topGroupSum = groupScores.slice(0, 4).reduce((sum, s) => sum + s, 0);
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
1950
|
+
// On >= 8 scene wikis, only specific (non-ubiquitous) matched groups earn full +25 coordination bonus
|
|
1951
|
+
let multiTokenBonus = 0;
|
|
1952
|
+
if (!useCorpusIdf) {
|
|
1953
|
+
multiTokenBonus =
|
|
1954
|
+
matchedGroupCount >= 2
|
|
1955
|
+
? (matchedGroupCount - 1) * 25 + (matchedGroupCount === tokenized.groups.length ? 15 : 0)
|
|
1956
|
+
: 0;
|
|
1957
|
+
}
|
|
1958
|
+
else {
|
|
1959
|
+
const specificBonus = specificMatchedGroupCount >= 2 ? (specificMatchedGroupCount - 1) * 25 : 0;
|
|
1960
|
+
const crossSupportBonus = specificMatchedGroupCount >= 1 && matchedGroupCount > specificMatchedGroupCount
|
|
1961
|
+
? Math.min(18, (matchedGroupCount - specificMatchedGroupCount) * 6)
|
|
1962
|
+
: 0;
|
|
1963
|
+
const fullMatchBonus = specificMatchedGroupCount >= 1 && matchedGroupCount === tokenized.groups.length ? 12 : 0;
|
|
1964
|
+
multiTokenBonus = specificBonus + crossSupportBonus + fullMatchBonus;
|
|
1965
|
+
}
|
|
1966
|
+
let selectivePhraseBonus = 0;
|
|
1967
|
+
for (const sb of plan.selectiveBigrams) {
|
|
1968
|
+
if (!doc.allTextLower.includes(sb.phrase)) {
|
|
1969
|
+
continue;
|
|
1970
|
+
}
|
|
1971
|
+
const inHighField = doc.titleLower.includes(sb.phrase) ||
|
|
1972
|
+
doc.summaryLower.includes(sb.phrase) ||
|
|
1973
|
+
doc.headingsLower.some((h) => h.includes(sb.phrase)) ||
|
|
1974
|
+
doc.chainTermsLower.some((c) => c.includes(sb.phrase));
|
|
1975
|
+
selectivePhraseBonus += inHighField ? sb.highBoost : sb.bodyBoost;
|
|
1976
|
+
bodySnippetTerms.push(sb.phrase);
|
|
1977
|
+
}
|
|
1978
|
+
const combinedScore = Math.max(weightedPhraseScore, topGroupSum +
|
|
705
1979
|
multiTokenBonus +
|
|
706
|
-
|
|
1980
|
+
selectivePhraseBonus +
|
|
1981
|
+
(tokenized.groups.length > 1 ? Math.round(weightedPhraseScore * 0.35) : 0));
|
|
707
1982
|
const orderedFields = FIELD_DISPLAY_ORDER.filter((f) => allMatchedFields.has(f));
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
1983
|
+
let activeTerms;
|
|
1984
|
+
let activeDesigs;
|
|
1985
|
+
if (includePinpoint) {
|
|
1986
|
+
activeTerms =
|
|
1987
|
+
specificPinpointTerms.length > 0 || specificPinpointDesignators.size > 0
|
|
1988
|
+
? specificPinpointTerms
|
|
1989
|
+
: fallbackPinpointTerms;
|
|
1990
|
+
activeDesigs =
|
|
1991
|
+
specificPinpointTerms.length > 0 || specificPinpointDesignators.size > 0
|
|
1992
|
+
? specificPinpointDesignators
|
|
1993
|
+
: fallbackPinpointDesignators;
|
|
1994
|
+
}
|
|
716
1995
|
return {
|
|
717
1996
|
score: combinedScore,
|
|
718
1997
|
matchedFields: orderedFields,
|
|
719
|
-
...(
|
|
1998
|
+
...(bodySnippetTerms.length > 0 ? { bodySnippetTerms } : {}),
|
|
1999
|
+
...(activeTerms && activeTerms.length > 0 ? { pinpointTerms: activeTerms } : {}),
|
|
2000
|
+
...(activeDesigs && activeDesigs.size > 0 ? { pinpointDesignators: activeDesigs } : {}),
|
|
720
2001
|
};
|
|
721
2002
|
}
|
|
2003
|
+
function collectActiveQueries(query, queries) {
|
|
2004
|
+
const active = [];
|
|
2005
|
+
if (query?.trim()) {
|
|
2006
|
+
active.push(query.trim());
|
|
2007
|
+
}
|
|
2008
|
+
for (const q of queries ?? []) {
|
|
2009
|
+
const trimmed = q.trim();
|
|
2010
|
+
if (trimmed && !active.includes(trimmed)) {
|
|
2011
|
+
active.push(trimmed);
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
return active;
|
|
2015
|
+
}
|
|
2016
|
+
function filterWeakBodyOnlyHits(items) {
|
|
2017
|
+
if (items.length <= 1) {
|
|
2018
|
+
return items;
|
|
2019
|
+
}
|
|
2020
|
+
let topStructuredScore = 0;
|
|
2021
|
+
for (const item of items) {
|
|
2022
|
+
const hasStructuredField = item.matchedFields?.some((f) => f !== "body") ?? false;
|
|
2023
|
+
if (hasStructuredField && (item.score ?? 0) > topStructuredScore) {
|
|
2024
|
+
topStructuredScore = item.score ?? 0;
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
if (topStructuredScore < 65) {
|
|
2028
|
+
return items;
|
|
2029
|
+
}
|
|
2030
|
+
const minBodyOnlyScore = Math.max(25, Math.round(topStructuredScore * 0.25));
|
|
2031
|
+
return items.filter((item) => {
|
|
2032
|
+
const isBodyOnly = item.matchedFields !== undefined &&
|
|
2033
|
+
item.matchedFields.length === 1 &&
|
|
2034
|
+
item.matchedFields[0] === "body";
|
|
2035
|
+
return !isBodyOnly || (item.score ?? 0) >= minBodyOnlyScore;
|
|
2036
|
+
});
|
|
2037
|
+
}
|
|
2038
|
+
function applyLimit(items, limit) {
|
|
2039
|
+
if (limit === undefined) {
|
|
2040
|
+
return items;
|
|
2041
|
+
}
|
|
2042
|
+
if (limit <= 0) {
|
|
2043
|
+
return [];
|
|
2044
|
+
}
|
|
2045
|
+
return items.slice(0, limit);
|
|
2046
|
+
}
|
|
722
2047
|
/**
|
|
723
2048
|
* Level-1 progressive discovery: returns compact 1-entry-per-domain summaries and aliases.
|
|
724
2049
|
*/
|
|
725
2050
|
export function discoverDomains(wiki, options = {}) {
|
|
726
2051
|
const includeInfra = options.includeInfrastructure ?? true;
|
|
727
|
-
const
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
}
|
|
733
|
-
if (!hasQuery || !options.query) {
|
|
734
|
-
prepared.push({ domain });
|
|
735
|
-
continue;
|
|
736
|
-
}
|
|
737
|
-
const childHighTerms = [];
|
|
738
|
-
const childAnchorTerms = [];
|
|
739
|
-
const childRepoTerms = [];
|
|
740
|
-
const childBodySources = [];
|
|
741
|
-
const codeSpans = extractInlineCodeSpans(domain.content);
|
|
742
|
-
for (const sceneId of domain.sceneIds) {
|
|
743
|
-
const scene = wiki.scenes.get(sceneId);
|
|
744
|
-
if (!scene) {
|
|
2052
|
+
const activeQueries = collectActiveQueries(options.query, options.queries);
|
|
2053
|
+
if (activeQueries.length === 0) {
|
|
2054
|
+
const out = [];
|
|
2055
|
+
for (const domain of wiki.domains.values()) {
|
|
2056
|
+
if (!includeInfra && domain.isInfrastructureDomain) {
|
|
745
2057
|
continue;
|
|
746
2058
|
}
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
childRepoTerms.push(repoRef.repo);
|
|
750
|
-
}
|
|
751
|
-
for (const anchor of scene.frontmatter.anchors ?? []) {
|
|
752
|
-
childAnchorTerms.push(anchor.file);
|
|
753
|
-
if (anchor.symbol) {
|
|
754
|
-
childAnchorTerms.push(anchor.symbol);
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
codeSpans.push(...extractInlineCodeSpans(scene.content));
|
|
758
|
-
childBodySources.push(scene.content, ...scene.details.map((d) => d.content));
|
|
759
|
-
}
|
|
760
|
-
prepared.push({
|
|
761
|
-
domain,
|
|
762
|
-
doc: {
|
|
763
|
-
id: domain.domainId,
|
|
2059
|
+
out.push({
|
|
2060
|
+
domainId: domain.domainId,
|
|
764
2061
|
title: domain.frontmatter.title,
|
|
765
2062
|
aliases: domain.frontmatter.aliases,
|
|
766
2063
|
summary: domain.frontmatter.summary,
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
});
|
|
2064
|
+
maintainers: domain.frontmatter.maintainers,
|
|
2065
|
+
isInfrastructureDomain: domain.isInfrastructureDomain,
|
|
2066
|
+
sceneCount: domain.sceneIds.length,
|
|
2067
|
+
relativePath: domain.relativePath,
|
|
2068
|
+
});
|
|
2069
|
+
}
|
|
2070
|
+
out.sort((a, b) => a.domainId.localeCompare(b.domainId));
|
|
2071
|
+
return applyLimit(out, options.limit);
|
|
776
2072
|
}
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
score = evalRes.score;
|
|
789
|
-
matchedFields = evalRes.matchedFields;
|
|
790
|
-
matchSnippet = evalRes.matchSnippet;
|
|
2073
|
+
const index = getPrecomputedWikiIndex(wiki);
|
|
2074
|
+
const allDomains = getPrecomputedDomains(wiki, index);
|
|
2075
|
+
const candidateDomains = allDomains.filter((entry) => includeInfra || !entry.domain.isInfrastructureDomain);
|
|
2076
|
+
const domainDocById = new Map(candidateDomains.map((d) => [d.domain.domainId, d.doc]));
|
|
2077
|
+
function runSingleDomainQuery(q) {
|
|
2078
|
+
const plan = buildPreparedQueryPlan(q, index);
|
|
2079
|
+
const hits = [];
|
|
2080
|
+
for (const { domain, doc } of candidateDomains) {
|
|
2081
|
+
const evalRes = evaluatePreparedQuery(plan, doc, false);
|
|
2082
|
+
if (evalRes.score <= 0) {
|
|
2083
|
+
continue;
|
|
791
2084
|
}
|
|
792
|
-
|
|
2085
|
+
const item = {
|
|
793
2086
|
domainId: domain.domainId,
|
|
794
2087
|
title: domain.frontmatter.title,
|
|
795
2088
|
aliases: domain.frontmatter.aliases,
|
|
@@ -798,117 +2091,255 @@ export function discoverDomains(wiki, options = {}) {
|
|
|
798
2091
|
isInfrastructureDomain: domain.isInfrastructureDomain,
|
|
799
2092
|
sceneCount: domain.sceneIds.length,
|
|
800
2093
|
relativePath: domain.relativePath,
|
|
801
|
-
|
|
802
|
-
...(matchedFields
|
|
803
|
-
|
|
2094
|
+
score: evalRes.score,
|
|
2095
|
+
...(evalRes.matchedFields.length > 0 ? { matchedFields: evalRes.matchedFields } : {}),
|
|
2096
|
+
};
|
|
2097
|
+
hits.push({
|
|
2098
|
+
item,
|
|
2099
|
+
score: evalRes.score,
|
|
2100
|
+
matchedFields: item.matchedFields,
|
|
2101
|
+
snippetTerms: evalRes.bodySnippetTerms,
|
|
804
2102
|
});
|
|
805
2103
|
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
options.
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
2104
|
+
const filtered = filterWeakBodyOnlyHits(hits);
|
|
2105
|
+
filtered.sort((a, b) => b.score - a.score || a.item.domainId.localeCompare(b.item.domainId));
|
|
2106
|
+
return filtered;
|
|
2107
|
+
}
|
|
2108
|
+
if (activeQueries.length === 1) {
|
|
2109
|
+
const limited = applyLimit(runSingleDomainQuery(activeQueries[0]), options.limit);
|
|
2110
|
+
return limited.map(({ item, snippetTerms }) => {
|
|
2111
|
+
if (!snippetTerms || snippetTerms.length === 0) {
|
|
2112
|
+
return item;
|
|
2113
|
+
}
|
|
2114
|
+
const doc = domainDocById.get(item.domainId);
|
|
2115
|
+
const snippet = doc
|
|
2116
|
+
? extractMatchSnippet(doc.rawSnippetSources, snippetTerms, 80)
|
|
2117
|
+
: undefined;
|
|
2118
|
+
return snippet ? { ...item, matchSnippet: snippet } : item;
|
|
2119
|
+
});
|
|
2120
|
+
}
|
|
2121
|
+
// Multi-query Reciprocal Rank Fusion (RRF) across sub-queries
|
|
2122
|
+
const fusedById = new Map();
|
|
2123
|
+
for (const q of activeQueries) {
|
|
2124
|
+
const subResults = runSingleDomainQuery(q);
|
|
2125
|
+
const topScore = Math.max(1, subResults[0]?.item.score ?? 1);
|
|
2126
|
+
const rrfWindow = Math.min(subResults.length, 30);
|
|
2127
|
+
for (let idx = 0; idx < rrfWindow; idx++) {
|
|
2128
|
+
const { item, snippetTerms } = subResults[idx];
|
|
2129
|
+
const rank = idx + 1;
|
|
2130
|
+
const rawScore = item.score ?? 0;
|
|
2131
|
+
const rrfContribution = 1200 / (1 + rank) + 40 * (rawScore / topScore);
|
|
2132
|
+
const existing = fusedById.get(item.domainId);
|
|
2133
|
+
if (!existing) {
|
|
2134
|
+
fusedById.set(item.domainId, {
|
|
2135
|
+
base: item,
|
|
2136
|
+
rrfSum: rrfContribution,
|
|
2137
|
+
maxRawScore: rawScore,
|
|
2138
|
+
bestRank: rank,
|
|
2139
|
+
fields: new Set(item.matchedFields ?? []),
|
|
2140
|
+
bestSnippetTerms: snippetTerms,
|
|
831
2141
|
});
|
|
832
2142
|
}
|
|
2143
|
+
else {
|
|
2144
|
+
existing.rrfSum += rrfContribution;
|
|
2145
|
+
existing.maxRawScore = Math.max(existing.maxRawScore, rawScore);
|
|
2146
|
+
for (const f of item.matchedFields ?? []) {
|
|
2147
|
+
existing.fields.add(f);
|
|
2148
|
+
}
|
|
2149
|
+
if (rank < existing.bestRank && snippetTerms?.length) {
|
|
2150
|
+
existing.bestRank = rank;
|
|
2151
|
+
existing.bestSnippetTerms = snippetTerms;
|
|
2152
|
+
}
|
|
2153
|
+
else if (!existing.bestSnippetTerms && snippetTerms?.length) {
|
|
2154
|
+
existing.bestSnippetTerms = snippetTerms;
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
833
2157
|
}
|
|
834
|
-
results.sort((a, b) => (b.score ?? 0) - (a.score ?? 0) || a.domainId.localeCompare(b.domainId));
|
|
835
2158
|
}
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
2159
|
+
const sortedEntries = Array.from(fusedById.values())
|
|
2160
|
+
.map((entry) => ({
|
|
2161
|
+
entry,
|
|
2162
|
+
fusedScore: Math.round(entry.rrfSum + 0.15 * entry.maxRawScore),
|
|
2163
|
+
}))
|
|
2164
|
+
.sort((a, b) => b.fusedScore - a.fusedScore || a.entry.base.domainId.localeCompare(b.entry.base.domainId));
|
|
2165
|
+
return applyLimit(sortedEntries, options.limit).map(({ entry, fusedScore }) => {
|
|
2166
|
+
const orderedFields = FIELD_DISPLAY_ORDER.filter((f) => entry.fields.has(f));
|
|
2167
|
+
const doc = domainDocById.get(entry.base.domainId);
|
|
2168
|
+
const snippet = doc && entry.bestSnippetTerms?.length
|
|
2169
|
+
? extractMatchSnippet(doc.rawSnippetSources, entry.bestSnippetTerms, 80)
|
|
2170
|
+
: undefined;
|
|
2171
|
+
return {
|
|
2172
|
+
...entry.base,
|
|
2173
|
+
score: fusedScore,
|
|
2174
|
+
...(orderedFields.length > 0 ? { matchedFields: orderedFields } : {}),
|
|
2175
|
+
...(snippet ? { matchSnippet: snippet } : {}),
|
|
2176
|
+
};
|
|
2177
|
+
});
|
|
840
2178
|
}
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
2179
|
+
const COMPARISON_GLUE_WORDS = new Set([
|
|
2180
|
+
"compare",
|
|
2181
|
+
"comparing",
|
|
2182
|
+
"comparison",
|
|
2183
|
+
"between",
|
|
2184
|
+
"versus",
|
|
2185
|
+
"vs",
|
|
2186
|
+
"and",
|
|
2187
|
+
"or",
|
|
2188
|
+
"for",
|
|
2189
|
+
"with",
|
|
2190
|
+
"from",
|
|
2191
|
+
"into",
|
|
2192
|
+
"the",
|
|
2193
|
+
"of",
|
|
2194
|
+
"in",
|
|
2195
|
+
"on",
|
|
2196
|
+
"to",
|
|
2197
|
+
"by",
|
|
2198
|
+
]);
|
|
2199
|
+
function expandMultiEntitySingleQuery(rawQuery, index, candidateScenes) {
|
|
2200
|
+
if (index.sceneCount < IDF_SCALING_MIN_SCENES) {
|
|
2201
|
+
return [rawQuery];
|
|
2202
|
+
}
|
|
2203
|
+
const plan = buildPreparedQueryPlan(rawQuery, index);
|
|
2204
|
+
const { tokenized, groupIdf } = plan;
|
|
2205
|
+
if (tokenized.groups.length < 2) {
|
|
2206
|
+
return [rawQuery];
|
|
2207
|
+
}
|
|
2208
|
+
const entityClusters = [];
|
|
2209
|
+
const entityGroupIndices = new Set();
|
|
2210
|
+
for (let i = 0; i < tokenized.groups.length; i++) {
|
|
2211
|
+
if (entityGroupIndices.has(i)) {
|
|
2212
|
+
continue;
|
|
2213
|
+
}
|
|
2214
|
+
const g = tokenized.groups[i];
|
|
2215
|
+
const idf = groupIdf[i];
|
|
2216
|
+
const isModelOrDesignator = Boolean(g.designators && g.designators.length > 0) ||
|
|
2217
|
+
/^[a-z]{1,6}-[a-z0-9]{2,}(?:-[a-z0-9]+)*$/.test(g.primary);
|
|
2218
|
+
if (!isModelOrDesignator || idf.docRatio > 0.08) {
|
|
850
2219
|
continue;
|
|
851
2220
|
}
|
|
852
|
-
|
|
2221
|
+
const hasCorpusPresence = getSceneTermDf(index, g.primary) > 0 ||
|
|
2222
|
+
(g.designators?.some((d) => getSceneDesignatorDf(index, d) > 0) ?? false);
|
|
2223
|
+
if (!hasCorpusPresence) {
|
|
853
2224
|
continue;
|
|
854
2225
|
}
|
|
855
|
-
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
2226
|
+
// Coalesce an immediately following rare model-variant token (e.g. "b-52" + "rmp" -> "b-52 rmp")
|
|
2227
|
+
const nextGroup = tokenized.groups[i + 1];
|
|
2228
|
+
const nextIdf = groupIdf[i + 1];
|
|
2229
|
+
if (nextGroup &&
|
|
2230
|
+
nextIdf &&
|
|
2231
|
+
!nextGroup.designators?.length &&
|
|
2232
|
+
/^[a-z0-9]{2,6}$/.test(nextGroup.primary) &&
|
|
2233
|
+
nextIdf.docRatio <= 0.08 &&
|
|
2234
|
+
getSceneTermDf(index, `${g.primary} ${nextGroup.primary}`) > 0) {
|
|
2235
|
+
entityGroupIndices.add(i);
|
|
2236
|
+
entityGroupIndices.add(i + 1);
|
|
2237
|
+
entityClusters.push({
|
|
2238
|
+
text: `${g.primary} ${nextGroup.primary}`,
|
|
2239
|
+
groupIndices: [i, i + 1],
|
|
2240
|
+
designators: g.designators ?? [],
|
|
2241
|
+
primaryLower: `${g.primary} ${nextGroup.primary}`,
|
|
2242
|
+
});
|
|
2243
|
+
i += 1;
|
|
2244
|
+
continue;
|
|
2245
|
+
}
|
|
2246
|
+
entityGroupIndices.add(i);
|
|
2247
|
+
entityClusters.push({
|
|
2248
|
+
text: g.primary,
|
|
2249
|
+
groupIndices: [i],
|
|
2250
|
+
designators: g.designators ?? [],
|
|
2251
|
+
primaryLower: g.primary,
|
|
2252
|
+
});
|
|
2253
|
+
}
|
|
2254
|
+
if (entityClusters.length < 2) {
|
|
2255
|
+
return [rawQuery];
|
|
2256
|
+
}
|
|
2257
|
+
// Only decompose into per-entity RRF sub-queries when NO single scene contains all entities together
|
|
2258
|
+
const singleSceneCoversAll = candidateScenes.some(({ doc }) => entityClusters.every((ent) => doc.allTextLower.includes(ent.primaryLower) ||
|
|
2259
|
+
ent.designators.some((d) => doc.designators.all.has(d))));
|
|
2260
|
+
if (singleSceneCoversAll) {
|
|
2261
|
+
return [rawQuery];
|
|
2262
|
+
}
|
|
2263
|
+
const sharedContextTokens = [];
|
|
2264
|
+
for (let i = 0; i < tokenized.groups.length; i++) {
|
|
2265
|
+
if (entityGroupIndices.has(i)) {
|
|
859
2266
|
continue;
|
|
860
2267
|
}
|
|
861
|
-
const
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
2268
|
+
const tok = tokenized.groups[i].primary;
|
|
2269
|
+
if (!COMPARISON_GLUE_WORDS.has(tok)) {
|
|
2270
|
+
sharedContextTokens.push(tok);
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
const sharedContext = sharedContextTokens.join(" ");
|
|
2274
|
+
return [
|
|
2275
|
+
rawQuery,
|
|
2276
|
+
...entityClusters.map((ent) => (sharedContext ? `${ent.text} ${sharedContext}` : ent.text)),
|
|
2277
|
+
];
|
|
2278
|
+
}
|
|
2279
|
+
/**
|
|
2280
|
+
* Level-2 progressive discovery: lists scenes/infrastructure entries filtered by domain and/or query/queries.
|
|
2281
|
+
*/
|
|
2282
|
+
export function discoverScenes(wiki, options = {}) {
|
|
2283
|
+
const kindFilter = options.kind ?? "all";
|
|
2284
|
+
const rawActiveQueries = collectActiveQueries(options.query, options.queries);
|
|
2285
|
+
if (rawActiveQueries.length === 0) {
|
|
2286
|
+
const out = [];
|
|
2287
|
+
for (const scene of wiki.scenes.values()) {
|
|
2288
|
+
if (options.domainId && scene.domainId !== options.domainId) {
|
|
2289
|
+
continue;
|
|
2290
|
+
}
|
|
2291
|
+
if (kindFilter !== "all" && scene.kind !== kindFilter) {
|
|
2292
|
+
continue;
|
|
2293
|
+
}
|
|
2294
|
+
const repoNames = scene.frontmatter.repositories.map((r) => r.repo);
|
|
2295
|
+
const sourceNames = scene.frontmatter.sources?.map((s) => s.source);
|
|
2296
|
+
const chainNames = scene.chains.map((c) => c.title);
|
|
2297
|
+
out.push({
|
|
880
2298
|
id: scene.id,
|
|
881
|
-
|
|
2299
|
+
domainId: scene.domainId,
|
|
2300
|
+
sceneId: scene.sceneId,
|
|
2301
|
+
kind: scene.kind,
|
|
882
2302
|
title: scene.frontmatter.title,
|
|
883
2303
|
aliases: scene.frontmatter.aliases,
|
|
884
|
-
chainTerms,
|
|
885
|
-
anchorTerms,
|
|
886
|
-
repoTerms: repoNames,
|
|
887
2304
|
summary: scene.frontmatter.summary,
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
2305
|
+
maintainers: scene.frontmatter.maintainers,
|
|
2306
|
+
...(scene.frontmatter.confidence !== undefined
|
|
2307
|
+
? { confidence: scene.frontmatter.confidence }
|
|
2308
|
+
: {}),
|
|
2309
|
+
dependsOn: scene.frontmatter.dependsOn,
|
|
2310
|
+
repositories: repoNames,
|
|
2311
|
+
...(sourceNames && sourceNames.length > 0 ? { sources: sourceNames } : {}),
|
|
2312
|
+
chainNames,
|
|
2313
|
+
hasPendingConfirmations: scene.hasPendingConfirmations,
|
|
2314
|
+
detailCount: scene.details.length,
|
|
2315
|
+
relativePath: scene.relativePath,
|
|
2316
|
+
});
|
|
2317
|
+
}
|
|
2318
|
+
out.sort((a, b) => a.id.localeCompare(b.id));
|
|
2319
|
+
return applyLimit(out, options.limit);
|
|
895
2320
|
}
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
2321
|
+
const index = getPrecomputedWikiIndex(wiki);
|
|
2322
|
+
const candidateScenes = index.scenes.filter((entry) => {
|
|
2323
|
+
if (options.domainId && entry.scene.domainId !== options.domainId) {
|
|
2324
|
+
return false;
|
|
2325
|
+
}
|
|
2326
|
+
if (kindFilter !== "all" && entry.scene.kind !== kindFilter) {
|
|
2327
|
+
return false;
|
|
2328
|
+
}
|
|
2329
|
+
return true;
|
|
2330
|
+
});
|
|
2331
|
+
const activeQueries = rawActiveQueries.length === 1
|
|
2332
|
+
? expandMultiEntitySingleQuery(rawActiveQueries[0], index, candidateScenes)
|
|
2333
|
+
: rawActiveQueries;
|
|
2334
|
+
function runSingleSceneQuery(q) {
|
|
2335
|
+
const plan = buildPreparedQueryPlan(q, index);
|
|
2336
|
+
const hits = [];
|
|
2337
|
+
for (const { scene, repoNames, sourceNames, chainNames, doc } of candidateScenes) {
|
|
2338
|
+
const evalRes = evaluatePreparedQuery(plan, doc, true);
|
|
2339
|
+
if (evalRes.score <= 0) {
|
|
2340
|
+
continue;
|
|
910
2341
|
}
|
|
911
|
-
|
|
2342
|
+
const item = {
|
|
912
2343
|
id: scene.id,
|
|
913
2344
|
domainId: scene.domainId,
|
|
914
2345
|
sceneId: scene.sceneId,
|
|
@@ -922,48 +2353,124 @@ export function discoverScenes(wiki, options = {}) {
|
|
|
922
2353
|
: {}),
|
|
923
2354
|
dependsOn: scene.frontmatter.dependsOn,
|
|
924
2355
|
repositories: repoNames,
|
|
2356
|
+
...(sourceNames.length > 0 ? { sources: sourceNames } : {}),
|
|
925
2357
|
chainNames,
|
|
926
2358
|
hasPendingConfirmations: scene.hasPendingConfirmations,
|
|
927
2359
|
detailCount: scene.details.length,
|
|
928
2360
|
relativePath: scene.relativePath,
|
|
929
|
-
|
|
930
|
-
...(matchedFields
|
|
931
|
-
|
|
2361
|
+
score: evalRes.score,
|
|
2362
|
+
...(evalRes.matchedFields.length > 0 ? { matchedFields: evalRes.matchedFields } : {}),
|
|
2363
|
+
};
|
|
2364
|
+
hits.push({
|
|
2365
|
+
item,
|
|
2366
|
+
score: evalRes.score,
|
|
2367
|
+
matchedFields: item.matchedFields,
|
|
2368
|
+
snippetTerms: evalRes.bodySnippetTerms,
|
|
2369
|
+
pinpointTerms: evalRes.pinpointTerms,
|
|
2370
|
+
pinpointDesignators: evalRes.pinpointDesignators,
|
|
932
2371
|
});
|
|
933
2372
|
}
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
2373
|
+
const filtered = filterWeakBodyOnlyHits(hits);
|
|
2374
|
+
filtered.sort((a, b) => b.score - a.score || a.item.id.localeCompare(b.item.id));
|
|
2375
|
+
return filtered;
|
|
2376
|
+
}
|
|
2377
|
+
function enrichSceneItem(base, snippetTerms, pTerms, pDesigs) {
|
|
2378
|
+
const doc = index.sceneById.get(base.id)?.doc;
|
|
2379
|
+
if (!doc) {
|
|
2380
|
+
return base;
|
|
2381
|
+
}
|
|
2382
|
+
const snippet = snippetTerms && snippetTerms.length > 0
|
|
2383
|
+
? extractMatchSnippet(doc.rawSnippetSources, [...snippetTerms], 80)
|
|
2384
|
+
: undefined;
|
|
2385
|
+
const pinpoint = pinpointSubSceneMatches(doc, pTerms, pDesigs);
|
|
2386
|
+
return {
|
|
2387
|
+
...base,
|
|
2388
|
+
...(snippet ? { matchSnippet: snippet } : {}),
|
|
2389
|
+
...(pinpoint.matchedChains.length > 0 ? { matchedChains: pinpoint.matchedChains } : {}),
|
|
2390
|
+
...(pinpoint.matchedDetails.length > 0 ? { matchedDetails: pinpoint.matchedDetails } : {}),
|
|
2391
|
+
...(pinpoint.matchedAnchors.length > 0 ? { matchedAnchors: pinpoint.matchedAnchors } : {}),
|
|
2392
|
+
};
|
|
2393
|
+
}
|
|
2394
|
+
if (activeQueries.length === 1) {
|
|
2395
|
+
const emptyDesigs = new Set();
|
|
2396
|
+
const limited = applyLimit(runSingleSceneQuery(activeQueries[0]), options.limit);
|
|
2397
|
+
return limited.map(({ item, snippetTerms, pinpointTerms, pinpointDesignators }) => enrichSceneItem(item, snippetTerms, pinpointTerms ?? [], pinpointDesignators ?? emptyDesigs));
|
|
2398
|
+
}
|
|
2399
|
+
// Multi-query Reciprocal Rank Fusion (RRF) so every queried entity/report gets top-K representation without cannibalization
|
|
2400
|
+
const fusedById = new Map();
|
|
2401
|
+
const rrfPerQueryCap = options.limit !== undefined ? Math.max(40, options.limit * 4) : 60;
|
|
2402
|
+
for (const q of activeQueries) {
|
|
2403
|
+
const subResults = runSingleSceneQuery(q);
|
|
2404
|
+
const topScore = Math.max(1, subResults[0]?.item.score ?? 1);
|
|
2405
|
+
const windowLen = Math.min(subResults.length, rrfPerQueryCap);
|
|
2406
|
+
for (let idx = 0; idx < windowLen; idx++) {
|
|
2407
|
+
const { item, snippetTerms, pinpointTerms, pinpointDesignators } = subResults[idx];
|
|
2408
|
+
const rank = idx + 1;
|
|
2409
|
+
const rawScore = item.score ?? 0;
|
|
2410
|
+
const rrfContribution = 1200 / (1 + rank) + 40 * (rawScore / topScore);
|
|
2411
|
+
const existing = fusedById.get(item.id);
|
|
2412
|
+
if (!existing) {
|
|
2413
|
+
fusedById.set(item.id, {
|
|
2414
|
+
base: item,
|
|
2415
|
+
rrfSum: rrfContribution,
|
|
2416
|
+
maxRawScore: rawScore,
|
|
2417
|
+
bestRank: rank,
|
|
2418
|
+
fields: new Set(item.matchedFields ?? []),
|
|
2419
|
+
pinpointTerms: new Set(pinpointTerms ?? []),
|
|
2420
|
+
pinpointDesignators: new Set(pinpointDesignators ?? []),
|
|
2421
|
+
bestSnippetTerms: snippetTerms,
|
|
959
2422
|
});
|
|
960
2423
|
}
|
|
2424
|
+
else {
|
|
2425
|
+
existing.rrfSum += rrfContribution;
|
|
2426
|
+
existing.maxRawScore = Math.max(existing.maxRawScore, rawScore);
|
|
2427
|
+
for (const f of item.matchedFields ?? []) {
|
|
2428
|
+
existing.fields.add(f);
|
|
2429
|
+
}
|
|
2430
|
+
for (const pt of pinpointTerms ?? []) {
|
|
2431
|
+
existing.pinpointTerms.add(pt);
|
|
2432
|
+
}
|
|
2433
|
+
for (const pd of pinpointDesignators ?? []) {
|
|
2434
|
+
existing.pinpointDesignators.add(pd);
|
|
2435
|
+
}
|
|
2436
|
+
if (rank < existing.bestRank && snippetTerms?.length) {
|
|
2437
|
+
existing.bestRank = rank;
|
|
2438
|
+
existing.bestSnippetTerms = snippetTerms;
|
|
2439
|
+
}
|
|
2440
|
+
else if (!existing.bestSnippetTerms && snippetTerms?.length) {
|
|
2441
|
+
existing.bestSnippetTerms = snippetTerms;
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
961
2444
|
}
|
|
962
|
-
results.sort((a, b) => (b.score ?? 0) - (a.score ?? 0) || a.id.localeCompare(b.id));
|
|
963
2445
|
}
|
|
964
|
-
|
|
965
|
-
|
|
2446
|
+
const sortedFused = Array.from(fusedById.values())
|
|
2447
|
+
.map((entry) => ({
|
|
2448
|
+
entry,
|
|
2449
|
+
fusedScore: Math.round(entry.rrfSum + 0.15 * entry.maxRawScore),
|
|
2450
|
+
}))
|
|
2451
|
+
.sort((a, b) => b.fusedScore - a.fusedScore || a.entry.base.id.localeCompare(b.entry.base.id));
|
|
2452
|
+
return applyLimit(sortedFused, options.limit).map(({ entry, fusedScore }) => {
|
|
2453
|
+
const orderedFields = FIELD_DISPLAY_ORDER.filter((f) => entry.fields.has(f));
|
|
2454
|
+
const baseWithScore = {
|
|
2455
|
+
...entry.base,
|
|
2456
|
+
score: fusedScore,
|
|
2457
|
+
...(orderedFields.length > 0 ? { matchedFields: orderedFields } : {}),
|
|
2458
|
+
};
|
|
2459
|
+
return enrichSceneItem(baseWithScore, entry.bestSnippetTerms, Array.from(entry.pinpointTerms), entry.pinpointDesignators);
|
|
2460
|
+
});
|
|
2461
|
+
}
|
|
2462
|
+
/**
|
|
2463
|
+
* Returns precomputed lowercase corpus text & canonical designator set for a scene in O(N_init) / O(1_cached).
|
|
2464
|
+
*/
|
|
2465
|
+
export function inspectSceneCoverageData(wiki, sceneId) {
|
|
2466
|
+
const index = getPrecomputedWikiIndex(wiki);
|
|
2467
|
+
const found = index.sceneById.get(sceneId);
|
|
2468
|
+
if (!found) {
|
|
2469
|
+
return undefined;
|
|
966
2470
|
}
|
|
967
|
-
return
|
|
2471
|
+
return {
|
|
2472
|
+
allTextLower: found.doc.allTextLower,
|
|
2473
|
+
designators: found.doc.designators.all,
|
|
2474
|
+
};
|
|
968
2475
|
}
|
|
969
2476
|
//# sourceMappingURL=discover.js.map
|