// Parses the leaderboard tables of the LocalLLaMA/typed-decisions dataset card (README.md). // Works in the browser (window.TDParse) and in Node (module.exports) so it can be tested offline. (function (root) { 'use strict'; // Sizes the card does not state in a parseable way. const SIZE_OVERRIDES = { 'Jeff-Gemma4-E2B': { total: 2e9, active: null, note: 'E2B, effective size' }, }; const DASH = /^[\s–—-]*$/; function splitRow(line) { let s = line.trim().replace(/\\\|/g, '\u0000'); if (s.startsWith('|')) s = s.slice(1); if (s.endsWith('|')) s = s.slice(0, -1); return s.split('|').map((c) => c.replace(/\u0000/g, '|').trim()); } function findTables(md) { const lines = md.split(/\r?\n/); const tables = []; let heading = ''; for (let i = 0; i < lines.length; i++) { const l = lines[i]; if (/^#{1,6}\s/.test(l)) heading = l.replace(/^#+\s*/, '').trim(); if (l.trim().startsWith('|') && i + 1 < lines.length && /^\s*\|?\s*:?-{2,}/.test(lines[i + 1])) { const header = splitRow(l); const rows = []; i += 2; while (i < lines.length && lines[i].trim().startsWith('|')) rows.push(splitRow(lines[i++])); i--; tables.push({ heading, header, rows }); } } return tables; } const LINK = /\[([^\]]+)\]\(([^)\s]+)\)/g; function links(cell) { return [...cell.matchAll(LINK)].map((m) => ({ text: m[1].replace(/\*\*|`/g, ''), url: m[2] })); } function text(cell) { return cell.replace(LINK, '$1').replace(/\*\*|__/g, '').replace(/`/g, '').replace(/\s+/g, ' ').trim(); } function num(cell) { const t = text(cell).replace(/,/g, ''); if (DASH.test(t)) return null; const m = t.match(/-?\d+(?:\.\d+)?/); return m ? parseFloat(m[0]) : null; } const SIZE_RE = /(? m[1]); for (const c of [...parens, name]) { const m = c.match(SIZE_RE); if (m) { const total = parseFloat(m[1]) * (/[Bb]/.test(m[2]) ? 1e9 : 1e6); return { total, active: m[3] ? parseFloat(m[3]) * 1e9 : null, note: '' }; } } return null; } function parseLatency(cell) { const t = text(cell).replace(/,/g, ''); if (DASH.test(t)) return null; const m = t.match(/(\d+(?:\.\d+)?)\s*(ms|s)\b/); if (!m) return null; const ms = parseFloat(m[1]) * (m[2] === 's' ? 1000 : 1); const note = (t.match(/\(([^)]*)\)/) || [])[1] || ''; return { ms, note, raw: text(cell) }; } function parsePrice(cell) { const t = text(cell); const m = t.match(/\$\s*(\d+(?:\.\d+)?)/); return { value: m ? parseFloat(m[1]) : null, raw: DASH.test(t) ? '' : t }; } function modelName(cell) { const raw = cell.replace(/^\s*\*\*/, ''); const first = links(cell).find((l) => !/^#\d+$/.test(l.text)); if (first && raw.trimStart().startsWith('[' )) return { name: first.text, url: first.url }; const t = text(cell); const cut = t.split(/\s\(|,\s|\svia\s/)[0].trim(); return { name: cut, url: null }; } function colIndex(header) { const find = (re) => header.findIndex((h) => re.test(h)); return { rank: find(/^#$/), model: find(/^model/i), kind: find(/^kind/i), acc: find(/accuracy/i), kl: find(/^kl/i), brier: find(/brier/i), ece: find(/^ece/i), lat: find(/latency/i), price: find(/price/i), }; } function parseRow(cells, ci, table) { const cell = (k) => (ci[k] >= 0 ? cells[ci[k]] || '' : ''); const modelCell = cell('model'); const full = text(modelCell); const { name, url } = modelName(modelCell); const kind = text(cell('kind')); const lat = parseLatency(cell('lat')); const price = parsePrice(cell('price')); const size = parseSize(full, name); const discussions = links(modelCell) .filter((l) => /^#\d+$/.test(l.text)) .map((l) => ({ num: +l.text.slice(1), url: l.url })); const via = (full.match(/\bvia\s+(.+?)(?:\s\(|$)/) || [])[1] || ''; const hfModel = url && /^https:\/\/huggingface\.co\/(?!datasets\/|spaces\/|docs\/)/.test(url); const open = /open weights/i.test(kind + ' ' + price.raw) || !!hfModel || /local/i.test(price.raw) || /\bours\b/.test(full); const apiServed = !open || !!via || /API/i.test(lat ? lat.note : ''); const rankText = text(cell('rank')); return { table, rank: /^\d+$/.test(rankText) ? +rankText : null, name, url, full, kind, via, open, apiServed, params: size ? size.total : null, active: size ? size.active || size.total : null, sizeNote: size ? size.note : '', acc: num(cell('acc')), kl: num(cell('kl')), brier: num(cell('brier')), ece: num(cell('ece')), lat: lat ? lat.ms : null, latNote: lat ? lat.note : '', latRaw: lat ? lat.raw : '', price: price.value, priceRaw: price.raw, discussions, isRef: rankText !== '' && !/^\d+$/.test(rankText) && /reference/i.test(kind), }; } function parseReadme(md) { const tables = findTables(md); const rows = []; const refs = []; for (const t of tables) { const ci = colIndex(t.header); if (ci.model >= 0 && ci.acc >= 0) { const fitted = /fitted|fine-tuned/i.test(t.heading); for (const cells of t.rows) { const r = parseRow(cells, ci, fitted ? 'fitted' : 'zero'); if (r.isRef) refs.push({ name: r.name, acc: r.acc, kl: r.kl, brier: r.brier, ece: r.ece, what: r.full }); else if (r.acc != null) rows.push(r); } } else if (/^reference$/i.test(t.header[0]) && t.header.some((h) => /accuracy/i.test(h))) { const ia = t.header.findIndex((h) => /accuracy/i.test(h)); const iw = t.header.findIndex((h) => /what/i.test(h)); for (const cells of t.rows) { const name = text(cells[0]); if (refs.some((r) => r.name.toLowerCase().startsWith(name.toLowerCase()))) continue; refs.push({ name, acc: num(cells[ia]), what: iw >= 0 ? text(cells[iw]) : '' }); } } } rows.forEach((r, i) => (r.id = `${r.table}-${i}`)); const ceilingMatch = md.match(/Scores well above\s+(\d\.\d+)/i); return { rows, refs, teacherCeiling: ceilingMatch ? parseFloat(ceilingMatch[1]) : null }; } const api = { parseReadme, findTables, parseSize, parseLatency, text, links }; if (typeof module !== 'undefined' && module.exports) module.exports = api; else root.TDParse = api; })(typeof window !== 'undefined' ? window : globalThis);