File size: 3,840 Bytes
c07bca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Next Trace Space: paste a document and a question; see which sentences support the answer, or an abstention. */
const { $, h, fmt } = NLUI;
let ctx, examples;

function render() {
  const doc = $("doc").value, q = $("q").value, mode = $("mode").value;
  const t0 = performance.now();
  const r = mode === "bm25" ? Core.bm25Baseline(ctx, q, doc) : Core.scoreDocument(ctx, q, doc);
  const ms = performance.now() - t0;
  const ui = $("lang").value === "tr";
  const v = $("verdict"); v.innerHTML = "";
  if (!r.spans.length) { v.textContent = ui ? "Belge boş." : "The document is empty."; return r; }
  if (r.abstain) {
    v.append(h("div", { class: "badge warn", style: "font-size:15px;padding:6px 12px" }, ui ? "Belge bu soruyu yanıtlamak için yeterli kanıt içermiyor." : "The document does not contain enough evidence to answer."));
  } else {
    v.append(h("div", { class: "badge", style: "font-size:15px;padding:6px 12px" }, (ui ? "Yanıtın dayandığı cümle: " : "Answer supported by sentence ") + r.evidence.map(i => "#" + (i + 1)).join(", ")));
  }
  const extra = mode === "bm25" ? `max BM25 ${fmt(Math.max(...r.bm25), 2)} (threshold ${fmt(ctx.m.meta.thresholds.bm25, 2)})`
    : `answerability ${fmt(r.answerable, 3)} (threshold ${fmt(ctx.m.meta.thresholds.answerable, 3)})`;
  v.append(h("div", { class: "small muted", style: "margin-top:6px" }, `${extra} · ${r.spans.length} sentences · ${fmt(ms, 1)} ms`));
  const out = $("marked"); out.innerHTML = "";
  const mx = Math.max(...r.bm25, 1e-9);
  r.spans.forEach(([a, b], i) => {
    const s = mode === "bm25" ? r.bm25[i] / mx : r.probs[i];
    const ev = r.evidence.includes(i);
    out.append(h("span", { style: `background:rgba(255,200,40,${0.08 + 0.6 * s});border-radius:4px;padding:1px 2px;${ev ? "outline:2px solid #0b6e4f;" : ""}`, title: `#${i + 1}: ${fmt(s, 3)}` },
      h("sup", { class: "muted" }, String(i + 1)), doc.slice(a, b)), " ");
  });
  const tb = $("tbl"); tb.innerHTML = "";
  tb.append(h("tr", {}, h("th", {}, "#"), h("th", { class: "num" }, mode === "bm25" ? "BM25" : "evidence p"), h("th", {}, ui ? "cümle" : "sentence")));
  const order = r.spans.map((_, i) => i).sort((x, y) => (mode === "bm25" ? r.bm25[y] - r.bm25[x] : r.probs[y] - r.probs[x]));
  for (const i of order.slice(0, 5)) tb.append(h("tr", {}, h("td", {}, "#" + (i + 1)), h("td", { class: "num" }, fmt(mode === "bm25" ? r.bm25[i] : r.probs[i], 3)),
    h("td", {}, doc.slice(r.spans[i][0], r.spans[i][1]).slice(0, 140))));
  return r;
}

function load(i) {
  const e = examples[i];
  $("doc").value = e.document; $("q").value = e.question; $("lang").value = e.lang;
  $("src").textContent = e.source;
  render();
}

window.__liveTest = function () {
  $("mode").value = "learned";
  $("doc").value = "Kasaba kütüphanesi 1962 yılında açıldı. Binanın mimarı Selma Arı'dır. Kütüphanede bugün yaklaşık 40 bin kitap bulunuyor. Her yıl haziran ayında bir kitap şenliği düzenleniyor.";
  $("q").value = "Kütüphane hangi yıl açıldı?"; $("q").dispatchEvent(new Event("input"));
  const a = render();
  $("q").value = "Kütüphanenin ilk müdürü kimdi?"; const b = render();
  return { ok: a.spans.length === 4 && a.probs.length === 4, answerable_evidence: a.evidence, answerable_abstain: a.abstain, answerable_p: a.answerable,
    unanswerable_abstain: b.abstain, unanswerable_p: b.answerable };
};

NLUI.boot(Core, async c => {
  ctx = c;
  examples = await (await fetch("examples.json")).json();
  examples.forEach((e, i) => $("ex").append(h("option", { value: i }, `${e.lang.toUpperCase()} · ${e.title}`)));
  $("ex").addEventListener("change", e => load(Number(e.target.value)));
  for (const id of ["doc", "q"]) $(id).addEventListener("input", render);
  for (const id of ["mode", "lang"]) $(id).addEventListener("change", render);
  load(0);
});