pashto-grammar/scripts/get-words.js

53 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-03-06 10:40:31 +00:00
const fs = require("fs");
const fetch = require("node-fetch");
const path = require("path");
2021-10-18 01:12:47 +00:00
const wordsPath = path.join(".", "src", "words");
const wordsFile = "raw-words.ts";
2021-10-18 01:12:47 +00:00
const verbCollectionPath = path.join(wordsPath, "verb-categories");
const nounAdjCollectionPath = path.join(wordsPath, "noun-adj-categories");
const adverbCollectionPath = path.join(wordsPath, "adverbs");
2021-03-06 10:40:31 +00:00
const verbTsFiles = fs.readdirSync(verbCollectionPath);
const nounAdjTsFiles = fs.readdirSync(nounAdjCollectionPath);
const adverbTsFiles = fs.readdirSync(adverbCollectionPath);
2021-03-06 10:40:31 +00:00
2021-10-19 05:17:27 +00:00
const allVerbTsS = verbTsFiles.flatMap(fileName => [
2022-09-06 14:13:55 +00:00
...require(path.join("..", verbCollectionPath, fileName))
2021-10-19 05:17:27 +00:00
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
2021-03-06 10:40:31 +00:00
2021-10-19 05:17:27 +00:00
const allNounAdjTsS = nounAdjTsFiles.flatMap(fileName => [
2022-09-06 14:13:55 +00:00
...require(path.join("..", nounAdjCollectionPath, fileName))
2021-10-19 05:17:27 +00:00
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
2021-03-06 10:40:31 +00:00
const allAdverbTsS = adverbTsFiles.flatMap(fileName => [
2022-09-06 14:13:55 +00:00
...require(path.join("..", adverbCollectionPath, fileName))
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
2022-11-02 06:08:24 +00:00
const allTs = [...allVerbTsS, ...allAdverbTsS, ...allNounAdjTsS];
2021-03-06 10:40:31 +00:00
console.log("getting words from dictionary...");
2022-11-02 06:08:24 +00:00
fetch("https://account.lingdocs.com/dictionary/entries", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ ids: allTs }),
}).then(res => res.json()).then(data => {
2021-10-18 01:12:47 +00:00
const content = `
// @ts-ignore
2022-11-02 06:08:24 +00:00
const words: Word[] = ${JSON.stringify(data.results)};
2021-10-18 01:12:47 +00:00
export default words;`;
fs.writeFileSync(path.join(wordsPath, wordsFile), content);
2022-11-02 06:08:24 +00:00
const missingEc = data.results.filter(x => "entry" in x && !x.entry.ec);
if (missingEc.length) {
console.log("verbs missing ec");
console.log(missingEc);
}
if (data.notFound.length) {
console.log("entries not found:");
console.log(data.notFound);
}
2021-03-06 10:40:31 +00:00
});