@polycode-projects/the-mechanical-code-talker 5.0.28 → 5.0.30
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/corpus/conceptnet/README.md +85 -41
- package/corpus/conceptnet/commonsenseqa-seed.mjs +322 -0
- package/corpus/conceptnet/filter-dump.mjs +6 -1
- package/corpus/conceptnet/manifest.json +51 -0
- package/corpus/conceptnet/quality-filter.mjs +11 -0
- package/corpus/conceptnet/slice.jsonl +28365 -42942
- package/package.json +2 -2
- package/src/domain/agent-belief.mjs +33 -11
- package/src/domain/choice-question.mjs +125 -1
- package/src/domain/el-classify.mjs +16 -0
- package/src/domain/grammar/ace.mjs +66 -3
- package/src/domain/news-feed.mjs +30 -6
- package/src/domain/tableau.mjs +101 -14
- package/src/services/chat-page-viz.mjs +6 -3
- package/src/services/extensions.mjs +9 -2
- package/src/services/mudiii-turn.mjs +114 -3
- package/src/services/mudiii-viz.mjs +85 -1
- package/src/services/news-viz.mjs +2 -0
- package/src/services/news.mjs +3 -2
- package/src/services/predator-prey.mjs +77 -33
- package/src/surfaces/web/memory-ask-browser.bundle.js +132 -132
- package/src/surfaces/web/mudiii-browser-entry.mjs +13 -2
- package/src/surfaces/web/news-browser-entry.mjs +20 -1
- package/src/surfaces/web/planner-instances.mjs +64 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polycode-projects/the-mechanical-code-talker",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.30",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The Mechanical Code Talker (tmct) — a tolerant, offline, $0 chat surface that guides you toward precision queries about a software repository. ELIZA/PARRY-style but domain-obsessed with code. No model calls; indexes a repo on request (tmct index) or reads any producer's graph.",
|
|
@@ -162,9 +162,9 @@
|
|
|
162
162
|
"bench:receipts": "node scripts/bench-receipts.mjs",
|
|
163
163
|
"claims": "node scripts/claims/run-all.mjs",
|
|
164
164
|
"claim:prose-band": "node scripts/claims/claim-prose-band.mjs",
|
|
165
|
-
"claim:openbookqa": "node scripts/claims/claim-openbookqa.mjs",
|
|
166
165
|
"claim:commonsenseqa": "node scripts/claims/claim-commonsenseqa.mjs",
|
|
167
166
|
"claim:planner": "node scripts/claims/claim-planner.mjs",
|
|
167
|
+
"claim:syllogist": "node scripts/claims/claim-syllogist.mjs",
|
|
168
168
|
"roll": "node scripts/roll.mjs",
|
|
169
169
|
"build:ask-bundle": "node scripts/build-ask-bundle.mjs",
|
|
170
170
|
"build:chat-bundle": "node scripts/build-chat-bundle.mjs",
|
|
@@ -29,33 +29,55 @@ const cellIdOf = (x, y) => `cell-${x}-${y}`;
|
|
|
29
29
|
|
|
30
30
|
const chebyshevSteps = (ax, ay, bx, by) => Math.max(Math.abs(ax - bx), Math.abs(ay - by));
|
|
31
31
|
|
|
32
|
-
/**
|
|
33
|
-
*
|
|
32
|
+
/** The one three-rung walk `believedCellOf` and `beliefOriginOf` both read,
|
|
33
|
+
* so the cell a panel shows and the "how it knows" label beside it can never
|
|
34
|
+
* disagree about which rung answered. Returns `{ origin, cell }` or null.
|
|
34
35
|
*
|
|
35
|
-
*
|
|
36
|
+
* Three rungs, in order:
|
|
36
37
|
*
|
|
37
38
|
* 1. A target the board has removed (eaten, starved, hatched) is believed
|
|
38
39
|
* nowhere, and no told fact can put it back. A claim about an id that was
|
|
39
40
|
* never placed at all is a different case and still lands, which is what
|
|
40
41
|
* keeps deception working.
|
|
41
|
-
* 2. Ground truth, when the target's real cell sits
|
|
42
|
-
* Chebyshev steps of `observerCell`.
|
|
43
|
-
* 3. The newest told fact addressed to this observer about this target
|
|
44
|
-
* `opts.toldFacts` rows shaped
|
|
45
|
-
*
|
|
42
|
+
* 2. Ground truth (`origin: "seen"`), when the target's real cell sits
|
|
43
|
+
* within `visionRadius` Chebyshev steps of `observerCell`.
|
|
44
|
+
* 3. The newest told fact addressed to this observer about this target
|
|
45
|
+
* (`origin: { told: turn }`) — `opts.toldFacts` rows shaped
|
|
46
|
+
* `{ subject, toAgent, cell, turn }`. Otherwise null.
|
|
46
47
|
*
|
|
47
48
|
* `state` is a folded world state: `.placements` (subject -> `{ cell }`) and
|
|
48
49
|
* `.removed` (a Set of subjects off the board). Pure. */
|
|
49
|
-
|
|
50
|
+
function believedRungOf(targetSubject, observerSubject, observerCell, state, opts = {}) {
|
|
50
51
|
if (state.removed.has(targetSubject)) return null;
|
|
51
52
|
const { visionRadius = DEFAULT_VISION_RADIUS, toldFacts = [] } = opts;
|
|
52
53
|
const place = state.placements.get(targetSubject);
|
|
53
54
|
const at = place ? cellCoordsOf(place.cell) : null;
|
|
54
|
-
if (at && chebyshevSteps(observerCell.x, observerCell.y, at.x, at.y) <= visionRadius)
|
|
55
|
+
if (at && chebyshevSteps(observerCell.x, observerCell.y, at.x, at.y) <= visionRadius) {
|
|
56
|
+
return { origin: "seen", cell: at };
|
|
57
|
+
}
|
|
55
58
|
const told = toldFacts
|
|
56
59
|
.filter((f) => f.toAgent === observerSubject && f.subject === targetSubject)
|
|
57
60
|
.sort((a, b) => (b.turn ?? 0) - (a.turn ?? 0))[0];
|
|
58
|
-
|
|
61
|
+
if (!told) return null;
|
|
62
|
+
const cell = cellCoordsOf(told.cell);
|
|
63
|
+
return cell ? { origin: { told: told.turn ?? 0 }, cell } : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Where `observerSubject` currently believes `targetSubject` to be, as an
|
|
67
|
+
* `{ x, y }` cell, or null for no belief at all. See `believedRungOf` for
|
|
68
|
+
* the three-rung walk this reads the cell half of. Pure. */
|
|
69
|
+
export function believedCellOf(targetSubject, observerSubject, observerCell, state, opts = {}) {
|
|
70
|
+
return believedRungOf(targetSubject, observerSubject, observerCell, state, opts)?.cell ?? null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Which rung answered `believedCellOf` for this exact call: `"seen"` when
|
|
74
|
+
* the target sits within vision, `{ told: turn }` for the newest told fact
|
|
75
|
+
* that answered, or null when the observer believes nothing. A belief
|
|
76
|
+
* panel's own "how it knows" column reads this beside the cell
|
|
77
|
+
* `believedCellOf` returns, off the same walk (`believedRungOf`), so the two
|
|
78
|
+
* can never drift apart. Pure. */
|
|
79
|
+
export function beliefOriginOf(targetSubject, observerSubject, observerCell, state, opts = {}) {
|
|
80
|
+
return believedRungOf(targetSubject, observerSubject, observerCell, state, opts)?.origin ?? null;
|
|
59
81
|
}
|
|
60
82
|
|
|
61
83
|
/** The nearest candidate (by believed Chebyshev distance) an observer has
|
|
@@ -223,13 +223,24 @@ function readStemNounPhrase(words) {
|
|
|
223
223
|
return out.join(" ");
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
+
/** Strips every leading determiner in a run ("the most fast food
|
|
227
|
+
* restaurants" -> "fast food restaurants"), not just the first — a
|
|
228
|
+
* quantifier stacked on an article is ordinary English, not a special
|
|
229
|
+
* case. */
|
|
230
|
+
function stripLeadingDeterminers(phrase) {
|
|
231
|
+
let out = phrase;
|
|
232
|
+
let m;
|
|
233
|
+
while ((m = LEADING_DETERMINER_RE.exec(out))) out = out.slice(m[0].length);
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
|
|
226
237
|
/** Strips a leading determiner, then reads a noun phrase — "" when the
|
|
227
238
|
* phrase would open on a pronoun or a boundary word, since neither ever
|
|
228
239
|
* names a source term. Stops at the first comma, so an option-like list
|
|
229
240
|
* ("a magazine, paper or gum") reads only its first member. */
|
|
230
241
|
function captureStemNounPhrase(tailText) {
|
|
231
242
|
const [beforeComma] = String(tailText).split(",", 1);
|
|
232
|
-
const stripped = beforeComma
|
|
243
|
+
const stripped = stripLeadingDeterminers(beforeComma);
|
|
233
244
|
const words = stripped.trim().toLowerCase().split(/[^a-z'-]+/).filter(Boolean);
|
|
234
245
|
if (words.length === 0 || PRONOUN_TERMS.has(words[0]) || PHRASE_BOUNDARY_WORDS.has(words[0])) return "";
|
|
235
246
|
return readStemNounPhrase(words);
|
|
@@ -291,6 +302,115 @@ const wantToSourceTerm = (stem) => {
|
|
|
291
302
|
return captureStemNounPhrase(m[2]) || m[1].toLowerCase();
|
|
292
303
|
};
|
|
293
304
|
|
|
305
|
+
// ---- postponed and mid-sentence wh-shapes ----
|
|
306
|
+
//
|
|
307
|
+
// leadsInterrogative (normalize.mjs) widened its gate to recognize four
|
|
308
|
+
// stem shapes where the wh-word is not the stem's own first word. Passing
|
|
309
|
+
// that gate only says the stem is a real question — it says nothing about
|
|
310
|
+
// where the source term sits inside it, so each shape needs its own
|
|
311
|
+
// extraction template here too. Every template below reads a closed, fixed
|
|
312
|
+
// vocabulary (the same PHRASE_BOUNDARY_WORDS/PREP_WORDS/PRONOUN_TERMS the
|
|
313
|
+
// fronted templates already use) and returns "" rather than guessing when
|
|
314
|
+
// nothing structured is there to read.
|
|
315
|
+
|
|
316
|
+
// A subordinator or preposition opening the clause ahead of a mid-sentence
|
|
317
|
+
// wh-word ("During a shark filled tornado where...") is framing, not the
|
|
318
|
+
// source term itself.
|
|
319
|
+
const SUBORDINATOR_LEAD_RE = /^(?:during|while|when|if|after|before|despite|though|although|because|since|as|without|about|for|in|on|at|with)\s+/i;
|
|
320
|
+
|
|
321
|
+
const BACKWARD_SCAN_STOP_WORDS = new Set([...PHRASE_BOUNDARY_WORDS, "be", "been"]);
|
|
322
|
+
|
|
323
|
+
/** Reads the noun phrase after the first preposition found in `text` — the
|
|
324
|
+
* locative-object reading a declarative lead clause usually wants
|
|
325
|
+
* ("plopped on the bench" -> "bench") over its own grammatical subject.
|
|
326
|
+
* Returns "" when the clause has no preposition to anchor on. */
|
|
327
|
+
function captureLocativeNounPhrase(text) {
|
|
328
|
+
const words = String(text).trim().toLowerCase().split(/[^a-z'-]+/).filter(Boolean);
|
|
329
|
+
const idx = words.findIndex((w) => PREP_WORDS.has(w));
|
|
330
|
+
if (idx === -1 || idx === words.length - 1) return "";
|
|
331
|
+
return captureStemNounPhrase(words.slice(idx + 1).join(" "));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Reads a short noun phrase backward from the end of `leadText`, skipping
|
|
335
|
+
* any boundary words trailing off the very end, then collecting content
|
|
336
|
+
* words until the next boundary word, pronoun, or the start of the text.
|
|
337
|
+
* Used only when the wh-word is the stem's own last word, where nothing
|
|
338
|
+
* marks the start of the noun phrase the way a template's cue word
|
|
339
|
+
* normally does. */
|
|
340
|
+
function captureTrailingNounPhrase(leadText) {
|
|
341
|
+
const words = String(leadText).trim().toLowerCase().split(/[^a-z'-]+/).filter(Boolean);
|
|
342
|
+
let end = words.length;
|
|
343
|
+
while (end > 0 && BACKWARD_SCAN_STOP_WORDS.has(words[end - 1])) end -= 1;
|
|
344
|
+
const out = [];
|
|
345
|
+
let i = end - 1;
|
|
346
|
+
while (i >= 0 && out.length < 3) {
|
|
347
|
+
const word = words[i];
|
|
348
|
+
if (BACKWARD_SCAN_STOP_WORDS.has(word) || PRONOUN_TERMS.has(word)) break;
|
|
349
|
+
out.unshift(word);
|
|
350
|
+
i -= 1;
|
|
351
|
+
}
|
|
352
|
+
if (out.length === 0) return "";
|
|
353
|
+
const stripped = stripLeadingDeterminers(out.join(" "));
|
|
354
|
+
const strippedWords = stripped.split(/\s+/).filter(Boolean);
|
|
355
|
+
if (strippedWords.length === 0 || PRONOUN_TERMS.has(strippedWords[0])) return "";
|
|
356
|
+
return stripped;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// "In what country are the most fast food restaurants?", "From where do
|
|
360
|
+
// aliens arrive?" — a wh-word fronted behind a single preposition, either
|
|
361
|
+
// carrying its own noun ("what country") or bare ("where"). The clause
|
|
362
|
+
// after the auxiliary/copula is where the source term sits.
|
|
363
|
+
const PREP_WH_FRONTED_CAPTURE_RE = new RegExp(
|
|
364
|
+
"^(?:in|at|on|from|by|with|for|of|to|off)\\s+"
|
|
365
|
+
+ "(?:(?:what|which)\\s+[a-z][\\w'-]*|where|when|why|how|who)\\s+"
|
|
366
|
+
+ "(?:is|are|was|were|do|does|did|can|could|should|would|will|has|have|might|must)\\s+(.+?)[?.!]*$", "i",
|
|
367
|
+
);
|
|
368
|
+
const prepWhFrontedSourceTerm = (stem) => {
|
|
369
|
+
const m = PREP_WH_FRONTED_CAPTURE_RE.exec(stem);
|
|
370
|
+
return m ? captureStemNounPhrase(m[1]) : "";
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// "During a shark filled tornado where should you not be?" — the wh-word
|
|
374
|
+
// sits mid-sentence, immediately ahead of an auxiliary, with no comma to
|
|
375
|
+
// split on. The source term sits in the clause ahead of the wh-word; a
|
|
376
|
+
// stem where the wh-word is already the first word captures an empty lead
|
|
377
|
+
// here and falls through, since that shape is the fronted templates' own.
|
|
378
|
+
// A lead that itself carries a comma is the trailing-clause shape's own
|
|
379
|
+
// territory instead, so this declines rather than reading across the join.
|
|
380
|
+
const MID_SENTENCE_WH_AUX_CAPTURE_RE = new RegExp(
|
|
381
|
+
"^(.*?)\\b(?:what|who|which|where|when|why|how)\\s+(?:i|you|he|she|it|we|they)?\\s*"
|
|
382
|
+
+ "(?:is|are|was|were|do|does|did|can|could|should|would|will|has|have|might|must)\\b", "i",
|
|
383
|
+
);
|
|
384
|
+
const midSentenceWhAuxSourceTerm = (stem) => {
|
|
385
|
+
const m = MID_SENTENCE_WH_AUX_CAPTURE_RE.exec(stem);
|
|
386
|
+
if (!m || !m[1].trim() || m[1].includes(",")) return "";
|
|
387
|
+
return captureStemNounPhrase(m[1].replace(SUBORDINATOR_LEAD_RE, ""));
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// "The trucker plopped on the bench with a sense of relief, where did he
|
|
391
|
+
// arrive?" — the stem's last comma- or period-delimited clause opens with
|
|
392
|
+
// a question-lead word; the source term sits in the declarative clause(s)
|
|
393
|
+
// ahead of it. A stem with no such split (one clause only) falls through,
|
|
394
|
+
// since there is no "earlier" clause to read.
|
|
395
|
+
const trailingClauseAfterPunctuationSourceTerm = (stem) => {
|
|
396
|
+
const clauses = stem.split(/[.,]+/).map((c) => c.trim()).filter(Boolean);
|
|
397
|
+
if (clauses.length < 2) return "";
|
|
398
|
+
const last = clauses[clauses.length - 1];
|
|
399
|
+
if (!QUESTION_LEAD_RE.test(last)) return "";
|
|
400
|
+
const lead = clauses.slice(0, -1).join(" ");
|
|
401
|
+
return captureLocativeNounPhrase(lead) || captureStemNounPhrase(lead);
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
// "... you will be doing what?", "... arrived at the what?" — a bare
|
|
405
|
+
// wh-word, with an optional leading article, is the stem's own last word
|
|
406
|
+
// before the closing "?". Nothing marks where the source term starts, so
|
|
407
|
+
// this reads backward from the wh-word for the nearest content phrase.
|
|
408
|
+
const TRAILING_BARE_WH_CAPTURE_RE = /^(.*?)\b(?:an?|the)?\s*(?:what|who|which|where|when|why|how)\s*\?\s*$/i;
|
|
409
|
+
const trailingBareWhSourceTerm = (stem) => {
|
|
410
|
+
const m = TRAILING_BARE_WH_CAPTURE_RE.exec(stem);
|
|
411
|
+
return m ? captureTrailingNounPhrase(m[1]) : "";
|
|
412
|
+
};
|
|
413
|
+
|
|
294
414
|
// Note: a leading "A/An/The <subject> ..." shape is not among these
|
|
295
415
|
// templates. coreParse gates every input on leadsInterrogative before this
|
|
296
416
|
// module ever runs, and that gate's QUESTION_LEAD_RE never accepts a line
|
|
@@ -304,6 +424,10 @@ function extractStemSourceTerm(stem) {
|
|
|
304
424
|
|| whereCopulaSourceTerm(stem)
|
|
305
425
|
|| trailingOfClauseSourceTerm(stem)
|
|
306
426
|
|| wantToSourceTerm(stem)
|
|
427
|
+
|| prepWhFrontedSourceTerm(stem)
|
|
428
|
+
|| midSentenceWhAuxSourceTerm(stem)
|
|
429
|
+
|| trailingClauseAfterPunctuationSourceTerm(stem)
|
|
430
|
+
|| trailingBareWhSourceTerm(stem)
|
|
307
431
|
);
|
|
308
432
|
}
|
|
309
433
|
|
|
@@ -59,6 +59,7 @@ const isType = (p) => lower(p) === "rdf:type";
|
|
|
59
59
|
const isDisjoint = (p) => lower(p) === "owl:disjointwith";
|
|
60
60
|
const isOnProperty = (p) => lower(p) === "owl:onproperty";
|
|
61
61
|
const isSomeValuesFrom = (p) => lower(p) === "owl:somevaluesfrom";
|
|
62
|
+
const isAllValuesFrom = (p) => lower(p) === "owl:allvaluesfrom";
|
|
62
63
|
const isOnClass = (p) => lower(p) === "owl:onclass";
|
|
63
64
|
const isIntersectionOf = (p) => lower(p) === "owl:intersectionof";
|
|
64
65
|
const isSubPropertyOf = (p) => lower(p) === "rdfs:subpropertyof";
|
|
@@ -89,6 +90,7 @@ export function normalizeElTBox(rows, { budget = 500 } = {}) {
|
|
|
89
90
|
// value) so an axiom's `from` can cite the real stored fact id it rode. ----
|
|
90
91
|
const onPropertyRow = new Map(); // restriction node -> its owl:onProperty row
|
|
91
92
|
const someValuesFromRow = new Map(); // restriction node -> its owl:someValuesFrom row
|
|
93
|
+
const allValuesFromRow = new Map(); // restriction node -> its owl:allValuesFrom row
|
|
92
94
|
const onClassRow = new Map(); // restriction node -> its owl:onClass row
|
|
93
95
|
const cardinalityRow = new Map(); // restriction node -> its owl:{min,max,}cardinality row
|
|
94
96
|
const intersectionRows = new Map(); // intersection node -> ordered [owl:intersectionOf row, …]
|
|
@@ -102,6 +104,10 @@ export function normalizeElTBox(rows, { budget = 500 } = {}) {
|
|
|
102
104
|
const p = r.predicate;
|
|
103
105
|
if (isOnProperty(p)) { onPropertyRow.set(r.subject, r); addConcept(r.subject); }
|
|
104
106
|
else if (isSomeValuesFrom(p)) { someValuesFromRow.set(r.subject, r); addConcept(r.subject); addConcept(r.object); }
|
|
107
|
+
// owl:allValuesFrom is left OUT of addConcept on purpose — EL has no
|
|
108
|
+
// universal restriction, and the restriction node it labels is stripped
|
|
109
|
+
// back out of `concepts` below rather than read as an atomic class.
|
|
110
|
+
else if (isAllValuesFrom(p)) { allValuesFromRow.set(r.subject, r); }
|
|
105
111
|
else if (isOnClass(p)) { onClassRow.set(r.subject, r); addConcept(r.subject); addConcept(r.object); }
|
|
106
112
|
else if (isCardinalityPredicate(p)) { cardinalityRow.set(r.subject, r); addConcept(r.subject); }
|
|
107
113
|
else if (isIntersectionOf(p)) {
|
|
@@ -122,6 +128,15 @@ export function normalizeElTBox(rows, { budget = 500 } = {}) {
|
|
|
122
128
|
const someValuesFromRestrictions = new Set(
|
|
123
129
|
[...onPropertyRow.keys()].filter((r) => someValuesFromRow.has(r)),
|
|
124
130
|
);
|
|
131
|
+
// EL has no universal restriction: an owl:allValuesFrom restriction node is
|
|
132
|
+
// recognised only so the loop below can skip it rather than fold it in as
|
|
133
|
+
// an atomic concept — and it never earned a concepts entry through this
|
|
134
|
+
// set in the first place (onProperty's own addConcept still ran, so the
|
|
135
|
+
// restriction id is scrubbed back out here).
|
|
136
|
+
const allValuesFromRestrictions = new Set(
|
|
137
|
+
[...onPropertyRow.keys()].filter((r) => allValuesFromRow.has(r)),
|
|
138
|
+
);
|
|
139
|
+
for (const r of allValuesFromRestrictions) concepts.delete(r);
|
|
125
140
|
const restrictionOf = new Map(); // restriction node -> { role, filler }
|
|
126
141
|
for (const r of someValuesFromRestrictions) {
|
|
127
142
|
restrictionOf.set(r, { role: onPropertyRow.get(r).object, filler: someValuesFromRow.get(r).object });
|
|
@@ -164,6 +179,7 @@ export function normalizeElTBox(rows, { budget = 500 } = {}) {
|
|
|
164
179
|
axioms.push({ form: "someLeft", role: "has", filler, sup: B, from: [opRow.id, ocRow.id, cardRow.id] });
|
|
165
180
|
continue;
|
|
166
181
|
}
|
|
182
|
+
if (allValuesFromRestrictions.has(B)) continue; // no EL universal restriction — skip, never guess an atomic concept
|
|
167
183
|
axioms.push({ form: "sub", sub: A, sup: B, from: [r.id] });
|
|
168
184
|
}
|
|
169
185
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// grammar/ace.mjs — tmct's deterministic ACE-OWL sub-fragment parser.
|
|
2
|
-
// Implements the
|
|
2
|
+
// Implements the 18 controlled-English sentence patterns of
|
|
3
3
|
// docs/references/schemas/ace-owl-fragment.md and nothing more: fitting the
|
|
4
4
|
// grammar is a strong signal, missing it is a FEATURE — parseAce returns null
|
|
5
5
|
// (or an empty-triples result carrying the unknown words as `residue`) and the
|
|
@@ -55,6 +55,8 @@ const PATTERN_DIFFERENT_FROM = "differentFrom";
|
|
|
55
55
|
const PATTERN_BARE_EXISTENTIAL = "bareExistential";
|
|
56
56
|
const PATTERN_TRANSITIVE_ROLE = "transitiveRole";
|
|
57
57
|
const PATTERN_INVERSE_ROLE = "inverseRole";
|
|
58
|
+
const PATTERN_UNIVERSAL = "universal";
|
|
59
|
+
const PATTERN_SUB_PROPERTY_OF = "subPropertyOf";
|
|
58
60
|
|
|
59
61
|
/** The pattern field's full domain, in the README's table order. */
|
|
60
62
|
const PATTERNS = Object.freeze([
|
|
@@ -62,7 +64,7 @@ const PATTERNS = Object.freeze([
|
|
|
62
64
|
PATTERN_CARDINALITY, PATTERN_DISJOINT_WITH, PATTERN_POSSESSIVE, PATTERN_ADJECTIVE,
|
|
63
65
|
PATTERN_CAPABILITY, PATTERN_UNION, PATTERN_COMPLEMENT, PATTERN_NEGATIVE_TYPE,
|
|
64
66
|
PATTERN_ENUMERATION, PATTERN_DIFFERENT_FROM, PATTERN_BARE_EXISTENTIAL, PATTERN_TRANSITIVE_ROLE,
|
|
65
|
-
PATTERN_INVERSE_ROLE,
|
|
67
|
+
PATTERN_INVERSE_ROLE, PATTERN_UNIVERSAL, PATTERN_SUB_PROPERTY_OF,
|
|
66
68
|
]);
|
|
67
69
|
|
|
68
70
|
const DET = new Set(["a", "an", "the"]);
|
|
@@ -438,6 +440,47 @@ function parseUnion(lexicon, toks, isIdx) {
|
|
|
438
440
|
]);
|
|
439
441
|
}
|
|
440
442
|
|
|
443
|
+
/** Pattern 18 — "every N1 VERB only N2" → allValuesFrom universal
|
|
444
|
+
* restriction: N1 ⊑ ∀VERB.N2, mirroring pattern 15's bare-existential
|
|
445
|
+
* node-naming convention (`all-<pred>-<filler>` beside
|
|
446
|
+
* `some-<pred>-<filler>`). Tried before the bare-existential arm so "only"
|
|
447
|
+
* claims the sentence first — "every heart contains only valves" never
|
|
448
|
+
* reaches pattern 15, and "every heart contains a valve" (no "only") always
|
|
449
|
+
* falls through to it unchanged. */
|
|
450
|
+
function parseUniversal(lexicon, toks, lower) {
|
|
451
|
+
const ns = lexicon.ns;
|
|
452
|
+
for (let i = 2; i < toks.length - 1; i += 1) {
|
|
453
|
+
const verb = lookupVerb(lexicon, lower[i]);
|
|
454
|
+
if (!verb) continue;
|
|
455
|
+
let objStart = i + 1;
|
|
456
|
+
if (verb.prep) {
|
|
457
|
+
if (lower[objStart] !== verb.prep) continue;
|
|
458
|
+
objStart += 1;
|
|
459
|
+
if (objStart >= toks.length) continue;
|
|
460
|
+
}
|
|
461
|
+
if (lower[objStart] !== "only") continue;
|
|
462
|
+
const fillerStart = objStart + 1;
|
|
463
|
+
if (fillerStart >= toks.length) continue;
|
|
464
|
+
const np1 = resolveNP(lexicon, toks.slice(1, i));
|
|
465
|
+
// Same homonym-tolerance as parseBareExistential: a subject that fails to
|
|
466
|
+
// resolve at THIS verb position tries the next candidate rather than
|
|
467
|
+
// committing to a miss that would shadow another pattern.
|
|
468
|
+
if (np1.term == null) continue;
|
|
469
|
+
const np2 = resolveNP(lexicon, toks.slice(fillerStart));
|
|
470
|
+
if (np2.term == null) return missOrNull(PATTERN_UNIVERSAL, [np1, np2]);
|
|
471
|
+
if (np1.individual || np2.individual) return null;
|
|
472
|
+
const pred = predicateOf(verb, ns);
|
|
473
|
+
const r = `${ns}all-${local(lexicon, pred)}-${local(lexicon, np2.term)}`;
|
|
474
|
+
return hit(PATTERN_UNIVERSAL, [np1, np2], [
|
|
475
|
+
{ subject: r, predicate: "rdf:type", object: "owl:Restriction", kind: "owl:allValuesFrom" },
|
|
476
|
+
{ subject: r, predicate: "owl:onProperty", object: pred, kind: "owl:allValuesFrom" },
|
|
477
|
+
{ subject: r, predicate: "owl:allValuesFrom", object: np2.term, kind: "owl:allValuesFrom" },
|
|
478
|
+
{ subject: np1.term, predicate: "rdfs:subClassOf", object: r, kind: "owl:allValuesFrom" },
|
|
479
|
+
]);
|
|
480
|
+
}
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
|
|
441
484
|
/** Pattern 15 — "every N1 VERB [det] N2" → someValuesFrom bare existential
|
|
442
485
|
* restriction: "every heart has a valve" (the `has` verb), and the general
|
|
443
486
|
* verb form "every heart contains a valve". Tried after the `that` and
|
|
@@ -487,6 +530,8 @@ function parseEvery(lexicon, toks, lower) {
|
|
|
487
530
|
if (hasIdx > 1 && (lower[hasIdx + 1] === "at" || lower[hasIdx + 1] === "exactly")) {
|
|
488
531
|
return parseCardinality(lexicon, toks, lower, hasIdx);
|
|
489
532
|
}
|
|
533
|
+
const universal = parseUniversal(lexicon, toks, lower);
|
|
534
|
+
if (universal) return universal;
|
|
490
535
|
const bareExistential = parseBareExistential(lexicon, toks, lower);
|
|
491
536
|
if (bareExistential) return bareExistential;
|
|
492
537
|
const isIdx = lower.indexOf("is");
|
|
@@ -638,6 +683,20 @@ function parseInverseRole(lexicon, toks, lower) {
|
|
|
638
683
|
]);
|
|
639
684
|
}
|
|
640
685
|
|
|
686
|
+
/** Pattern 19 — "V1 implies V2" → rdfs:subPropertyOf from V1's minted
|
|
687
|
+
* predicate to V2's minted predicate, one row, one direction — a
|
|
688
|
+
* subproperty relation is directional, unlike pattern 17's owl:inverseOf.
|
|
689
|
+
* Both sides accept the same 3sg-or-gerund fold patterns 16 and 17 already
|
|
690
|
+
* share, through the same verbPredicateFromSurface. */
|
|
691
|
+
function parseSubProperty(lexicon, toks, lower) {
|
|
692
|
+
const p1 = verbPredicateFromSurface(lexicon, lower[0]);
|
|
693
|
+
const p2 = verbPredicateFromSurface(lexicon, lower[2]);
|
|
694
|
+
if (!p1 || !p2) return null;
|
|
695
|
+
return hit(PATTERN_SUB_PROPERTY_OF, [], [
|
|
696
|
+
{ subject: p1, predicate: "rdfs:subPropertyOf", object: p2, kind: "rdfs:subPropertyOf" },
|
|
697
|
+
]);
|
|
698
|
+
}
|
|
699
|
+
|
|
641
700
|
/** Pattern 7 — "N1's N2 is VALUE" / "the N2 of N1 is VALUE": data or object
|
|
642
701
|
* property assertion per the possessive noun's DECLARED typing (undeclared
|
|
643
702
|
* typing defaults to data — a literal value is the honest floor). */
|
|
@@ -721,7 +780,7 @@ function parseCopula(lexicon, toks, lower, isIdx) {
|
|
|
721
780
|
]);
|
|
722
781
|
}
|
|
723
782
|
|
|
724
|
-
/** Parse one sentence against the
|
|
783
|
+
/** Parse one sentence against the 18-pattern ACE-OWL sub-fragment. See the
|
|
725
784
|
* file header for the result contract; `lexicon` defaults to the committed
|
|
726
785
|
* core under the library's own neutral DEFAULT_NS ("ex:") when the caller
|
|
727
786
|
* doesn't supply one. */
|
|
@@ -737,6 +796,10 @@ export function parseAce(sentence, lexicon = loadLexicon()) {
|
|
|
737
796
|
const inverse = parseInverseRole(lexicon, toks, lower);
|
|
738
797
|
if (inverse) return inverse;
|
|
739
798
|
}
|
|
799
|
+
if (toks.length === 3 && lower[1] === "implies") {
|
|
800
|
+
const subProperty = parseSubProperty(lexicon, toks, lower);
|
|
801
|
+
if (subProperty) return subProperty;
|
|
802
|
+
}
|
|
740
803
|
if (lower[0] === "everything" && lower[1] === "that") return parseComplement(lexicon, toks, lower);
|
|
741
804
|
if (lower[0] === "every") return parseEvery(lexicon, toks, lower);
|
|
742
805
|
if (lower[0] === "no") return parseDisjoint(lexicon, toks, lower);
|
package/src/domain/news-feed.mjs
CHANGED
|
@@ -60,11 +60,35 @@ export function scoreHubs(rows, windowRows, { limit = 6 } = {}) {
|
|
|
60
60
|
.slice(0, limit);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
/** One normalization pass over `rows`: each term to the row indices touching
|
|
64
|
+
* it, plus every row's own normalized pair. Built once per feed assembly and
|
|
65
|
+
* shared across every hub's walk — re-normalizing the whole store per
|
|
66
|
+
* frontier term is minutes of main-thread work on a browser-sized graph. */
|
|
67
|
+
export function buildTermAdjacency(rows) {
|
|
68
|
+
const byTerm = new Map();
|
|
69
|
+
const terms = new Array(rows.length);
|
|
70
|
+
for (let i = 0; i < rows.length; i += 1) {
|
|
71
|
+
const s = normFactTerm(rows[i].subject);
|
|
72
|
+
const o = normFactTerm(rows[i].object);
|
|
73
|
+
terms[i] = [s, o];
|
|
74
|
+
let forSubject = byTerm.get(s);
|
|
75
|
+
if (!forSubject) byTerm.set(s, (forSubject = []));
|
|
76
|
+
forSubject.push(i);
|
|
77
|
+
if (o !== s) {
|
|
78
|
+
let forObject = byTerm.get(o);
|
|
79
|
+
if (!forObject) byTerm.set(o, (forObject = []));
|
|
80
|
+
forObject.push(i);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { byTerm, terms };
|
|
84
|
+
}
|
|
85
|
+
|
|
63
86
|
/** Breadth-first over subject/object adjacency from `hub`, exactly `hops`
|
|
64
87
|
* levels deep, then capped by content-addressed id — so the cap never
|
|
65
88
|
* depends on `rows`' own order, only on which rows the hop-bounded walk
|
|
66
89
|
* actually reaches. */
|
|
67
|
-
export function subgraphAround(rows, hub, { hops = NEWS_HUB_HOPS, cap = 60 } = {}) {
|
|
90
|
+
export function subgraphAround(rows, hub, { hops = NEWS_HUB_HOPS, cap = 60, adjacency = null } = {}) {
|
|
91
|
+
const adj = adjacency ?? buildTermAdjacency(rows);
|
|
68
92
|
const hubTerm = normFactTerm(hub);
|
|
69
93
|
const visited = new Set([hubTerm]);
|
|
70
94
|
let frontier = [hubTerm];
|
|
@@ -72,11 +96,10 @@ export function subgraphAround(rows, hub, { hops = NEWS_HUB_HOPS, cap = 60 } = {
|
|
|
72
96
|
for (let hop = 0; hop < hops; hop += 1) {
|
|
73
97
|
const nextFrontier = new Set();
|
|
74
98
|
for (const term of [...frontier].sort()) {
|
|
75
|
-
const
|
|
76
|
-
|
|
99
|
+
for (const idx of adj.byTerm.get(term) ?? []) {
|
|
100
|
+
const row = rows[idx];
|
|
77
101
|
collected.set(row.id, row);
|
|
78
|
-
const s =
|
|
79
|
-
const o = normFactTerm(row.object);
|
|
102
|
+
const [s, o] = adj.terms[idx];
|
|
80
103
|
if (!visited.has(s)) nextFrontier.add(s);
|
|
81
104
|
if (!visited.has(o)) nextFrontier.add(o);
|
|
82
105
|
}
|
|
@@ -174,8 +197,9 @@ export function renderNewsParagraph(hub, subgraphRows) {
|
|
|
174
197
|
export function buildNewsItems(rows, { now, windowMs, limit = 6, sourcesByFactId = new Map() } = {}) {
|
|
175
198
|
const windowRows = newsWindowRows(rows, { now, windowMs });
|
|
176
199
|
const hubs = scoreHubs(rows, windowRows, { limit });
|
|
200
|
+
const adjacency = buildTermAdjacency(rows);
|
|
177
201
|
const items = hubs.map(({ term, changed }) => {
|
|
178
|
-
const subgraphRows = subgraphAround(rows, term);
|
|
202
|
+
const subgraphRows = subgraphAround(rows, term, { adjacency });
|
|
179
203
|
const factIds = subgraphRows.map((r) => r.id).sort();
|
|
180
204
|
return {
|
|
181
205
|
id: `news-feed:${sha256HexPrefix(`${term}\0${factIds.join(",")}`, 8)}`,
|