Skip to content

Instantly share code, notes, and snippets.

@fergusq
Created March 16, 2021 17:53
Show Gist options
  • Save fergusq/0cb0d3a9f252540571b3c5346e98ea3c to your computer and use it in GitHub Desktop.
Save fergusq/0cb0d3a9f252540571b3c5346e98ea3c to your computer and use it in GitHub Desktop.
This is a script that can analyze Klingon words using the boQwI' dictionary.
/*
* This is a script that can analyze Klingon words using the boQwI' dictionary.
*
* You need to have dictionary.json available somewhere.
*
* Usage:
*
* const analyzeWord = await initializeDictionary();
* console.log(analyzeWord("Heghlu'meH"))
*/
interface Dictionary {
qawHaq: { [id: string]: DictionaryEntry }
}
interface DictionaryEntry {
entry_name: string
part_of_speech: string
definition: {
de: string
en: string
fi: string
sv: string
}
}
async function initializeDictionary() {
const verbs = []
const stative_verbs = []
const nouns = []
const dictionary_index: { [key: string]: DictionaryEntry[] } = {}
const dictionary = await (await fetch("dictionary.json")).json()
for (const entry of Object.values(dictionary.qawHaq)) {
const word = entry.entry_name
const pos = entry.part_of_speech
if (pos.match(/^v:?/)) {
if (!word.includes(" ") && !pos.match(/pref|suff|deriv/)) verbs.push(word)
dictionary_index[`${word}:v`] = dictionary_index[`${word}:v`] || []
dictionary_index[`${word}:v`].push(entry)
if (pos.startsWith("v:is")) {
stative_verbs.push(word)
}
}
else if (pos.match(/^n:?/)) {
if (!word.includes(" ") && !pos.match(/pref|suff|deriv/)) nouns.push(word)
dictionary_index[`${word}:n`] = dictionary_index[`${word}:n`] || []
dictionary_index[`${word}:n`].push(entry)
}
else {
dictionary_index[`${word}:other`] = dictionary_index[`${word}:other`] || []
dictionary_index[`${word}:other`].push(entry)
}
}
const noun_suffix_regex = "('a'|Hom|(?<=[bDHjqlmnpQrStvwy'hg])oy|(?<![bDHjqlmnpQrStvwy'hg])'oy)?(pu'|Du'|mey)?(qoq|Hey|na')?(wI'|ma'|lI'|ra'|wIj|maj|lIj|raj|Daj|chaj|vam|vetlh)?(Daq|vo'|mo'|vaD|'e')?"
const noun_regex = new RegExp("^(" + nouns.join("|") + ")" + noun_suffix_regex + "$")
const verb_regex = new RegExp("^(|HI|gho|yI|tI|pe|qa|Sa|vI|jI|pI|re|DI|wI|ma|cho|ju|Da|bI|tu|che|bo|Su|mu|nu|Du|lI|nI|lu)("
+ verbs.join("|")
+ ")(Ha')?('egh|chuq)?(be'|qu')?(nIS|qang|rup|beH|vIp)?(be'|qu')?(choH|qa')?(be'|qu')?(moH)?(be'|qu')?(lu'|laH)?(be'|qu')?(chu'|bej|ba'|law')?(be'|qu')?(pu'|ta'|taH|lI')?(be'|qu')?(neS)?(be'|qu'|Qo')?(?:(DI'|chugh|pa'|vIS|mo'|bogh|meH|'a'|jaj)|(?:(wI'|ghach)" + noun_suffix_regex + "))?$")
const stative_verb_regex = new RegExp("^(" + stative_verbs.join("|") + ")(Ha')?(be')?(qu')?(be')?(Daq|vo'|mo'|vaD|'e')$")
return word => {
let m, ans: DictionaryEntry[] = []
if (dictionary_index[word + ":other"]) {
ans = ans.concat(dictionary_index[word + ":other"])
}
if (m = word.match(noun_regex)) {
for (let i = 1; i < m.length; i++) {
let group = m[i]
if (i > 1) group = "-" + group
if (dictionary_index[group + ":n"]) {
ans = ans.concat(dictionary_index[group + ":n"])
}
}
}
if (m = word.match(verb_regex)) {
let pos = ":v"
for (let i = 1; i < m.length; i++) {
let group = m[i]
if (i < 2) group = group + "-"
if (i > 2) group = "-" + group
if (dictionary_index[group + pos]) {
ans = ans.concat(dictionary_index[group + pos])
}
if (group === "-ghach" || group === "-wI'") {
pos = ":n"
}
}
}
if (m = word.match(stative_verb_regex)) {
for (let i = 1; i < m.length; i++) {
let group = m[i]
if (i > 1) group = "-" + group
const pos = group.match(/Daq|vo'|mo'|vaD|'e'/) ? ":n" : ":v"
if (dictionary_index[group + pos]) {
ans = ans.concat(dictionary_index[group + pos])
}
}
}
return ans
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment