@avinashgaurav/manti 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +226 -2
- package/action/run.js +82 -0
- package/action.yml +46 -0
- package/bin/manti.js +123 -0
- package/package.json +50 -4
- package/src/ai.js +109 -0
- package/src/checks.js +534 -0
- package/src/dates.js +69 -0
- package/src/evalchecks.js +182 -0
- package/src/fix.js +137 -0
- package/src/index.js +99 -0
- package/src/morechecks.js +308 -0
- package/src/notecheck.js +130 -0
- package/src/numbers.js +141 -0
- package/src/text.js +64 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// Checks 15 to 23: scale and units, rounding drift, percent vs points, direction flips, relative dates,
|
|
2
|
+
// broken links, changed job titles, readability, and facts from the notes that never made it in.
|
|
3
|
+
// Same contract as checks.js: (units, ctx) -> [{ check, severity, line, text, message, suggest? }].
|
|
4
|
+
// `suggest` is { from, to }: an exact replacement the fixer can apply on that line.
|
|
5
|
+
|
|
6
|
+
import { findNumbers, isLabelled, isSupported } from "./numbers.js";
|
|
7
|
+
import { findDates } from "./dates.js";
|
|
8
|
+
import { URL_RE } from "./text.js";
|
|
9
|
+
|
|
10
|
+
const f = (check, severity, unit, text, message, suggest) => ({ check, severity, line: unit.line, text, message, ...(suggest ? { suggest } : {}) });
|
|
11
|
+
const STOP = new Set(("the a an and or of for on in to is are was were be we our it its this that with per each about around roughly nearly almost over under " +
|
|
12
|
+
"more than less from into have has had will would could should been being they them their there these those usd eur gbp inr dollars total").split(" "));
|
|
13
|
+
export const topicWords = (s) => new Set((s.toLowerCase().match(/[a-z]{4,}/g) ?? []).filter((w) => !STOP.has(w)));
|
|
14
|
+
const overlap = (a, b) => [...a].filter((w) => b.has(w)).length;
|
|
15
|
+
const close = (a, b, rel = 0.005) => Math.abs(a - b) <= Math.max(Math.abs(b) * rel, 1e-9);
|
|
16
|
+
const fmt = (v) => (+v.toFixed(2)).toLocaleString("en-US");
|
|
17
|
+
|
|
18
|
+
/** Numbers in the notes, each with the words around it. Cached on ctx. */
|
|
19
|
+
function facts(ctx) {
|
|
20
|
+
if (ctx._facts) return ctx._facts;
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const line of (ctx.source ?? ctx.sourceLower).split("\n")) {
|
|
23
|
+
if (!line.trim()) continue;
|
|
24
|
+
for (const n of findNumbers(line)) out.push({ ...n, line: line.trim(), words: topicWords(line) });
|
|
25
|
+
}
|
|
26
|
+
return (ctx._facts = out);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 15. Right digits, wrong size: 1.2M in the notes, 12M here; 200 ms in the notes, 200 s here. */
|
|
30
|
+
const FACTORS = [10, 100, 1000, 1e6, 1e9];
|
|
31
|
+
const UNIT_RE = /(\d[\d,]*(?:\.\d+)?)[\s-]?(ms|milliseconds?|secs?|seconds?|s|mins?|minutes?|hrs?|hours?|days?|weeks?|kb|mb|gb|tb)\b/gi;
|
|
32
|
+
const UNIT_GROUP = { ms: "ms", millisecond: "ms", milliseconds: "ms", s: "s", sec: "s", secs: "s", second: "s", seconds: "s", min: "min", mins: "min", minute: "min", minutes: "min", hr: "h", hrs: "h", hour: "h", hours: "h", day: "d", days: "d", week: "w", weeks: "w", kb: "KB", mb: "MB", gb: "GB", tb: "TB" };
|
|
33
|
+
const unitsIn = (t) => [...t.matchAll(UNIT_RE)].map((m) => ({ raw: m[0], value: parseFloat(m[1].replace(/,/g, "")), unit: UNIT_GROUP[m[2].toLowerCase()] }));
|
|
34
|
+
export function scaleMismatch(units, ctx) {
|
|
35
|
+
if (!ctx.hasSource) return [];
|
|
36
|
+
const out = [];
|
|
37
|
+
const src = facts(ctx);
|
|
38
|
+
const srcUnits = (ctx.source ?? ctx.sourceLower).split("\n").flatMap((line) => unitsIn(line).map((u) => ({ ...u, words: topicWords(line) })));
|
|
39
|
+
for (const u of units) {
|
|
40
|
+
if (u.heading) continue;
|
|
41
|
+
const words = topicWords(u.text);
|
|
42
|
+
for (const n of findNumbers(u.text)) {
|
|
43
|
+
// computable figures are fine unless they are exactly 10x, 100x or 1000x a figure the notes give for the same thing
|
|
44
|
+
const derivable = isSupported(n, ctx.derived);
|
|
45
|
+
if (derivable && ctx.values.some((v) => close(v, n.value))) continue;
|
|
46
|
+
// durations only compare with durations ("2.5 minutes" vs "25 minutes")
|
|
47
|
+
const hit = src.find((s) => (n.kind === "duration" ? s.kind === "duration" : s.kind === n.kind || (!derivable && (s.kind === "count" || n.kind === "count"))) && overlap(words, s.words) > 0 &&
|
|
48
|
+
(derivable ? [10, 100, 1000] : FACTORS).some((k) => close(n.value, s.value * k) || close(n.value * k, s.value)));
|
|
49
|
+
if (!hit) continue;
|
|
50
|
+
const k = Math.round(Math.max(n.value / hit.value, hit.value / n.value));
|
|
51
|
+
out.push(f("scale-mismatch", derivable ? "warning" : "error", u, n.raw, `${n.raw} is ${hit.raw} in your notes: ${k}x ${n.value > hit.value ? "too big" : "too small"}.`, { from: n.raw, to: hit.raw }));
|
|
52
|
+
}
|
|
53
|
+
for (const m of unitsIn(u.text)) {
|
|
54
|
+
// same number, different unit: topic words must match, unless that number appears with a unit only once in the notes
|
|
55
|
+
const same = srcUnits.filter((s) => s.value === m.value && s.unit !== m.unit && sameKind(s.unit, m.unit));
|
|
56
|
+
const hit = same.find((s) => overlap(words, s.words) > 0) ?? (same.length === 1 && srcUnits.filter((s) => s.value === m.value).length === 1 ? same[0] : null);
|
|
57
|
+
if (hit) out.push(f("scale-mismatch", "error", u, m.raw, `${m.raw} is ${hit.raw} in your notes: the unit changed.`, { from: m.raw, to: hit.raw }));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
const sameKind = (a, b) => ["ms", "s", "min", "h", "d", "w"].includes(a) === ["ms", "s", "min", "h", "d", "w"].includes(b);
|
|
63
|
+
|
|
64
|
+
/** 16. Rounding that changes the story: 38.6% in the notes becomes "nearly 50%". */
|
|
65
|
+
const APPROX = /(?:\b(just under|just over|nearly|almost|close to|about|around|roughly|approximately|more than|over|above|at least|less than|under|below)\b|(~))\s*([$€£₹]?\d[\d,]*(?:\.\d+)?(?:\s?(?:k|m|bn|million|billion)\b)?\s?%?)/gi;
|
|
66
|
+
const FRACTIONS = { half: 50, "a third": 33.3, "one third": 33.3, "two thirds": 66.7, "a quarter": 25, "one quarter": 25, "three quarters": 75, "a fifth": 20, "a tenth": 10 };
|
|
67
|
+
const FRACTION = /\b(just under|just over|nearly|almost|close to|about|around|roughly|approximately|more than|over|less than|under)\s+(half|a third|one third|two thirds|a quarter|one quarter|three quarters|a fifth|a tenth)\b/gi;
|
|
68
|
+
const RULE = {
|
|
69
|
+
"just under": (s, x) => s < x && s >= x * 0.9, nearly: (s, x) => s <= x && s >= x * 0.9, almost: (s, x) => s <= x && s >= x * 0.9, "close to": (s, x) => Math.abs(s - x) <= x * 0.1,
|
|
70
|
+
about: (s, x) => Math.abs(s - x) <= x * 0.1, around: (s, x) => Math.abs(s - x) <= x * 0.1, roughly: (s, x) => Math.abs(s - x) <= x * 0.1, approximately: (s, x) => Math.abs(s - x) <= x * 0.1, "~": (s, x) => Math.abs(s - x) <= x * 0.1,
|
|
71
|
+
"just over": (s, x) => s > x && s <= x * 1.1, "more than": (s, x) => s > x, over: (s, x) => s > x, above: (s, x) => s > x, "at least": (s, x) => s >= x,
|
|
72
|
+
"less than": (s, x) => s < x, under: (s, x) => s < x, below: (s, x) => s < x,
|
|
73
|
+
};
|
|
74
|
+
export function roundingDrift(units, ctx) {
|
|
75
|
+
if (!ctx.hasSource) return [];
|
|
76
|
+
const out = [];
|
|
77
|
+
const src = facts(ctx);
|
|
78
|
+
for (const u of units) {
|
|
79
|
+
if (u.heading || isLabelled(u.text)) continue;
|
|
80
|
+
const words = topicWords(u.text);
|
|
81
|
+
// "nearly half", "about a third", "over two thirds": a fraction in words is a percentage claim
|
|
82
|
+
for (const m of u.text.matchAll(FRACTION)) {
|
|
83
|
+
const x = FRACTIONS[m[2].toLowerCase()], word = m[1].toLowerCase();
|
|
84
|
+
const cands = src.filter((s) => s.kind === "percent" && overlap(words, s.words) > 0 && s.value > 0 && s.value < 100);
|
|
85
|
+
if (!cands.length) continue;
|
|
86
|
+
const s = cands.sort((a, b) => Math.abs(a.value - x) - Math.abs(b.value - x))[0];
|
|
87
|
+
if (RULE[word](s.value, x)) continue;
|
|
88
|
+
out.push(f("rounding-drift", "warning", u, m[0].trim(), `Your notes say ${s.raw}, so "${m[0].trim()}" ${s.value < x ? "overstates" : "understates"} it.`, { from: m[0].trim(), to: s.raw }));
|
|
89
|
+
}
|
|
90
|
+
for (const m of u.text.matchAll(APPROX)) {
|
|
91
|
+
const n = findNumbers(m[3])[0];
|
|
92
|
+
if (!n || n.value === 0) continue;
|
|
93
|
+
const word = (m[1] ?? m[2]).toLowerCase();
|
|
94
|
+
const cands = src.filter((s) => s.kind === n.kind && overlap(words, s.words) > 0 && s.value > 0 && Math.max(s.value / n.value, n.value / s.value) < 3);
|
|
95
|
+
if (!cands.length || cands.some((s) => s.value === n.value)) continue;
|
|
96
|
+
const s = cands.sort((a, b) => Math.abs(Math.log(a.value / n.value)) - Math.abs(Math.log(b.value / n.value)))[0];
|
|
97
|
+
if (RULE[word](s.value, n.value)) continue;
|
|
98
|
+
const dir = s.value < n.value ? "overstates" : "understates";
|
|
99
|
+
out.push(f("rounding-drift", "warning", u, m[0].trim(), `Your notes say ${s.raw}, so "${m[0].trim()}" ${dir} it.`, { from: m[0].trim(), to: s.raw }));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 17. Percent vs percentage points: 20% to 25% is 5 points, a 25% rise. */
|
|
106
|
+
const FROM_TO = /(\d+(?:\.\d+)?)\s?%\s*(?:[a-z]+\s+){0,3}?(?:to|→|->)\s*(\d+(?:\.\d+)?)\s?%/gi;
|
|
107
|
+
const CHANGE = /(?:\b(?:up|down|rose|fell|grew|dropped|increased|decreased|improved|declined|climbed|jumped|lifted|gained|lost|by|a|an)\s+(?:by\s+)?(\d+(?:\.\d+)?)\s?%(?!\s*(?:points?|pts?|pp)\b))|(?:(\d+(?:\.\d+)?)\s?%\s+(?:increase|rise|growth|jump|drop|decrease|decline|fall|lift|gain|improvement|uplift)\b)|(?:\b(?:increase|rise|gain|lift|jump|drop|decrease|decline|fall|improvement) of\s+(\d+(?:\.\d+)?)\s?%(?!\s*(?:points?|pts?|pp)\b))/gi;
|
|
108
|
+
function pairsIn(text) {
|
|
109
|
+
return [...text.matchAll(FROM_TO)].map((m) => ({ a: +m[1], b: +m[2] })).filter((p) => p.a > 0);
|
|
110
|
+
}
|
|
111
|
+
export function percentPoints(units, ctx) {
|
|
112
|
+
const out = [];
|
|
113
|
+
const srcPairs = ctx.hasSource ? (ctx.source ?? ctx.sourceLower).split("\n").flatMap((line) => pairsIn(line).map((p) => ({ ...p, words: topicWords(line) }))) : [];
|
|
114
|
+
for (const u of units) {
|
|
115
|
+
if (u.heading) continue;
|
|
116
|
+
const own = pairsIn(u.text);
|
|
117
|
+
const words = topicWords(u.text);
|
|
118
|
+
for (const m of u.text.matchAll(CHANGE)) {
|
|
119
|
+
const raw = m[1] ?? m[2] ?? m[3];
|
|
120
|
+
const x = +raw;
|
|
121
|
+
const p = own.find((q) => close(Math.abs(q.b - q.a), x, 0.01)) ??
|
|
122
|
+
(own.length ? null : srcPairs.find((q) => close(Math.abs(q.b - q.a), x, 0.01) && overlap(words, q.words) > 0));
|
|
123
|
+
if (!p) continue;
|
|
124
|
+
const rel = (Math.abs(p.b - p.a) / p.a) * 100;
|
|
125
|
+
if (close(rel, x, 0.02)) continue; // it really is the relative change
|
|
126
|
+
const pct = `${raw}%`;
|
|
127
|
+
out.push(f("percent-points", "error", u, pct, `${p.a}% to ${p.b}% is ${raw} percentage points (a ${fmt(rel)}% relative change), not ${pct}.`, { from: pct, to: `${raw} points` }));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 18. The notes say it went down; the draft says it went up. */
|
|
134
|
+
const UP = /\b(rose|risen|rises|increased|increases|grew|grown|jumped|climbed|gained|higher|edged up|ticked up|crept up|surged|soared|up(?=\s+(?:by\s+)?[$€£₹]?\d|\s+from))\b/i;
|
|
135
|
+
const DOWN = /\b(fell|fallen|falls|dropped|drops|declined|decreased|decreases|shrank|shrunk|lower|cut|slipped|slid|dipped|eased|edged down|ticked down|plunged|tumbled|down(?=\s+(?:by\s+)?[$€£₹]?\d|\s+from))\b/i;
|
|
136
|
+
function direction(text) {
|
|
137
|
+
const m = text.match(/from\s+[$€£₹]?([\d,]+(?:\.\d+)?)\s*(?:k|m|%)?\s*(?:[a-z]+\s+){0,3}?to\s+[$€£₹]?([\d,]+(?:\.\d+)?)/i);
|
|
138
|
+
// "from 88 to 92% ... to 71%" is a range, not a change
|
|
139
|
+
if (m && /\bto\s+[$€£₹]?\d/i.test(text.slice(m.index + m[0].length, m.index + m[0].length + 80))) return null;
|
|
140
|
+
if (m) {
|
|
141
|
+
const a = parseFloat(m[1].replace(/,/g, "")), b = parseFloat(m[2].replace(/,/g, ""));
|
|
142
|
+
if (a !== b) return { dir: b > a ? "up" : "down", fromTo: true };
|
|
143
|
+
}
|
|
144
|
+
const up = UP.test(text), down = DOWN.test(text);
|
|
145
|
+
return up !== down ? { dir: up ? "up" : "down", fromTo: false } : null;
|
|
146
|
+
}
|
|
147
|
+
export function directionFlip(units, ctx) {
|
|
148
|
+
const out = [];
|
|
149
|
+
const srcLines = ctx.hasSource ? (ctx.source ?? ctx.sourceLower).split("\n").map((line) => ({ line, d: direction(line), nums: findNumbers(line).map((n) => n.value), words: topicWords(line) })).filter((s) => s.d) : [];
|
|
150
|
+
for (const u of units) {
|
|
151
|
+
if (u.heading) continue;
|
|
152
|
+
const up = UP.exec(u.text), down = DOWN.exec(u.text);
|
|
153
|
+
const word = up && !down ? up[0] : down && !up ? down[0] : null;
|
|
154
|
+
if (!word) continue;
|
|
155
|
+
const said = up ? "up" : "down";
|
|
156
|
+
const d = direction(u.text);
|
|
157
|
+
if (d?.fromTo && d.dir !== said) {
|
|
158
|
+
out.push(f("direction-flip", "error", u, word, `"${word}" says ${said}, but the numbers in this sentence go ${d.dir}.`));
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const nums = findNumbers(u.text).map((n) => n.value);
|
|
162
|
+
const words = topicWords(u.text);
|
|
163
|
+
const hit = srcLines.find((s) => s.d.dir !== said && s.nums.some((v) => nums.includes(v)) && overlap(words, s.words) > 0);
|
|
164
|
+
if (hit) out.push(f("direction-flip", "error", u, word, `"${word}" says ${said}, but your notes say it went ${hit.d.dir}.`));
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 19. "Last quarter" in a doc with no date: in a month nobody will know which quarter. */
|
|
170
|
+
const REL = /\b(?:(?:last|this|next|previous|coming|past) (?:week|month|quarter|year|sprint)|yesterday|tomorrow|recently|lately|a few (?:days|weeks|months) ago|earlier this (?:week|month|quarter|year)|in the coming (?:days|weeks|months))\b/gi;
|
|
171
|
+
const ANCHOR = /\b(?:as of|dated?:|updated:?)\s|\b(?:Q[1-4]|H[12]|FY)\s*'?(?:20)?\d{2}\b|\b(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\.?\s+20\d{2}\b|\b20\d{2}-\d{2}\b/i;
|
|
172
|
+
export function relativeDates(units) {
|
|
173
|
+
if (units.some((u) => findDates(u.text).length || ANCHOR.test(u.text))) return []; // the doc says when "now" is
|
|
174
|
+
const out = [];
|
|
175
|
+
for (const u of units) {
|
|
176
|
+
if (u.heading) continue;
|
|
177
|
+
for (const m of u.text.matchAll(REL)) {
|
|
178
|
+
out.push(f("relative-date", "note", u, m[0], `"${m[0]}" has no anchor: the doc never says when it was written. Add a date, or say which ${/week|month|quarter|year|sprint/i.exec(m[0])?.[0] ?? "day"}.`));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** 20. Links that go nowhere: empty, placeholder, example domains, typos, missing reference links. */
|
|
185
|
+
const LOCAL = "https?:\\/\\/(?:localhost|127\\.0\\.0\\.1|0\\.0\\.0\\.0)\\b";
|
|
186
|
+
const BAD_MD = new RegExp(`\\[([^\\]]+)\\]\\(\\s*(#?|url|link|todo|tbd|(?:insert|add) (?:the |a )?link(?: here)?|link here|(?:https?:\\/\\/(?:www\\.)?(?:example\\.(?:com|org|net)|your-?(?:domain|site|company|link)\\.\\w+|domain\\.com|link\\.com|placeholder\\.\\w+)|${LOCAL})[^)]*)\\s*\\)`, "gi");
|
|
187
|
+
const BAD_BARE = new RegExp(`\\bhttps?:\\/\\/(?:www\\.)?(?:example\\.(?:com|org|net)|your-?(?:domain|site|company)\\.\\w+|domain\\.com|placeholder\\.\\w+)\\S*|\\b${LOCAL}\\S*`, "gi");
|
|
188
|
+
const TYPO = /\b(?:htps?:\/\/|https?\/\/|https?:\/(?!\/)|http:\/\/\/)\S*/gi;
|
|
189
|
+
export function brokenLinks(units) {
|
|
190
|
+
const out = [];
|
|
191
|
+
const defs = new Set(units.map((u) => u.raw.match(/^\s*\[([^\]]+)\]:\s*\S+/)?.[1].toLowerCase()).filter(Boolean));
|
|
192
|
+
const seen = new Set();
|
|
193
|
+
for (const u of units) {
|
|
194
|
+
if (seen.has(u.line)) continue;
|
|
195
|
+
seen.add(u.line);
|
|
196
|
+
const raw = u.raw;
|
|
197
|
+
for (const m of raw.matchAll(BAD_MD)) out.push(f("broken-link", "warning", u, m[0], `The link for "${m[1]}" goes nowhere (${m[2] || "empty"}). Add the real URL.`));
|
|
198
|
+
for (const m of raw.replace(BAD_MD, " ").matchAll(BAD_BARE)) out.push(f("broken-link", "warning", u, m[0], `${m[0]} is a placeholder domain. Add the real URL.`));
|
|
199
|
+
for (const m of raw.matchAll(TYPO)) out.push(f("broken-link", "warning", u, m[0], `${m[0]} is not a valid link (check the "https://").`));
|
|
200
|
+
for (const m of raw.matchAll(/\[([^\]]+)\]\[([^\]]*)\]/g)) {
|
|
201
|
+
const ref = (m[2] || m[1]).toLowerCase();
|
|
202
|
+
if (!defs.has(ref)) out.push(f("broken-link", "warning", u, m[0], `Reference link [${m[2] || m[1]}] is never defined.`));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 21. People whose job title changed between your notes and the draft. */
|
|
209
|
+
const TITLE = "(?:CEO|CTO|CFO|COO|CPO|CMO|CRO|CISO|(?:SVP|EVP|VP)(?:,? (?:of )?[A-Z][a-z]+)?|Head of [A-Z][a-z]+|Director(?:,? (?:of )?[A-Z][a-z]+)?|(?:Senior |Staff |Principal |Lead )?(?:PM|EM|Product Manager|Engineering Manager|Program Manager|Engineer|Designer|Analyst|Architect|Researcher)|(?:Eng|Engineering|Tech|Design|SRE|QA|Data|Product|Growth|Sales|Support|Security) Lead|Staff PM|Lead PM|Co-?founder|Founder|Intern)";
|
|
210
|
+
const NAME = "([A-Z][a-z]+(?: [A-Z][a-z]+)?)";
|
|
211
|
+
const NOT_NAME = new Set("The Our Their Your Its This That Every Each Any Some New Former Acting Interim And Or With Ask Tell".split(" "));
|
|
212
|
+
const ROLE_RES = [
|
|
213
|
+
new RegExp(`${NAME}\\s*\\((?:our |the )?(${TITLE})\\b[^)]*\\)`, "g"),
|
|
214
|
+
new RegExp(`${NAME},\\s*(?:our |the |a )?(${TITLE})\\b`, "g"),
|
|
215
|
+
new RegExp(`${NAME} (?:is|was|as) (?:our |the |a )?(${TITLE})\\b`, "g"),
|
|
216
|
+
new RegExp(`\\b(${TITLE}) ${NAME}\\b`, "g"),
|
|
217
|
+
];
|
|
218
|
+
function rolesIn(text) {
|
|
219
|
+
const out = [];
|
|
220
|
+
ROLE_RES.forEach((re, i) => {
|
|
221
|
+
for (const m of text.matchAll(re)) {
|
|
222
|
+
const [name, title] = i === 3 ? [m[2], m[1]] : [m[1], m[2]];
|
|
223
|
+
if (NOT_NAME.has(name.split(" ")[0]) || new RegExp(`^${TITLE}$`).test(name)) continue;
|
|
224
|
+
out.push({ name, title, span: m[0] });
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
const normTitle = (t) => t.toLowerCase().replace(/\beng\b/, "engineering").replace(/,? of /g, " ").replace(/,/g, "").replace(/product manager/, "pm").replace(/engineering manager/, "em").replace(/co-?founder/, "cofounder");
|
|
230
|
+
export function roleChanges(units, ctx) {
|
|
231
|
+
if (!ctx.hasSource) return [];
|
|
232
|
+
const known = new Map();
|
|
233
|
+
for (const r of rolesIn(ctx.source ?? "")) known.set(r.name.split(" ")[0].toLowerCase(), r);
|
|
234
|
+
if (!known.size) return [];
|
|
235
|
+
const out = [];
|
|
236
|
+
for (const u of units) {
|
|
237
|
+
for (const r of rolesIn(u.text)) {
|
|
238
|
+
const k = known.get(r.name.split(" ")[0].toLowerCase());
|
|
239
|
+
if (k && normTitle(k.title) !== normTitle(r.title)) {
|
|
240
|
+
out.push(f("role-change", "error", u, r.span, `${r.name} is ${/^[AEIOU]/.test(k.title) ? "an" : "a"} ${k.title} in your notes, not ${r.title}.`, { from: r.span, to: r.span.replace(r.title, k.title) }));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** 22. Hard to read: jargon, very long sentences, and a high reading grade. */
|
|
248
|
+
const JARGON = [
|
|
249
|
+
[/\butili[sz](e|es|ed|ing)\b/gi, (m) => ({ e: "use", es: "uses", ed: "used", ing: "using" })[m[1].toLowerCase()]],
|
|
250
|
+
[/\bleverag(e|es|ed|ing)(?=\s+(?:our|the|this|these|its|their|a|an|existing|AI|data|[a-z]+'s)\b)/gi, (m) => ({ e: "use", es: "uses", ed: "used", ing: "using" })[m[1].toLowerCase()]],
|
|
251
|
+
[/\bin order to\b/gi, () => "to"], [/\bdue to the fact that\b/gi, () => "because"], [/\bat this point in time\b/gi, () => "now"],
|
|
252
|
+
[/\bcircle back\b/gi, () => "follow up"], [/\bdouble[- ]click (?:on|into)\b/gi, () => "look closer at"], [/\bnorth star\b/gi, () => "main goal"], [/\balign on\b/gi, () => "agree on"], [/\btouch base\b/gi, () => "check in"], [/\btake (?:this|it) offline\b/gi, () => "discuss it separately"], [/\bunlock (?:value|growth)\b/gi, () => "create value"], [/\blow[- ]hanging fruit\b/gi, () => "quick wins"], [/\bmove the needle\b/gi, () => "make a real difference"],
|
|
253
|
+
[/\boperationali[sz]e\b/gi, () => "put into practice"], [/\bactionable insights\b/gi, () => "findings"], [/\bgoing forward\b/gi, () => "from now on"],
|
|
254
|
+
[/\bvalue[- ]add\b/gi, () => "benefit"], [/\bideate\b/gi, () => "brainstorm"],
|
|
255
|
+
[/\b(synerg(?:y|ies)|paradigm shift|holistic|best[- ]of[- ]breed|boil the ocean|thought leadership)\b/gi, () => null],
|
|
256
|
+
];
|
|
257
|
+
const matchCase = (src, rep) => (src[0] === src[0].toUpperCase() ? rep[0].toUpperCase() + rep.slice(1) : rep);
|
|
258
|
+
function syllables(w) {
|
|
259
|
+
w = w.toLowerCase().replace(/[^a-z]/g, "");
|
|
260
|
+
if (w.length <= 3) return 1;
|
|
261
|
+
const s = w.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, "").replace(/^y/, "").match(/[aeiouy]{1,2}/g);
|
|
262
|
+
return Math.max(1, s?.length ?? 1);
|
|
263
|
+
}
|
|
264
|
+
export function readability(units) {
|
|
265
|
+
const out = [];
|
|
266
|
+
let words = 0, sentences = 0, syl = 0;
|
|
267
|
+
for (const u of units) {
|
|
268
|
+
if (u.heading || u.table) continue;
|
|
269
|
+
const ws = u.text.replace(URL_RE, " ").match(/[A-Za-z][A-Za-z'-]*/g) ?? [];
|
|
270
|
+
if (/^\s*(?:[-*+]|\d+[.)])\s/.test(u.raw) && ws.length < 12) continue; // short bullets are not prose
|
|
271
|
+
if (ws.length >= 3) { words += ws.length; sentences++; syl += ws.reduce((s, w) => s + syllables(w), 0); }
|
|
272
|
+
if (ws.length > 40 && out.filter((x) => /words\. Split/.test(x.message)).length < 3) out.push(f("readability", "note", u, u.text.slice(0, 50), `This sentence is ${ws.length} words. Split it in two.`));
|
|
273
|
+
for (const [re, rep] of JARGON) {
|
|
274
|
+
for (const m of u.text.matchAll(re)) {
|
|
275
|
+
const to = rep(m);
|
|
276
|
+
out.push(f("readability", "note", u, m[0], to ? `"${m[0]}" is jargon. Say "${to}".` : `"${m[0]}" is jargon. Say what you mean in plain words.`, to ? { from: m[0], to: matchCase(m[0], to) } : undefined));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (sentences >= 5) {
|
|
281
|
+
const grade = 0.39 * (words / sentences) + 11.8 * (syl / words) - 15.59;
|
|
282
|
+
if (grade > 14) {
|
|
283
|
+
const first = units.find((u) => !u.heading) ?? units[0];
|
|
284
|
+
out.push(f("readability", "note", first, "reading grade", `The whole doc reads at grade ${Math.round(grade)}. Aim for 10 or under for busy readers: shorter sentences, plainer words.`));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** 23. Key figures in your notes that never made it into the draft. */
|
|
291
|
+
export function missingFacts(units, ctx) {
|
|
292
|
+
if (!ctx.hasSource || !units.length) return [];
|
|
293
|
+
const draftVals = units.flatMap((u) => findNumbers(u.text).map((n) => n.value));
|
|
294
|
+
const covered = (v) => draftVals.some((d) => close(d, v, 0.01) || close(d, v * 100, 0.01) || close(d * 100, v, 0.01) ||
|
|
295
|
+
[1e3, 1e6, 1e9].some((k) => close(d * k, v, 0.01))); // a column headed "($M)" or "(000s)" holds the figure without its scale
|
|
296
|
+
const seen = new Set();
|
|
297
|
+
const missing = [];
|
|
298
|
+
for (const s of facts(ctx)) {
|
|
299
|
+
const key = s.kind !== "count" || s.value >= 100;
|
|
300
|
+
if (!key || s.kind === "duration" || seen.has(s.value) || covered(s.value)) continue;
|
|
301
|
+
seen.add(s.value);
|
|
302
|
+
missing.push(s);
|
|
303
|
+
}
|
|
304
|
+
const at = units.find((u) => /^\s*#\s/.test(u.raw)) ?? units[0];
|
|
305
|
+
const out = missing.slice(0, 5).map((s) => f("missing-fact", "note", at, s.raw, `Your notes say "${s.line.length > 90 ? s.line.slice(0, 87) + "..." : s.line}", but the draft never uses ${s.raw}. Left out on purpose?`));
|
|
306
|
+
if (missing.length > 5) out.push(f("missing-fact", "note", at, `+${missing.length - 5}`, `${missing.length - 5} more figures from your notes are not in the draft.`));
|
|
307
|
+
return out;
|
|
308
|
+
}
|
package/src/notecheck.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Checks 24 and 25: claims that say the opposite of a status line in your notes, and counts that differ from them.
|
|
2
|
+
// Same contract as checks.js: (units, ctx) -> [{ check, severity, line, text, message }].
|
|
3
|
+
|
|
4
|
+
import { findNumbers } from "./numbers.js";
|
|
5
|
+
|
|
6
|
+
const f = (check, severity, unit, text, message) => ({ check, severity, line: unit.line, text, message });
|
|
7
|
+
const STOP = new Set(("the a an and or of for on in to is are was were be been being has have had this that with from into our their its it " +
|
|
8
|
+
"yet still any other new initial first release mvp only will would can could should explicitly currently").split(" "));
|
|
9
|
+
const stem = (w) => w.toLowerCase().replace(/(?:ically|ally|ing|ed|ies|es|s|ly)$/, "");
|
|
10
|
+
const words = (s) => (s.toLowerCase().match(/[a-z][a-z0-9-]+/g) ?? []).filter((w) => !STOP.has(w) && w.length >= 3);
|
|
11
|
+
const has = (sentenceStems, key) => sentenceStems.some((w) => w === key || (key.length >= 4 && w.startsWith(key)) || (w.length >= 4 && key.startsWith(w)));
|
|
12
|
+
const GA = (s) => s.replace(/\bGA\b/g, "general availability");
|
|
13
|
+
|
|
14
|
+
// What the notes say is excluded, not done, or still working.
|
|
15
|
+
const RULES = [
|
|
16
|
+
// "Azure and GCP are explicitly out of scope", "Push notifications are excluded from the MVP"
|
|
17
|
+
{ kind: "excluded", re: /^(?:[-*]\s*)?(?:but\s+)?(.{3,60}?)\s+(?:is|are)\s+(?:explicitly\s+|currently\s+)?(?:out of scope|excluded|not (?:in scope|included|supported))/i },
|
|
18
|
+
// "No support for GitHub Copilot", "No paid advertising campaign has been approved", "No revenue impact has been quantified"
|
|
19
|
+
{ kind: "none", re: /\bno\s+(?:support for\s+)?(.{3,60}?)\s*(?:(?:has|have|was|were|is|are)\s+(?:been\s+)?(?:approved|agreed|calculated|quantified|conducted|forecast|confirmed|finali[sz]ed|planned|set|made|priced)|in this release|yet\b|\.|$)/i },
|
|
20
|
+
// "Expansion into European markets is being evaluated, not approved", "Snowflake integration is still pending", "not yet priced", "awaiting approval"
|
|
21
|
+
{ kind: "open", re: /^(?:[-*]\s*)?(.{3,60}?)\s+(?:is|are|has|have)\s+(?:still\s+|not\s+yet\s+|yet to be\s+)?(?:being evaluated|pending|awaiting|not (?:yet )?(?:approved|priced|finali[sz]ed|confirmed|agreed|decided))/i },
|
|
22
|
+
// "Cloud resource monitoring and automated remediation continued functioning normally"
|
|
23
|
+
{ kind: "working", re: /^(?:[-*]\s*)?(.{3,60}?)\s+(?:continued (?:functioning|working|operating)|remained (?:operational|available|up)|(?:was|were) not affected|unaffected)/i },
|
|
24
|
+
];
|
|
25
|
+
const NEGATION = /\b(?:no|not|never|without|excluded?|out of scope|outside|later|pending|awaiting|evaluat\w*|until|future|tbd|tbc|n't|instead)\b/i;
|
|
26
|
+
const DONE = /\b(?:approved|completed?|confirmed|agreed|live|launched|included|includ\w+|finali[sz]ed|signed|support(?:s|ed)?|will be charged|charged|priced|estimated|projected|expected|forecast)\b/i;
|
|
27
|
+
const DOWN = /\b(?:unavailable|down|failed|offline|outage|affected|disrupted|stopped)\b/i;
|
|
28
|
+
|
|
29
|
+
function statements(source) {
|
|
30
|
+
const out = [];
|
|
31
|
+
for (const raw of source.split("\n")) {
|
|
32
|
+
const line = GA(raw.replace(/^\s*[-*]\s*/, ""));
|
|
33
|
+
for (const clause of line.split(/(?<=[.;])\s+|;\s*/)) {
|
|
34
|
+
for (const r of RULES) {
|
|
35
|
+
const m = clause.match(r.re);
|
|
36
|
+
if (!m) continue;
|
|
37
|
+
const thing = m[1].replace(/^(?:the|our|any|all)\s+/i, "").replace(/[:,]$/, "");
|
|
38
|
+
// "No additional hiring beyond these three positions" is a limit, not a ban
|
|
39
|
+
if (/\b(?:beyond|other than|except|more than|outside of|apart from)\b/i.test(thing)) break;
|
|
40
|
+
// "No discounts or reseller margins" is two things
|
|
41
|
+
for (const part of thing.split(/\s+(?:or|and|nor)\s+|,\s*/)) {
|
|
42
|
+
const keys = [...new Set(words(part).map(stem))].filter((k) => k.length >= 3);
|
|
43
|
+
if (keys.length) out.push({ kind: r.kind, keys, note: raw.trim().replace(/^[-*]\s*/, ""), all: thing.includes(" and ") && r.kind === "excluded" });
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// "Azure and GCP are out of scope": both must appear, so treat them as one group as well
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 24. The draft says the opposite of what your notes say about a decision, a scope or a status. */
|
|
54
|
+
export function notesConflict(units, ctx) {
|
|
55
|
+
if (!ctx.hasSource) return [];
|
|
56
|
+
const facts = statements(ctx.source ?? "");
|
|
57
|
+
if (!facts.length) return [];
|
|
58
|
+
const srcNums = new Set(findNumbers(ctx.source ?? "").map((n) => n.value));
|
|
59
|
+
const out = [];
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
for (const u of units) {
|
|
62
|
+
if (u.heading) continue;
|
|
63
|
+
const text = GA(u.text);
|
|
64
|
+
const st = words(text).map(stem);
|
|
65
|
+
for (const fa of facts) {
|
|
66
|
+
const hits = fa.keys.filter((k) => has(st, k)).length;
|
|
67
|
+
const need = fa.keys.length >= 2 ? 2 : 1;
|
|
68
|
+
if (hits < need) continue;
|
|
69
|
+
let bad = false;
|
|
70
|
+
if (fa.kind === "excluded") bad = !NEGATION.test(text);
|
|
71
|
+
else if (fa.kind === "working") bad = DOWN.test(text) && !/\bnot\b/i.test(text);
|
|
72
|
+
else {
|
|
73
|
+
// "none" and "open": the draft must assert something: a done-word, or a figure the notes never give
|
|
74
|
+
const newFigure = findNumbers(text).some((n) => !srcNums.has(n.value));
|
|
75
|
+
bad = !NEGATION.test(text) && (DONE.test(text) || newFigure) && (need === 2 || newFigure || DONE.test(text));
|
|
76
|
+
if (need === 1 && !newFigure && !/\b(?:approved|completed?|confirmed|agreed|finali[sz]ed|included)\b/i.test(text)) bad = false;
|
|
77
|
+
}
|
|
78
|
+
if (!bad) continue;
|
|
79
|
+
const key = `${u.line}|${fa.note}`;
|
|
80
|
+
if (seen.has(key)) continue;
|
|
81
|
+
seen.add(key);
|
|
82
|
+
out.push(f("notes-conflict", "warning", u, u.text.slice(0, 60), `Your notes say "${fa.note.length > 110 ? fa.note.slice(0, 107) + "..." : fa.note}" This line says otherwise.`));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 25. A count that differs from the notes for the same thing: "10 existing customers" when the notes say 5. */
|
|
89
|
+
const WORDNUM = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10, eleven: 11, twelve: 12, fifteen: 15, twenty: 20, thirty: 30, fifty: 50, hundred: 100 };
|
|
90
|
+
const GENERIC = new Set("day week month year hour minute min sec second time per percent point item thing one more".split(" "));
|
|
91
|
+
const DESCRIBERS = new Set("existing new current total active additional paying pilot beta enterprise".split(" "));
|
|
92
|
+
function counts(text) {
|
|
93
|
+
const out = [];
|
|
94
|
+
const re = /\b(\d[\d,]*(?:\.\d+)?|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|fifteen|twenty|thirty|fifty|hundred)\s+([a-z][a-z-]+)(?:\s+([a-z][a-z-]+))?/gi;
|
|
95
|
+
for (const m of text.matchAll(re)) {
|
|
96
|
+
const v = /\d/.test(m[1]) ? parseFloat(m[1].replace(/,/g, "")) : WORDNUM[m[1].toLowerCase()];
|
|
97
|
+
if (v == null || (v >= 1900 && v <= 2100)) continue;
|
|
98
|
+
const before = text.slice(Math.max(0, m.index - 1), m.index);
|
|
99
|
+
if (/[$€£₹.%:\/-]/.test(before)) continue;
|
|
100
|
+
const w1 = stem(m[2]), w2 = m[3] ? stem(m[3]) : "";
|
|
101
|
+
// the counted thing: skip describers ("10 existing customers" -> customer)
|
|
102
|
+
const noun = DESCRIBERS.has(m[2].toLowerCase()) && w2 ? w2 : w1;
|
|
103
|
+
if (!noun || STOP.has(m[2].toLowerCase()) || ["time", "per", "percent", "point", "item", "thing", "one", "more"].includes(noun)) continue;
|
|
104
|
+
out.push({ v, raw: m[0], noun, desc: DESCRIBERS.has(m[2].toLowerCase()) ? m[2].toLowerCase() : "", generic: GENERIC.has(noun) });
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
export function countMismatch(units, ctx) {
|
|
109
|
+
if (!ctx.hasSource) return [];
|
|
110
|
+
const notes = [];
|
|
111
|
+
// "11 seats each" is a per-item figure, not a total
|
|
112
|
+
for (const line of (ctx.source ?? "").split("\n")) for (const c of counts(line)) if (!/\b(?:each|apiece)\b/i.test(c.raw + " " + line.slice(line.indexOf(c.raw) + c.raw.length, line.indexOf(c.raw) + c.raw.length + 8))) notes.push({ ...c, words: new Set(words(line).map(stem)), line: line.trim().replace(/^[-*]\s*/, "") });
|
|
113
|
+
const out = [];
|
|
114
|
+
for (const u of units) {
|
|
115
|
+
if (u.heading || u.table) continue;
|
|
116
|
+
const st = new Set(words(u.text).map(stem));
|
|
117
|
+
for (const c of counts(u.text)) {
|
|
118
|
+
const same = notes.filter((n) => n.noun === c.noun && (!c.desc || !n.desc || c.desc === n.desc));
|
|
119
|
+
if (!same.length || same.some((n) => n.v === c.v)) continue;
|
|
120
|
+
// exactly 10x, 100x or 1000x off is the scale check's finding, not a second one
|
|
121
|
+
if (same.some((n) => [10, 100, 1000].some((k) => Math.abs(n.v * k - c.v) < 1e-9 || Math.abs(c.v * k - n.v) < 1e-9))) continue;
|
|
122
|
+
// the sentences must be about the same thing: share at least one more word than the noun
|
|
123
|
+
// durations ("12 months" vs "6 months") are everywhere, so they need two shared topic words
|
|
124
|
+
const hit = same.find((n) => [...n.words].filter((w) => w !== c.noun && st.has(w)).length >= (c.generic ? 2 : 1));
|
|
125
|
+
if (!hit) continue;
|
|
126
|
+
out.push(f("count-mismatch", "warning", u, c.raw, `Your notes say "${hit.raw}" ("${hit.line.length > 90 ? hit.line.slice(0, 87) + "..." : hit.line}"), not ${c.raw}.`));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
package/src/numbers.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Finding numbers in text and deciding whether a source supports them.
|
|
2
|
+
|
|
3
|
+
import { stripUrls } from "./text.js";
|
|
4
|
+
import { findDates } from "./dates.js";
|
|
5
|
+
|
|
6
|
+
const MONTHS = "jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec";
|
|
7
|
+
const MULT = { k: 1e3, thousand: 1e3, m: 1e6, mn: 1e6, million: 1e6, b: 1e9, bn: 1e9, billion: 1e9, lakh: 1e5, lakhs: 1e5, crore: 1e7, crores: 1e7, cr: 1e7 };
|
|
8
|
+
|
|
9
|
+
// A number with optional currency, thousands separators, decimals, scale and unit.
|
|
10
|
+
const NUM_RE =
|
|
11
|
+
/(?<cur>[$€£₹¥]|\b(?:USD|EUR|GBP|INR|Rs\.?)\s?)?(?<n>\d{1,3}(?:,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?)(?<scale>\s?(?:k|m|mn|bn|b|thousand|million|billion|lakhs?|crores?|cr)\b)?(?<pct>\s?%|\s?percent\b|\s?pp\b)?(?<x>x\b)?/gi;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Every number in a sentence, with its value, kind and position. Dates,
|
|
15
|
+
* list markers, quarter names, versions and years are skipped: dates are
|
|
16
|
+
* handled by the date check.
|
|
17
|
+
*/
|
|
18
|
+
export function findNumbers(sentence) {
|
|
19
|
+
const text = stripUrls(sentence);
|
|
20
|
+
const found = [];
|
|
21
|
+
for (const m of text.matchAll(NUM_RE)) {
|
|
22
|
+
const { cur, n, scale, pct, x } = m.groups;
|
|
23
|
+
const start = m.index;
|
|
24
|
+
const end = start + m[0].length;
|
|
25
|
+
const before = text.slice(Math.max(0, start - 12), start);
|
|
26
|
+
const after = text.slice(end, end + 14);
|
|
27
|
+
const raw = m[0].trim();
|
|
28
|
+
|
|
29
|
+
if (/^\s*$/.test(text.slice(0, start)) && /^[.)]\s/.test(after)) continue; // "1. item"
|
|
30
|
+
if (/[A-Za-z_]$/.test(before) && !cur) continue; // Q1, H2, P0, v2, OKR1
|
|
31
|
+
if (/\b(?:version|ver|release|v)\.?\s*$/i.test(before) && !cur) continue; // Version 2.1
|
|
32
|
+
if (/^\.\d/.test(after) || /\d\.$/.test(before)) continue; // version strings 1.2.3
|
|
33
|
+
if (/^\s?(?:st|nd|rd|th)\b/i.test(after)) continue; // ordinals
|
|
34
|
+
if (new RegExp(`(?:${MONTHS})[a-z]*\\.?\\s*$`, "i").test(before)) continue; // May 15
|
|
35
|
+
if (new RegExp(`^\\s*(?:${MONTHS})[a-z]*\\b`, "i").test(after)) continue; // 15 May
|
|
36
|
+
if (/^[-/]\d/.test(after) && /^\d{4}$/.test(n)) continue; // 2026-05-15
|
|
37
|
+
if (/\d[-/]$/.test(before)) continue; // tail of an ISO date
|
|
38
|
+
if (/^\d{4}$/.test(n) && +n >= 1900 && +n <= 2100 && !cur && !pct && !scale) continue; // a year
|
|
39
|
+
if (/^:\d/.test(after) || /\d:$/.test(before)) continue; // times 10:30
|
|
40
|
+
if (/[A-Za-z][-–]$/.test(before)) continue; // L-14, T-3, D-7 countdown labels
|
|
41
|
+
// Section and key-result numbers: "KR 1.1", "Objective 2", "**3.2**", "Step 4.1"
|
|
42
|
+
if (/^\d{1,2}(\.\d{1,2})?$/.test(n) && !cur && !pct && /\b(?:KR|O|Obj(?:ective)?|Key Result|Step|Phase|Section|Stage|Story|Epic)\s*$/i.test(text.slice(Math.max(0, start - 16), start))) continue;
|
|
43
|
+
// At the start of a line only when it reads as a number label ("3.2 Scope", "**3**", "2: Pricing"),
|
|
44
|
+
// not "19 enterprise deals are blocked".
|
|
45
|
+
if (/^\d{1,2}(\.\d{1,2})?$/.test(n) && !cur && !pct && /^[\s>*|#-]*\**$/.test(text.slice(0, start)) && (n.includes(".") || /\*\*$/.test(text.slice(0, start)) || /^\**(?:\s+[A-Z]|[:.)]|\s*$)/.test(text.slice(end)))) continue;
|
|
46
|
+
// ...and the rest of a list of them: "KR1.1–1.4", "KR 1.1, 1.2 and 3.2"
|
|
47
|
+
if (/^\d{1,2}(\.\d{1,2})?$/.test(n) && !cur && !pct && /\b(?:KRs?|Key Results?|Steps?|Sections?|Phases?)\s*\d{1,2}(?:\.\d{1,2})?(?:\s*(?:[,–-]|and|to)\s*\d{1,2}(?:\.\d{1,2})?)*\s*(?:[,–-]|and|to)\s*$/i.test(text.slice(Math.max(0, start - 60), start))) continue;
|
|
48
|
+
|
|
49
|
+
let value = parseFloat(n.replace(/,/g, ""));
|
|
50
|
+
if (scale) value *= MULT[scale.trim().toLowerCase()] ?? 1;
|
|
51
|
+
const kind = pct ? "percent" : cur ? "money" : x ? "multiple" : "count";
|
|
52
|
+
// Small bare counts ("3 moves", "2 options") are structure, not claims.
|
|
53
|
+
if (kind === "count" && !scale && value <= 10 && Number.isInteger(value)) continue;
|
|
54
|
+
const duration = !cur && !pct && /^\s?(?:business |working |calendar )?(?:days?|weeks?|wks?|months?|hours?|hrs?|minutes?|mins?|sprints?|quarters?)\b/i.test(after);
|
|
55
|
+
found.push({ raw, value, kind: duration ? "duration" : kind, start, end, decimals: (n.split(".")[1] || "").length });
|
|
56
|
+
}
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** All numeric values that appear in the source text. */
|
|
61
|
+
export function sourceValues(sourceText) {
|
|
62
|
+
const values = [];
|
|
63
|
+
for (const line of sourceText.split(/\n/)) {
|
|
64
|
+
for (const n of findNumbers(line)) values.push(n.value);
|
|
65
|
+
// Also accept small counts and years from the source, so they can be derived from.
|
|
66
|
+
// but not labels and years: Q2, H1, v2 and 2026 are not quantities, and using them lets almost anything "derive"
|
|
67
|
+
for (const m of stripUrls(line).matchAll(/(?<![A-Za-z\d.])\d+(?:\.\d+)?(?![\d])/g)) {
|
|
68
|
+
const v = parseFloat(m[0]);
|
|
69
|
+
if (Number.isInteger(v) && v >= 1900 && v <= 2100) continue;
|
|
70
|
+
values.push(v);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return [...new Set(values)];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The numbers in the source a reader would compute with: everything but dates, years, times and list markers. */
|
|
77
|
+
export function sourceFigures(sourceText) {
|
|
78
|
+
const values = [];
|
|
79
|
+
for (const line of sourceText.split(/\n/)) {
|
|
80
|
+
const text = stripUrls(line);
|
|
81
|
+
const dates = findDates(text).map((d) => [d.index, d.index + d.raw.length]);
|
|
82
|
+
for (const m of text.matchAll(/\d[\d,]*(?:\.\d+)?/g)) {
|
|
83
|
+
const at = m.index, v = parseFloat(m[0].replace(/,/g, ""));
|
|
84
|
+
if (dates.some(([a, b]) => at >= a && at < b)) continue; // "11 December", "2026-05-15"
|
|
85
|
+
if (/^\d{4}$/.test(m[0]) && v >= 1900 && v <= 2100) continue; // a year
|
|
86
|
+
if (/^\s*$/.test(text.slice(0, at)) && /^[.)]\s/.test(text.slice(at + m[0].length))) continue; // "1. item"
|
|
87
|
+
if (/\d:$/.test(text.slice(0, at)) || /^:\d/.test(text.slice(at + m[0].length))) continue; // 14:05
|
|
88
|
+
if (/[A-Za-z][-–]?$/.test(text.slice(0, at))) continue; // Q3, KR1, INC-2291
|
|
89
|
+
values.push(v);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return [...new Set(values)];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Values a reader can compute from the source: sums, differences, ratios,
|
|
97
|
+
* percentages and percent changes of any two source numbers, and simple
|
|
98
|
+
* time conversions of one.
|
|
99
|
+
* Two-number results only combine `figures`: a day of the month, a year or a list number
|
|
100
|
+
* is not something a reader computes with, and letting them in made almost every
|
|
101
|
+
* invented number look derived (any value within 10 of a real one, and nearly any
|
|
102
|
+
* percentage as the "change" between two dates).
|
|
103
|
+
*/
|
|
104
|
+
export function derivedValues(values, figures = values) {
|
|
105
|
+
const out = new Set(values);
|
|
106
|
+
const fig = new Set(figures);
|
|
107
|
+
for (const a of values) {
|
|
108
|
+
for (const f of [12, 4, 52, 365, 1 / 12, 1 / 4, 1 / 52, 1 / 365]) out.add(a * f);
|
|
109
|
+
if (Math.abs(a) <= 1) out.add(a * 100); // 0.25 -> 25%
|
|
110
|
+
if (Math.abs(a) >= 1) out.add(a / 100); // 25% -> 0.25
|
|
111
|
+
}
|
|
112
|
+
const vs = values.filter((v) => fig.has(v)).slice(0, 200);
|
|
113
|
+
for (const a of vs) {
|
|
114
|
+
for (const b of vs) {
|
|
115
|
+
if (a === b) continue;
|
|
116
|
+
out.add(a + b);
|
|
117
|
+
out.add(a - b);
|
|
118
|
+
if (b !== 0) {
|
|
119
|
+
out.add(a / b);
|
|
120
|
+
out.add((a / b) * 100);
|
|
121
|
+
out.add(((a - b) / b) * 100);
|
|
122
|
+
out.add(((b - a) / b) * 100);
|
|
123
|
+
}
|
|
124
|
+
out.add((a * b) / 100); // 50% of 3,050
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return [...out];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** True when the value matches a source or derived value, allowing rounding. */
|
|
131
|
+
export function isSupported(n, derived) {
|
|
132
|
+
const tol = Math.max(Math.abs(n.value) * 0.005, 0.5 * 10 ** -n.decimals);
|
|
133
|
+
return derived.some((d) => Math.abs(d - n.value) <= tol || Math.abs(Math.abs(d) - n.value) <= tol);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** True when the sentence marks its figures as not-fact. */
|
|
137
|
+
export function isLabelled(sentence) {
|
|
138
|
+
return /\b(assum\w*|estimat\w*|target\w*|tbd|goal|hypothes\w*|projected|projection|forecast\w*|illustrative|example|e\.g\.|for instance|placeholder|to (?:be )?(?:confirm|validat|verif)\w*|unverified|guess)\b/i.test(
|
|
139
|
+
sentence,
|
|
140
|
+
);
|
|
141
|
+
}
|
package/src/text.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Splitting a markdown document into lines and sentences we can check.
|
|
2
|
+
|
|
3
|
+
const FENCE = /^\s*(```|~~~)/;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Turn markdown into checkable units. Each unit is one sentence (or one table
|
|
7
|
+
* row) with its 1-based line number. Code blocks, inline code and URLs are
|
|
8
|
+
* masked so their contents are never read as claims.
|
|
9
|
+
*/
|
|
10
|
+
export function toUnits(markdown) {
|
|
11
|
+
const units = [];
|
|
12
|
+
let inFence = false;
|
|
13
|
+
const lines = markdown.replace(/\r\n?/g, "\n").split("\n");
|
|
14
|
+
lines.forEach((raw, i) => {
|
|
15
|
+
if (FENCE.test(raw)) {
|
|
16
|
+
inFence = !inFence;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (inFence) return;
|
|
20
|
+
const line = i + 1;
|
|
21
|
+
const heading = /^\s*#{1,6}\s/.test(raw);
|
|
22
|
+
const text = maskCode(raw);
|
|
23
|
+
if (!text.trim()) return;
|
|
24
|
+
if (/^\s*\|/.test(text) && text.length <= 4000) {
|
|
25
|
+
if (/^\s*\|?\s*:?-{3,}/.test(text)) return; // table divider
|
|
26
|
+
units.push({ line, text, raw, heading: false, table: true });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
for (const s of splitSentences(text)) {
|
|
30
|
+
// a single "sentence" longer than this is not prose; cut it so no check does quadratic work on it
|
|
31
|
+
for (let k = 0; k < s.length; k += 1000) units.push({ line, text: s.slice(k, k + 1000), raw: raw.length > 4000 ? raw.slice(0, 4000) : raw, heading, table: false });
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return units;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Replace inline code with spaces of the same length. */
|
|
38
|
+
export function maskCode(s) {
|
|
39
|
+
return s.replace(/`[^`]*`/g, (m) => " ".repeat(m.length));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Split a line into sentences without breaking decimals or abbreviations. */
|
|
43
|
+
export function splitSentences(line) {
|
|
44
|
+
const out = [];
|
|
45
|
+
let start = 0;
|
|
46
|
+
const re = /[.!?](?=\s+[A-Z(*"\[]|\s*$)/g;
|
|
47
|
+
let m;
|
|
48
|
+
while ((m = re.exec(line))) {
|
|
49
|
+
const before = line.slice(Math.max(0, m.index - 4), m.index + 1);
|
|
50
|
+
if (/\b(e\.g|i\.e|vs|etc|approx|est|no|Inc|Ltd|Dr|Mr|Ms)\.$/i.test(before)) continue;
|
|
51
|
+
out.push(line.slice(start, m.index + 1).trim());
|
|
52
|
+
start = m.index + 1;
|
|
53
|
+
}
|
|
54
|
+
const rest = line.slice(start).trim();
|
|
55
|
+
if (rest) out.push(rest);
|
|
56
|
+
return out.filter((s) => /[A-Za-z0-9]/.test(s));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const URL_RE = /\bhttps?:\/\/[^\s)\]>"']+/g;
|
|
60
|
+
|
|
61
|
+
/** Text with URLs replaced by spaces, so numbers inside links are ignored. */
|
|
62
|
+
export function stripUrls(s) {
|
|
63
|
+
return s.replace(URL_RE, (m) => " ".repeat(m.length));
|
|
64
|
+
}
|