File size: 7,080 Bytes
995b09c
4d4c4ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Source number check: find figures an AI answer did not get from its source</title>
<meta name="description" content="Paste a source and an AI answer: every number in the answer that does not appear in the source is highlighted. Works in the browser, nothing is sent.">
<style>
:root{--bg:#fff;--ink:#17212B;--mut:#5B6773;--line:#E9EDF1;--acc:#0B7285;--ok:#157A5E;--okb:#E3F4EC;--ko:#B42318;--kob:#FDECEA;--panel:#FBFCFD}
@media (prefers-color-scheme:dark){:root{--bg:#12171D;--ink:#E6EBF0;--mut:#98A4B0;--line:#2C3640;--acc:#4FB3C4;--ok:#4CC49E;--okb:#0E2A22;--ko:#F87171;--kob:#3A1414;--panel:#1A2129}}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:16px/1.55 system-ui,-apple-system,Segoe UI,Roboto,sans-serif}
main{max-width:980px;margin:0 auto;padding:32px 16px 48px}h1{font-size:clamp(26px,4vw,36px);line-height:1.15;margin:0 0 8px}
.lead{color:var(--mut);margin:0 0 22px}a{color:var(--acc)}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:16px}@media (max-width:760px){.grid{grid-template-columns:1fr}}
label{display:block;font-weight:600;margin:0 0 6px}textarea{width:100%;min-height:220px;padding:10px 12px;border:1px solid var(--line);border-radius:10px;background:var(--panel);color:var(--ink);font:14px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;resize:vertical}
.bar{display:flex;flex-wrap:wrap;gap:10px;align-items:center;margin:14px 0}
button{border:1px solid var(--line);background:var(--panel);color:var(--ink);border-radius:8px;padding:8px 14px;font:inherit;cursor:pointer}
button.main{background:var(--acc);border-color:var(--acc);color:#fff;font-weight:600}.opt{color:var(--mut);font-size:14px}
#sum{font-weight:600;margin:6px 0 10px}#out{white-space:pre-wrap;border:1px solid var(--line);border-radius:10px;padding:14px;min-height:60px;background:var(--panel)}
mark{border-radius:4px;padding:0 3px}mark.ok{background:var(--okb);color:var(--ok)}mark.ko{background:var(--kob);color:var(--ko);font-weight:700}
.note{font-size:14px;color:var(--mut)}footer{margin-top:32px;font-size:14px;color:var(--mut);border-top:1px solid var(--line);padding-top:14px}
</style></head><body><main>
<h1>Source number check</h1>
<p class="lead">Paste the source an AI was given (a table, a document, retrieved passages) and the answer it wrote. Every number in the answer that does not appear in the source is highlighted in red. Useful to test a RAG pipeline or a grounded assistant. Everything runs in your browser: nothing is sent anywhere.</p>
<div class="grid">
  <div><label for="src">Source given to the AI</label><textarea id="src" spellcheck="false"></textarea></div>
  <div><label for="ans">AI answer</label><textarea id="ans" spellcheck="false"></textarea></div>
</div>
<div class="bar">
  <button class="main" id="go">Check the numbers</button>
  <button id="ex">Load an example</button>
  <label class="opt" style="display:inline;font-weight:400"><input type="checkbox" id="small" checked> ignore whole numbers below 10</label>
  <label class="opt" style="display:inline;font-weight:400"><input type="checkbox" id="years"> ignore years (1900–2099)</label>
</div>
<p id="sum"></p>
<div id="out" aria-live="polite"></div>
<p class="note">Numbers are compared by value, whatever the format: 39,232 · 39.232 · 39 232 · 39232 are the same number, and 0,45 equals 0.45. A red number is not necessarily wrong (it may be a sum, a percentage or a rounding), but it does not come from the source as written: check it.</p>
<footer>Made by <a href="https://contextetech.com/en/">Contexte Tech</a>, an open European platform for verified AI context. The same check validates our open datasets grounded in official statistics, where every figure must come from the context: <a href="https://contextetech.com/en/articles/donnees-publiques-europeennes-ia">how we built them</a>. Source code of the platform: <a href="https://github.com/contexte-tech/contextetech">GitHub</a>.</footer>
</main>
<script>
// Nombres : chiffres avec séparateurs (point, virgule, espace, espace insécable), comparés par valeur
const RE = /\d+(?:[.,   ]\d+)*/g;
function value(tok) {
  let t = tok.replace(/[   ]/g, " ");
  if (/ \d{3}(?!\d)/.test(t)) t = t.replace(/ /g, "");          // espaces entre groupes de 3 = milliers
  else t = t.split(" ")[0];
  const dots = (t.match(/\./g) || []).length, commas = (t.match(/,/g) || []).length;
  if (dots && commas) {                                            // le dernier séparateur est décimal
    const dec = t.lastIndexOf(".") > t.lastIndexOf(",") ? "." : ",";
    t = t.split(dec === "." ? "," : ".").join("").replace(dec, ".");
  } else if (dots || commas) {
    const sep = dots ? "." : ",", parts = t.split(sep);
    const thousands = parts.length > 2 || (parts.length === 2 && parts[1].length === 3 && parts[0].length <= 3 && parts[0] !== "0");
    t = thousands ? parts.join("") : parts.join(".");
  }
  return parseFloat(t);
}
const esc = (s) => s.replace(/[&<>]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" })[c]);
function check() {
  const src = document.getElementById("src").value, ans = document.getElementById("ans").value;
  const small = document.getElementById("small").checked, years = document.getElementById("years").checked;
  const known = new Set((src.match(RE) || []).map(value).filter((v) => !isNaN(v)).map(String));
  let ok = 0, ko = 0, last = 0, html = "";
  for (const m of ans.matchAll(RE)) {
    const v = value(m[0]);
    html += esc(ans.slice(last, m.index));
    const skip = isNaN(v) || (small && v < 10 && !/[.,]/.test(m[0])) || (years && Number.isInteger(v) && v >= 1900 && v <= 2099 && m[0].length === 4);
    if (skip) html += esc(m[0]);
    else if (known.has(String(v))) { ok++; html += `<mark class="ok">${esc(m[0])}</mark>`; }
    else { ko++; html += `<mark class="ko" title="Not found in the source">${esc(m[0])}</mark>`; }
    last = m.index + m[0].length;
  }
  html += esc(ans.slice(last));
  document.getElementById("out").innerHTML = html || "<span class='note'>Paste an answer to check.</span>";
  document.getElementById("sum").textContent = ok + ko === 0 ? "" :
    ko === 0 ? `All ${ok} numbers come from the source.` : `${ko} number${ko > 1 ? "s" : ""} not found in the source, ${ok} found.`;
}
document.getElementById("go").onclick = check;
document.getElementById("ex").onclick = () => {
  document.getElementById("src").value = "INE data (municipal register, 1 January 2025):\n- Torrent (Valencia): 90,928 inhabitants, 44,395 men, 46,533 women\n- Arucas (Las Palmas): 39,232 inhabitants, 19,421 men, 19,811 women\n- Ontinyent (Valencia): 37,012 inhabitants, 18,273 men, 18,739 women";
  document.getElementById("ans").value = "According to the municipal register on 1 January 2025, Arucas had 39.232 inhabitants (19.421 men and 19.811 women), slightly more than Ontinyent with 37 012. Its population grew by 1.8% in a year, reaching about 39,500 people.";
  check();
};
</script>
</body></html>