pashto-grammar/scripts/get-words.js

67 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-03-06 10:40:31 +00:00
const fs = require("fs");
const fetch = require("node-fetch");
2021-03-16 10:02:53 +00:00
const { readDictionary } = require("@lingdocs/pashto-inflector");
2021-03-06 10:40:31 +00:00
const path = require("path");
const verbsPath = path.join(".", "src", "words");
const verbCollectionPath = path.join(verbsPath, "verb-categories");
const nounAdjCollectionPath = path.join(verbsPath, "noun-adj-categories");
const verbTsFiles = fs.readdirSync(verbCollectionPath);
const nounAdjTsFiles = fs.readdirSync(nounAdjCollectionPath);
const allVerbTsS = [...new Set(verbTsFiles.reduce((arr, fileName) => {
const TsS = require(path.join("..", verbCollectionPath, fileName));
return [...arr, ...TsS];
}, []))];
const allNounAdjTsS = [...new Set(nounAdjTsFiles.reduce((arr, fileName) => {
const TsS = require(path.join("..", nounAdjCollectionPath, fileName));
return [...arr, ...TsS.map(x => ({ ...x, category: path.parse(fileName).name }))];
}, []))];
console.log("getting words from dictionary...");
2021-03-16 10:02:53 +00:00
fetch(process.env.LINGDOCS_DICTIONARY_URL).then(res => res.arrayBuffer()).then(buffer => {
const dictionary = readDictionary(buffer);
const entries = dictionary.entries;
2021-03-06 10:40:31 +00:00
// MAKE VERBS FILE
const allVerbs = getVerbsFromTsS(entries);
const content = `const verbs = ${JSON.stringify(allVerbs)};
export default verbs;`;
fs.writeFileSync(path.join(verbsPath, "verbs.js"), content);
// MAKE NOUN-ADJ FILE
const allNounsAdjs = getNounsAdjsFromTsS(entries);
const content1 = `const nounsAdjs = ${JSON.stringify(allNounsAdjs)};
export default nounsAdjs;`;
fs.writeFileSync(path.join(verbsPath, "nouns-adjs.js"), content1);
});
function getVerbsFromTsS(entries) {
return allVerbTsS.map(item => {
const entry = entries.find(x => item.ts === x.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, def: item.e };
}).filter(x => x);
}
function getNounsAdjsFromTsS(entries) {
return allNounAdjTsS.map(item => {
const entry = entries.find(x => item.ts === x.ts);
if (!entry) {
console.log("couldn't find ts", ts);
return undefined;
}
return { entry, def: item.e, category: item.category };
}).filter(x => x);
}