// Offline reproducible projection of the frozen experiment databases. No glosses // are shipped: these indexes identify word units, inflections and frequencies. const fs = require("fs"), path = require("path"), crypto = require("crypto"); const zlib = require("zlib"), Database = require("better-sqlite3"); const source = process.argv[2] || "local/benchmarks/dictionary/data/lookup.sqlite3"; const frequency = process.argv[3] || "local/benchmarks/selection-reading/data/frequency.sqlite3"; const out = "public/vocabulary-data/v1"; fs.mkdirSync(out, { recursive: true }); const sha = (f) => crypto.createHash("sha256").update(fs.readFileSync(f)).digest("hex"); const manifest = { version: 1, sources: { dictionary: sha(source), frequency: sha(frequency) }, files: {}, }; for (const kind of ["lexicon", "forms", "frequency"]) { const file = path.join(out, kind + ".sqlite3"); if (fs.existsSync(file)) fs.unlinkSync(file); const d = new Database(file); d.prepare("ATTACH DATABASE ? AS original").run(path.resolve(source)); d.prepare("ATTACH DATABASE ? AS corpus").run(path.resolve(frequency)); if (kind === "lexicon") { d.exec( "CREATE TABLE entries(lang TEXT, key TEXT, lemma TEXT, PRIMARY KEY(lang,key)) WITHOUT ROWID", ); // Preserve the prototype lookup's first row for each key. Same indexed // source order; one lemma only, no definitions or translations. const q = d.prepare( "SELECT lemma FROM original.entries WHERE src=? AND dst=? AND key=? LIMIT 1", ); const put = d.prepare("INSERT INTO entries VALUES (?,?,?)"); d.transaction(() => { for (const [lang, dst] of [ ["en", "es"], ["es", "en"], ["ja", "en"], ]) { for (const { key } of d .prepare( "SELECT DISTINCT key FROM original.entries WHERE src=? AND dst=? ORDER BY key", ) .all(lang, dst)) put.run(lang, key, q.get(lang, dst, key).lemma); } })(); } else if (kind === "forms") { d.exec( "CREATE TABLE forms(lang TEXT, form TEXT, lemma TEXT); INSERT INTO forms SELECT * FROM original.forms; CREATE INDEX lookup ON forms(lang,form)", ); } else { d.exec( "CREATE TABLE frequency(lang TEXT, word TEXT, zipf REAL, PRIMARY KEY(lang,word)) WITHOUT ROWID; INSERT INTO frequency SELECT * FROM corpus.frequency", ); } d.exec("VACUUM"); d.close(); const bytes = fs.readFileSync(file), packed = zlib.gzipSync(bytes, { level: 9 }); fs.writeFileSync(file + ".gz", packed); manifest.files[kind] = { bytes: bytes.length, sha256: sha(file), gzipSha256: sha(file + ".gz"), compressedBytes: packed.length, }; fs.unlinkSync(file); } fs.writeFileSync( path.join(out, "manifest.json"), JSON.stringify(manifest, null, 2) + "\n", ); console.log(manifest);