diff --git a/report.js b/report.js index afaf662..03d9d88 100644 --- a/report.js +++ b/report.js @@ -139,24 +139,67 @@ function factcheck(report, evidenceText, keyNums) { const strip = t => t.replace(/0x[0-9a-fA-F]+/g, ' ').replace(/\b\d{4}-\d{2}-\d{2}\b/g, ' ').replace(/\b\d{1,2}:\d{2}\b/g, ' '); const stripped = strip(all); const okNum = v => allowed.has(v) || [2, 3, 4, 5, 6].some(d => allowed.has(Number(v.toFixed(d)))); - // shown arithmetic: a number that is the sum or difference of 2 to 4 other allowed numbers written in the same sentence - const shown = (v, sentence) => { - const xs = numbersIn(sentence).map(x => x.v).filter(x => x !== v && okNum(x)).slice(0, 12); - const near = t => Math.abs(t - v) <= Math.max(0.011, Math.abs(v) * 1e-9); - const n = xs.length; - for (let a = 0; a < n; a++) for (let b = a + 1; b < n; b++) { - if (near(xs[a] + xs[b]) || near(Math.abs(xs[a] - xs[b]))) return true; - for (let c = b + 1; c < n; c++) { if (near(xs[a] + xs[b] + xs[c])) return true; for (let d = c + 1; d < n; d++) if (near(xs[a] + xs[b] + xs[c] + xs[d])) return true; } + // shown arithmetic, checked per sentence (Marty's rule: evidence numbers or plain arithmetic of them, written out): + // (1) an explicit expression "A x B + C = R" (or "... = R1 = R2") whose inputs are all allowed is evaluated, and R is + // accepted when it matches to the decimals written; (2) a number that is the sum or difference of 2 to 4 allowed + // numbers in the sentence, or a ratio / percentage of two of them (to the decimals written). Numbers proven in a + // sentence count as allowed for the rest of that sentence only. + const tol = raw => { const d = raw.includes('.') ? raw.split('.')[1].length : 0; return 0.5 * Math.pow(10, -d) + 1e-9; }; + const evalExpr = e => { + const t = e.replace(/(\d),(?=\d{3}\b)/g, '$1').replace(/[$%]/g, '').replace(/[x\u00d7]/gi, '*').trim(); + if (!/^[\d.\s+\-*/()]+$/.test(t) || !/\d/.test(t) || !/[+\-*/]/.test(t)) return null; + let i = 0; const S = t.replace(/\s+/g, ''); + const num = () => { const m = /^\d+(?:\.\d+)?/.exec(S.slice(i)); if (!m) throw 0; i += m[0].length; return Number(m[0]); }; + const atom = () => { if (S[i] === '(') { i++; const v = sum(); if (S[i] !== ')') throw 0; i++; return v; } if (S[i] === '-') { i++; return -atom(); } return num(); }; + const prod = () => { let v = atom(); while (S[i] === '*' || S[i] === '/') { const o = S[i++]; const w = atom(); v = o === '*' ? v * w : v / w; } return v; }; + const sum = () => { let v = prod(); while (S[i] === '+' || S[i] === '-') { const o = S[i++]; const w = prod(); v = o === '+' ? v + w : v - w; } return v; }; + try { const v = sum(); return i === S.length && isFinite(v) ? v : null; } catch (x) { return null; } + }; + const proveSentence = se => { + const ok = new Set(); + const isOk = n => okNum(n.v) || ok.has(n.raw) || (!n.raw.includes('.') && !n.raw.includes(',') && n.v < 10); + for (let pass = 0; pass < 3; pass++) { + // (1) explicit expressions + const parts = se.split('='); + for (let k = 1; k < parts.length; k++) { + const rm = /^\s*\$?\s*(\d{1,3}(?:,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?)/.exec(parts[k]); if (!rm) continue; + const em = /[\d.,\s+\-x\u00d7*/()%$]*$/i.exec(parts[k - 1]); if (!em) continue; + let expr = em[0].replace(/^[\s,.)]+/, ''); // trim to a balanced start + while ((expr.match(/\)/g) || []).length > (expr.match(/\(/g) || []).length && expr.includes(')')) expr = expr.slice(expr.indexOf(')') + 1); + while ((expr.match(/\(/g) || []).length > (expr.match(/\)/g) || []).length) expr = expr.slice(expr.indexOf('(') + 1); + const ins = numbersIn(expr); if (!ins.length || !ins.every(isOk)) continue; + const v = evalExpr(expr); const r = Number(rm[1].replace(/,/g, '')); + if (v != null && Math.abs(v - r) <= tol(rm[1])) ok.add(rm[1]); + else if (v != null) { // the right side is itself an expression ("... = 355 / 10 = 35.5"): equal values prove its numbers + const rx = /^[\d.,\s+\-x×*/()%$]*/i.exec(parts[k])[0].replace(/[\s,.]+$/, ''); const rv = evalExpr(rx); + if (rv != null && Math.abs(rv - v) <= Math.max(1e-9, Math.abs(v) * 1e-9)) for (const q of numbersIn(rx)) ok.add(q.raw); + } + } + // (2) sums, differences, ratios of allowed numbers in the sentence + const all = numbersIn(se); const xs = all.filter(x => okNum(x.v) || ok.has(x.raw)).map(x => x.v).slice(0, 12); // small skipped counts never feed sums + for (const n of all) { + if (isOk(n)) continue; + const v = n.v, tl = tol(n.raw), near = t => Math.abs(t - v) <= Math.max(0.011, Math.abs(v) * 1e-9); + let hit = false; const m = xs.length; + for (let a = 0; a < m && !hit; a++) for (let b = 0; b < m && !hit; b++) { + if (a === b) continue; + if (b > a && (near(xs[a] + xs[b]) || near(Math.abs(xs[a] - xs[b])))) hit = true; + else if (xs[b] && (Math.abs(xs[a] / xs[b] * 100 - v) <= tl || Math.abs(xs[a] / xs[b] - v) <= tl)) hit = true; + for (let c = b + 1; c < m && !hit && b > a; c++) { if (near(xs[a] + xs[b] + xs[c])) hit = true; for (let d = c + 1; d < m && !hit; d++) if (near(xs[a] + xs[b] + xs[c] + xs[d])) hit = true; } + } + if (hit) ok.add(n.raw); + } } - return false; + return ok; }; const sentences = texts.flatMap(t => strip(t).split(/(?<=[.;!?])\s+/)); + const provenIn = sentences.map(se => ({ se, ok: proveSentence(se) })); for (const n of numbersIn(stripped)) { const v = n.v; if (!n.raw.includes('.') && !n.raw.includes(',') && v < 10) continue; // small counts and scores if (!n.raw.includes('.') && !n.raw.includes(',') && v >= 1990 && v <= 2035) continue; // years if (okNum(v)) continue; - if (sentences.some(se => se.includes(n.raw) && shown(v, se))) continue; + if (provenIn.some(x => x.ok.has(n.raw) && x.se.includes(n.raw))) continue; // a rounded form of an allowed value (3,632 for 3,632.30, 29.06k style excluded) let near = false; // integers: allowed already holds every allowed value rounded to 0 decimals (3,632 for 3,632.30) if (n.raw.includes('.')) { const d = n.raw.split('.')[1].length; for (const x of [v - Math.pow(10, -d), v + Math.pow(10, -d)]) if (allowed.has(Number(x.toFixed(d)))) near = true; }