michaelkimmel commited on
Commit
7334a85
·
verified ·
1 Parent(s): 991f1af

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. app.js +49 -7
  2. index.html +3 -3
  3. scan.js +22 -4
  4. style.css +1 -0
app.js CHANGED
@@ -909,8 +909,9 @@
909
 
910
  // metre: scansion of the lines of verse, computed in the browser
911
  const meters = window.Scan && Scan.metersFor(w.id, w.genre);
912
- if (meters) {
913
- const lines = range.slice(0, 6);
 
914
  cats.push({ id: "metre", short: "metre", name: "Metre · scansion", count: lines.length, render: (el) => {
915
  const NAME = { hex: "dactylic hexameter", pent: "elegiac pentameter", trim: "iambic trimeter", trimc: "iambic trimeter (comic)", anap: "anapaestic tetrameter (catalectic)" };
916
  const rows = lines.map((li) => {
@@ -1692,6 +1693,7 @@ grc,φίλον ἦτορ,"dear heart, my heart",Polis</pre>
1692
  }
1693
  async function semSearch(q, lang, genre, keep) {
1694
  const out = await SEM.model("Represent this sentence for searching relevant passages: " + q, { pooling: "cls", normalize: true });
 
1695
  return semRank(out.data, lang, genre, keep, -1);
1696
  }
1697
  // the stored vector of passage i, as a unit-length query
@@ -1768,6 +1770,36 @@ grc,φίλον ἦτορ,"dear heart, my heart",Polis</pre>
1768
  if (o.like) ps.push("like=" + encodeURIComponent(o.like));
1769
  return "#/ideas/" + encodeURIComponent(q || "") + (ps.length ? "?" + ps.join("&") : "");
1770
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1771
  async function renderIdeas(q0, params0) {
1772
  const raw = location.hash.replace(/^#\/ideas\/?/, "");
1773
  const cut = raw.indexOf("?");
@@ -1827,13 +1859,23 @@ grc,φίλον ἦτορ,"dear heart, my heart",Polis</pre>
1827
  return `<div class="item idea" data-i="${i}"><div class="src-name"><a href="#${w.id}/${b}">${esc(w.author)}, ${esc(w.title)}</a> <span class="muted small">${w.books.length > 1 ? esc((w.bookLabel || "Book") + " " + b) : ""} ${reranked ? "" : " · match " + (s * 100).toFixed(0)}</span></div><div class="q en loading small">…</div></div>`;
1828
  }).join("") || `<p class="muted">Nothing found.</p>`;
1829
  status(`${top.length} passages, best matches first (at most three per work)${reranked ? ", re-ranked" : ""}`);
1830
- for (const [i] of top) {
1831
- const [wi, b, first, ns] = SEM.meta.p[i], w = S.byId[SEM.meta.works[wi]];
1832
- semText(i).then(({ text, ref }) => {
1833
- const words = text.split(/\s+/), cut = words.length > 90 ? words.slice(0, 90).join(" ") + "…" : text;
 
1834
  const el = box.querySelector(`[data-i="${i}"] .q`);
1835
- if (el) { el.classList.remove("loading", "small"); el.innerHTML = `${esc(cut)} <a class="small" href="#${w.id}/${b}/${esc(ref)}">${esc(refLabel(w, b, ref))} → open in the text</a>`; }
 
 
1836
  });
 
 
 
 
 
 
 
1837
  }
1838
  } catch (e) {
1839
  console.error(e);
 
909
 
910
  // metre: scansion of the lines of verse, computed in the browser
911
  const meters = window.Scan && Scan.metersFor(w.id, w.genre);
912
+ const scanLines = meters ? range.filter((li) => !(Scan.skipLine && Scan.skipLine(w.id, book.book, book.lines[li][0]))) : [];
913
+ if (meters && scanLines.length) {
914
+ const lines = scanLines.slice(0, 6);
915
  cats.push({ id: "metre", short: "metre", name: "Metre · scansion", count: lines.length, render: (el) => {
916
  const NAME = { hex: "dactylic hexameter", pent: "elegiac pentameter", trim: "iambic trimeter", trimc: "iambic trimeter (comic)", anap: "anapaestic tetrameter (catalectic)" };
917
  const rows = lines.map((li) => {
 
1693
  }
1694
  async function semSearch(q, lang, genre, keep) {
1695
  const out = await SEM.model("Represent this sentence for searching relevant passages: " + q, { pooling: "cls", normalize: true });
1696
+ SEM.lastQ = out.data;
1697
  return semRank(out.data, lang, genre, keep, -1);
1698
  }
1699
  // the stored vector of passage i, as a unit-length query
 
1770
  if (o.like) ps.push("like=" + encodeURIComponent(o.like));
1771
  return "#/ideas/" + encodeURIComponent(q || "") + (ps.length ? "?" + ps.join("&") : "");
1772
  }
1773
+ // split a passage into sentences (short pieces join the one before) and find the one closest to the question
1774
+ function semSentences(text) {
1775
+ const raw = text.match(/[^.!?;]+(?:[.!?;]+["'”’)\]]*|$)\s*/g) || [text];
1776
+ const out = [];
1777
+ for (const r of raw) {
1778
+ if (out.length && (r.trim().split(/\s+/).length < 5 || out[out.length - 1].trim().split(/\s+/).length < 5)) out[out.length - 1] += r;
1779
+ else out.push(r);
1780
+ }
1781
+ return out;
1782
+ }
1783
+ async function semBestSentence(text, qv) {
1784
+ const ss = semSentences(text);
1785
+ if (ss.length < 2) return null;
1786
+ const out = await SEM.model(ss.map((x) => x.trim()), { pooling: "cls", normalize: true });
1787
+ const d = out.dims[1], data = out.data;
1788
+ let best = 0, bs = -1;
1789
+ ss.forEach((_, k) => { let v = 0; for (let j = 0; j < d; j++) v += data[k * d + j] * qv[j]; if (v > bs) { bs = v; best = k; } });
1790
+ return { ss, best };
1791
+ }
1792
+ // the snippet shown for a result: around the best sentence (marked) when known, else the opening
1793
+ function semSnippet(text, hit) {
1794
+ if (!hit) { const words = text.split(/\s+/); return esc(words.length > 90 ? words.slice(0, 90).join(" ") + "…" : text); }
1795
+ const { ss, best } = hit;
1796
+ let a = best, b = best + 1, n = ss[best].split(/\s+/).length;
1797
+ while (n < 70 && (a > 0 || b < ss.length)) {
1798
+ if (a > 0) { a--; n += ss[a].split(/\s+/).length; }
1799
+ if (n < 70 && b < ss.length) { n += ss[b].split(/\s+/).length; b++; }
1800
+ }
1801
+ return (a > 0 ? "… " : "") + ss.slice(a, b).map((x, k) => a + k === best ? `<mark class="best">${esc(x.trim())}</mark> ` : esc(x)).join("") + (b < ss.length ? " …" : "");
1802
+ }
1803
  async function renderIdeas(q0, params0) {
1804
  const raw = location.hash.replace(/^#\/ideas\/?/, "");
1805
  const cut = raw.indexOf("?");
 
1859
  return `<div class="item idea" data-i="${i}"><div class="src-name"><a href="#${w.id}/${b}">${esc(w.author)}, ${esc(w.title)}</a> <span class="muted small">${w.books.length > 1 ? esc((w.bookLabel || "Book") + " " + b) : ""} ${reranked ? "" : " · match " + (s * 100).toFixed(0)}</span></div><div class="q en loading small">…</div></div>`;
1860
  }).join("") || `<p class="muted">Nothing found.</p>`;
1861
  status(`${top.length} passages, best matches first (at most three per work)${reranked ? ", re-ranked" : ""}`);
1862
+ // the texts first; then, one passage at a time, the sentence that best matches the question is marked
1863
+ const qv = like ? null : SEM.lastQ, token = (S.ideaRun = (S.ideaRun || 0) + 1);
1864
+ const shown = top.map(([i]) => {
1865
+ const [wi, b] = SEM.meta.p[i], w = S.byId[SEM.meta.works[wi]];
1866
+ return semText(i).then(({ text, ref }) => {
1867
  const el = box.querySelector(`[data-i="${i}"] .q`);
1868
+ const link = ` <a class="small" href="#${w.id}/${b}/${esc(ref)}">${esc(refLabel(w, b, ref))} → open in the text</a>`;
1869
+ if (el) { el.classList.remove("loading", "small"); el.innerHTML = semSnippet(text) + link; }
1870
+ return { el, text, link };
1871
  });
1872
+ });
1873
+ if (qv && SEM.model) {
1874
+ for (const p of shown) {
1875
+ const { el, text, link } = await p;
1876
+ if (S.ideaRun !== token || !el || !document.body.contains(el)) break;
1877
+ try { const hit = await semBestSentence(text, qv); if (hit) el.innerHTML = semSnippet(text, hit) + link; } catch (e) { console.error(e); break; }
1878
+ }
1879
  }
1880
  } catch (e) {
1881
  console.error(e);
index.html CHANGED
@@ -8,7 +8,7 @@
8
  <link rel="preconnect" href="https://fonts.googleapis.com">
9
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,400;0,600;0,700;1,400&family=Noto+Serif:ital,wght@0,400;0,700;1,400&family=IBM+Plex+Mono:wght@400;500&display=swap">
11
- <link rel="stylesheet" href="style.css?v=28">
12
  </head>
13
  <body>
14
  <header class="top">
@@ -53,7 +53,7 @@
53
  window.MOUSEION_DATA = new URLSearchParams(location.search).get("data")
54
  || (location.hostname.endsWith(".hf.space") ? "https://huggingface.co/datasets/michaelkimmel/mouseion-library-data/resolve/main/" : "data/");
55
  </script>
56
- <script src="scan.js?v=5"></script>
57
- <script src="app.js?v=51"></script>
58
  </body>
59
  </html>
 
8
  <link rel="preconnect" href="https://fonts.googleapis.com">
9
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,400;0,600;0,700;1,400&family=Noto+Serif:ital,wght@0,400;0,700;1,400&family=IBM+Plex+Mono:wght@400;500&display=swap">
11
+ <link rel="stylesheet" href="style.css?v=29">
12
  </head>
13
  <body>
14
  <header class="top">
 
53
  window.MOUSEION_DATA = new URLSearchParams(location.search).get("data")
54
  || (location.hostname.endsWith(".hf.space") ? "https://huggingface.co/datasets/michaelkimmel/mouseion-library-data/resolve/main/" : "data/");
55
  </script>
56
+ <script src="scan.js?v=6"></script>
57
+ <script src="app.js?v=52"></script>
58
  </body>
59
  </html>
scan.js CHANGED
@@ -329,6 +329,13 @@
329
  if (ok.length) r.paths = ok;
330
  else { extra = (r.paths.some(diaer) ? 0 : 1.2) + (r.paths.some((p) => !clash(p)) ? 0 : 0.8); const k2 = r.paths.filter((p) => diaer(p)); if (k2.length) r.paths = k2; }
331
  }
 
 
 
 
 
 
 
332
  const total = r.cost + v.cost + extra;
333
  if (total > MAXCOST) continue;
334
  if (!best || total < best.total - 1e-9) best = { total, v, sylls, meter, r };
@@ -431,18 +438,29 @@
431
  const HEX = new Set(["iliad", "odyssey", "argonautica", "posthomerica-the-fall-of-troy", "theogony", "works", "shield",
432
  "eclogues", "georgics", "aeneid", "metamorphoses", "pharsalia", "statius-thebaid", "statius-achilleid", "satires",
433
  "juvenal-satires", "the-art-of-poetry-to-the-pisos", "mosella", "cupido-cruciatur", "griphus-ternarii-numeri",
434
- "precationes", "genethliacon-ad-ausonium-nepotem", "ausonius-epistularum"]);
 
 
 
435
  const ELEG = new Set(["amours", "epistles", "art-of-love", "art-of-beauty", "remedy-of-love", "epicedion-in-patrem",
436
  "de-bello-gildonico", "de-bello-gothico", "de-consulatu-stilichonis", "in-eutropium", "in-rufinum", "de-raptu-proserpinae",
437
- "epithalamium-de-nuptiis-honorii-augusti"]);
438
  function metersFor(id, genre) {
439
- if (genre === "Tragedy") return ["trim"];
440
  if (genre === "Comedy" && /^(clouds|birds)$/.test(id)) return ["trimc", "anap"];
441
  if (ELEG.has(id) || /^panegyricus-/.test(id)) return ["hex", "pent"];
442
  if (HEX.has(id) || /^hymn-\d+-/.test(id)) return ["hex"];
443
  return null;
444
  }
445
 
446
- root.Scan = { scan, metersFor };
 
 
 
 
 
 
 
 
447
  if (typeof module !== "undefined") module.exports = root.Scan;
448
  })(typeof window !== "undefined" ? window : globalThis);
 
329
  if (ok.length) r.paths = ok;
330
  else { extra = (r.paths.some(diaer) ? 0 : 1.2) + (r.paths.some((p) => !clash(p)) ? 0 : 0.8); const k2 = r.paths.filter((p) => diaer(p)); if (k2.length) r.paths = k2; }
331
  }
332
+ if (meter === "hex" && lang !== "grc") {
333
+ // a Latin line forced into a hexameter shows as spondee after spondee (an iambic line read with
334
+ // lengths the spelling does not fix): keep only parses with a dactyl in the fifth foot or two dactyls
335
+ const real = r.paths.filter((p) => p[4] && (p[4].qs.length === 3 || p.slice(0, 4).filter((f) => f.qs.length === 3).length >= 2));
336
+ if (!real.length) continue;
337
+ r.paths = real;
338
+ }
339
  const total = r.cost + v.cost + extra;
340
  if (total > MAXCOST) continue;
341
  if (!best || total < best.total - 1e-9) best = { total, v, sylls, meter, r };
 
438
  const HEX = new Set(["iliad", "odyssey", "argonautica", "posthomerica-the-fall-of-troy", "theogony", "works", "shield",
439
  "eclogues", "georgics", "aeneid", "metamorphoses", "pharsalia", "statius-thebaid", "statius-achilleid", "satires",
440
  "juvenal-satires", "the-art-of-poetry-to-the-pisos", "mosella", "cupido-cruciatur", "griphus-ternarii-numeri",
441
+ "precationes", "genethliacon-ad-ausonium-nepotem", "ausonius-epistularum",
442
+ // added after a survey (scripts/scan_rate.js) and a check of each work's known metre
443
+ "psychomachia", "apotheosis", "hamartigenia", "contra-symmachum", "dittochaeon", "ordo-urbium-nobilium",
444
+ "versus-paschales-pro-augusto-dicti", "oratio-consulis-ausonii-versibus-rhopali"]);
445
  const ELEG = new Set(["amours", "epistles", "art-of-love", "art-of-beauty", "remedy-of-love", "epicedion-in-patrem",
446
  "de-bello-gildonico", "de-bello-gothico", "de-consulatu-stilichonis", "in-eutropium", "in-rufinum", "de-raptu-proserpinae",
447
+ "epithalamium-de-nuptiis-honorii-augusti", "epigrammata-ausonii-de-diversis-rebus", "de-herediolo"]);
448
  function metersFor(id, genre) {
449
+ if (genre === "Tragedy" || id === "christus-patiens-dub") return ["trim"];
450
  if (genre === "Comedy" && /^(clouds|birds)$/.test(id)) return ["trimc", "anap"];
451
  if (ELEG.has(id) || /^panegyricus-/.test(id)) return ["hex", "pent"];
452
  if (HEX.has(id) || /^hymn-\d+-/.test(id)) return ["hex"];
453
  return null;
454
  }
455
 
456
+ // lines of a listed work that are in another metre: Prudentius' prefaces (numbered "pr.…") are iambic or lyric,
457
+ // and the second preface of the Apotheosis is iambic
458
+ function skipLine(id, book, ref) {
459
+ if (/^(psychomachia|hamartigenia|contra-symmachum)$/.test(id) && /^pr/.test(String(ref))) return true;
460
+ if (id === "apotheosis" && String(book) === "2") return true;
461
+ return false;
462
+ }
463
+
464
+ root.Scan = { scan, metersFor, skipLine };
465
  if (typeof module !== "undefined") module.exports = root.Scan;
466
  })(typeof window !== "undefined" ? window : globalThis);
style.css CHANGED
@@ -425,3 +425,4 @@ a.q-open { cursor: pointer; }
425
  #text .sec:focus-visible { outline: 2px solid var(--maroon); outline-offset: 2px; }
426
  #text .w:focus-visible { outline: 2px solid var(--maroon); border-radius: 2px; }
427
  #text .sec:focus:not(:focus-visible) { outline: none; }
 
 
425
  #text .sec:focus-visible { outline: 2px solid var(--maroon); outline-offset: 2px; }
426
  #text .w:focus-visible { outline: 2px solid var(--maroon); border-radius: 2px; }
427
  #text .sec:focus:not(:focus-visible) { outline: none; }
428
+ .idea mark.best { background: #fbeec4; color: inherit; padding: 0 1px; border-radius: 2px; }