Last active
January 16, 2022 10:48
-
-
Save metanomial/991da7d7a5a2e3ba288cbf235a2b995d to your computer and use it in GitHub Desktop.
Toki Pona word generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Caution: combinatorial explosion will eat your harddrive | |
const MAX_SYLLABLE_COUNT = 4; | |
const OUTPUT_FILE = `${MAX_SYLLABLE_COUNT}-syllables.txt`; | |
// deno-fmt-ignore | |
const syllables = [ | |
"a", "e", "i", "o", "u", | |
"ja", "je", "jo", "ju", "ka", | |
"ke", "ki", "ko", "ku", "la", | |
"le", "li", "lo", "lu", "ma", | |
"me", "mi", "mo", "mu", "na", | |
"ne", "ni", "nja", "nje", "njo", | |
"nju", "nka", "nke", "nki", "nko", | |
"nku", "nla", "nle", "nli", "nlo", | |
"nlu", "no", "npa", "npe", "npi", | |
"npo", "npu", "nsa", "nse", "nsi", | |
"nso", "nsu", "nta", "nte", "nto", | |
"ntu", "nu", "nwa", "nwe", "nwi", | |
"pa", "pe", "pi", "po", "pu", | |
"sa", "se", "si", "so", "su", | |
"ta", "te", "to", "tu", "wa", | |
"we", "wi", | |
]; | |
const encoder = new TextEncoder(); | |
const output = await Deno.open(OUTPUT_FILE, { | |
create: true, | |
write: true, | |
truncate: true, | |
}); | |
void function generateWords(sequence: string[], lengthLeft: number) { | |
// write to file | |
if (sequence.length) { | |
const word = sequence.join(""); | |
output.writeSync( | |
encoder.encode(`${word}\n${word}n\n`), | |
); | |
} | |
if (lengthLeft == 0) return; | |
// generate longer words | |
for (let i = sequence.length ? 5 : 0; i < syllables.length; ++i) { | |
// don't start words with ncv | |
if (!sequence.length && i > 26 && i < 60 && i != 41 && i != 56) continue; | |
generateWords(sequence.concat(syllables[i]), lengthLeft - 1); | |
} | |
}([], MAX_SYLLABLE_COUNT); | |
output.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment