pashto-grammar/scripts/misc-query.js

91 lines
1.7 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-04-17 10:58:49 +00:00
const { readDictionary } = require("@lingdocs/pashto-inflector");
2021-03-06 10:40:31 +00:00
const verbsPath = path.join(".", "src", "words");
const pChars = [
"آ",
'ځ',
'څ',
'ښ',
'ئ',
'ي',
'پ',
'ټ',
'ڼ',
'ظ',
'ط',
'ژ',
'ډ',
'ض',
'ص',
'ث',
'ق',
'ف',
'غ',
'ع',
'ه',
'خ',
'ح',
'ج',
'چ',
'ش',
'س',
'ی',
'ب',
'ل',
'ا',
'ت',
'ن',
'م',
'ک',
'ګ',
'ۍ',
'ې',
'ز',
'ر',
'ذ',
'د',
'ړ',
'و',
'ږ',
];
2021-03-06 10:40:31 +00:00
2021-04-17 10:58:49 +00:00
fetch(process.env.LINGDOCS_DICTIONARY_URL).then(res => res.arrayBuffer()).then(data => {
const { entries } = readDictionary(data);
const filtered = shuffle(entries.filter(e => (
e.p.length === 5 && e.p.split("").every((char) => pChars.includes(char))
)));
// const content = `module.exports = [
// ${filtered.reduce((text, entry) => (
// text + `{ ts: ${entry.ts}, e: \`${entry.e.replace(/"/g, '\\"')}\` }, // ${entry.p} - ${entry.f}
// `), "")}
// ];`;
const content = `export const WORDS = [
${filtered.map((entry) => (entry.p.replace("آ", "ا"))).reduce((text, p) => (
text + `"${p}",\n`
), "")}
2021-03-06 10:40:31 +00:00
];`;
fs.writeFileSync(path.join(verbsPath, "wordle-words.ts"), content);
2021-03-06 10:40:31 +00:00
});
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}