nicer get-words using new ts lookup api

This commit is contained in:
adueck 2022-11-02 10:46:04 +05:00
parent f4165b1e76
commit 09d137a50b
1 changed files with 19 additions and 56 deletions

View File

@ -12,8 +12,6 @@ const verbCollectionPath = path.join(".", "vocab", "verbs");
const nounAdjCollectionPath = path.join(".", "vocab", "nouns-adjs");
const verbTsFiles = fs.readdirSync(verbCollectionPath);
const nounAdjTsFiles = fs.readdirSync(nounAdjCollectionPath);
const protoModels = require("./src/lib/src/dictionary-models.js");
const Pbf = require("pbf");
const allVerbTsS = [...new Set(verbTsFiles.reduce((arr, fileName) => {
const TsS = require("./vocab/verbs/"+fileName);
@ -25,11 +23,18 @@ const allNounAdjTsS = [...new Set(nounAdjTsFiles.reduce((arr, fileName) => {
return [...arr, ...TsS];
}, []))];
fetch(process.env.LINGDOCS_DICTIONARY_URL).then(res => res.arrayBuffer()).then(buffer => {
const pbf = new Pbf(buffer);
const dictionary = protoModels.Dictionary.read(pbf);
const entries = dictionary.entries;
const allVerbs = getVerbsFromTsS(entries, allVerbTsS);
fetch("https://account.lingdocs.com/dictionary/entry", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ ids: [...allNounAdjTsS, ...allVerbTsS] }),
}).then(res => res.json()).then(res => {
const verbs = res.results.filter(x => "entry" in x);
const missingEc = [];
verbs.forEach(v => {
if (!v.entry.ec) missingEc.push(v);
});
const verbsContent = `
/**
* Copyright (c) 2021 lingdocs.com
@ -44,10 +49,10 @@ import { DictionaryEntry, VerbEntry } from "./types";
const verbs: {
entry: DictionaryEntry,
complement?: DictionaryEntry,
}[] = ${JSON.stringify(allVerbs)};
}[] = ${JSON.stringify(verbs)};
export default verbs as VerbEntry[];`;
fs.writeFileSync("./src/verbs.ts", verbsContent);
const allNounsAdjs = getNounsAdjsFromTsS(entries, allNounAdjTsS);
const nounsAdjs = res.results.filter(x => !("entry" in x));
const nounsAdjsContent = `
/**
* Copyright (c) 2021 lingdocs.com
@ -59,55 +64,13 @@ export default verbs as VerbEntry[];`;
import { DictionaryEntry } from "./types";
const nounsAdjs: DictionaryEntry[] = ${JSON.stringify(allNounsAdjs)};
const nounsAdjs: DictionaryEntry[] = ${JSON.stringify(nounsAdjs)};
export default nounsAdjs;`;
fs.writeFileSync("./src/nouns-adjs.ts", nounsAdjsContent);
console.log("fetched words from dictionary");
if (missingEc.length > 0) {
console.log("Missing ec's in verbs:");
console.log(missingEc);
}
});
function getNounsAdjsFromTsS(entries, tss) {
const missingEc = [];
const toReturn = tss.map(ts => {
const entry = entries.find(x => ts === x.ts);
if (!entry) {
console.log("couldn't find ts", ts);
return undefined;
}
if (ts.ec) {
missingEc.push(ts);
}
return entry;
}).filter(x => x);
if (missingEc.length !== 0) {
console.log("missingEc", missingEc);
}
return toReturn;
}
function getVerbsFromTsS(entries, tss) {
const missingEc = [];
const toReturn = tss.map(ts => {
const entry = entries.find(x => ts === x.ts);
if (!entry.ec) {
missingEc.push(entry.ts);
}
if (!entry) {
console.log("couldn't find ts", ts);
return undefined;
}
if (entry.c && entry.c.includes("comp.")) {
const complement = entries.find(x => entry.l === x.ts);
return {
entry,
complement,
};
}
return { entry };
}).filter(x => x);
if (missingEc.length !== 0) {
console.log("missingEc", missingEc);
}
return toReturn;
}