const HF = "https://huggingface.co/";
const AUDIO_SPACE = HF + "spaces/danielrosehill/Audio-Dataset-Navigator";
const SENTENCE_SPACE = HF + "spaces/danielrosehill/Sentence-Dataset-Navigator";
const esc = (s) => String(s ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c]));
const ds = (id) => HF + "datasets/" + id;
const link = (url, text) => `${esc(text)}`;
const size = (b) => (b >= 1e9 ? (b / 1e9).toFixed(1) + " GB" : b >= 1e6 ? Math.round(b / 1e6) + " MB" : Math.max(1, Math.round(b / 1e3)) + " KB");
const hours = (h) => (h >= 1 ? h.toFixed(1) + " hours" : Math.max(1, Math.round(h * 60)) + " min");
const short = (id) => id.split("/")[1].replace(/-/g, " ");
function who(d, kind) {
const org = d.owner === "Hebrish";
const text = org
? `Hebrish org · Daniel's own ${kind}`
: `danielrosehill · Daniel's own ${kind}`;
return `
${text} `;
}
function card(d, kind, unit) {
const meta = [];
if (d.rows != null) meta.push(`${d.rows.toLocaleString("en-US")} ${unit}`);
if (d.hours != null) meta.push(hours(d.hours));
meta.push(size(d.bytes));
if ("hours" in d) meta.push("Updated " + d.modified.slice(0, 10));
if (d.gated) meta.push('Access request');
const pairs = (d.banks || []).map((b) => link(ds(b), "Text: " + short(b)));
if (d.pairs_with) pairs.push(link(ds(d.pairs_with), "Audio: " + short(d.pairs_with)));
return `${who(d, kind)}
${link(ds(d.id), d.name)}
${esc(d.summary)}
${meta.join("·")}
${pairs.length ? `${pairs.join("")}
` : ""}`;
}
async function navigator({ kind, unit }) {
const data = await (await fetch("data.json")).json();
const list = data.datasets;
const q = document.getElementById("q"), owner = document.getElementById("owner"), sort = document.getElementById("sort"), out = document.getElementById("list");
document.getElementById("built").textContent = data.built;
const total = list.reduce((n, d) => n + (d.rows || 0), 0);
const master = list.find((d) => d.group === "Master");
const stat = (v, l) => `${v} ${l}`;
document.getElementById("stats").innerHTML =
stat(list.length, "datasets") +
["danielrosehill", "Hebrish"].map((o) => [o, list.filter((d) => d.owner === o).length]).filter(([, n]) => n).map(([o, n]) => stat(n, "on " + o)).join("") +
stat((master ? master.rows : total).toLocaleString("en-US"), master ? "takes in the master" : "sentences") +
(master && master.hours != null ? stat(master.hours.toFixed(1), "hours in the master") : "");
if (new Set(list.map((d) => d.owner)).size < 2) owner.hidden = true;
const draw = () => {
const term = q.value.trim().toLowerCase();
const hit = list.filter((d) => (!owner.value || d.owner === owner.value) && (d.id + " " + d.summary + " " + d.group).toLowerCase().includes(term));
const groups = [...new Set(hit.map((d) => d.group))];
const order = sort && { takes: (a, b) => (b.rows ?? -1) - (a.rows ?? -1), updated: (a, b) => b.modified.localeCompare(a.modified) }[sort.value];
if (order && hit.length) {
out.innerHTML = `${[...hit].sort(order).map((d) => card(d, kind, unit)).join("")}
`;
return;
}
out.innerHTML = groups.map((g) => `${esc(g)}
${hit.filter((d) => d.group === g).map((d) => card(d, kind, unit)).join("")}
`).join("")
|| 'No datasets match.
';
};
q.addEventListener("input", draw);
owner.addEventListener("change", draw);
if (sort) sort.addEventListener("change", draw);
draw();
}
async function thirdParty() {
const data = await (await fetch("data.json")).json();
document.getElementById("built").textContent = data.built;
document.getElementById("collection").href = HF + "collections/" + data.collections.sentences_third_party;
document.getElementById("list").innerHTML = data.third_party.map((d) => `
${esc(d.id.split("/")[0])}
${link(ds(d.id), short(d.id))}
${esc(d.note)}
`).join("");
}