|
| 1 | +import { dictionary as globalDictionary } from "./custom_dictionary.ts"; |
| 2 | +import { Definition, Dictionary } from "./type.ts"; |
| 3 | + |
| 4 | +// all of these global constants are mutable |
| 5 | + |
| 6 | +const customDictionary: Dictionary = new Map(); |
| 7 | +export const dictionary: Dictionary = new Map(); |
| 8 | + |
| 9 | +export const contentWordSet: Set<string> = new Set(); |
| 10 | +export const prepositionSet: Set<string> = new Set(); |
| 11 | +export const preverbSet: Set<string> = new Set(); |
| 12 | +export const fillerSet: Set<string> = new Set(); |
| 13 | +export const numeralSet: Set<string> = new Set(); |
| 14 | +export const tokiPonaWordSet: Set<string> = new Set(); |
| 15 | + |
| 16 | +update(); |
| 17 | + |
| 18 | +export function loadCustomDictionary(dictionary: Dictionary): void { |
| 19 | + customDictionary.clear(); |
| 20 | + for (const [key, value] of dictionary) { |
| 21 | + customDictionary.set(key, value); |
| 22 | + } |
| 23 | + update(); |
| 24 | +} |
| 25 | +function update() { |
| 26 | + dictionary.clear(); |
| 27 | + const words = new Set([ |
| 28 | + ...globalDictionary.keys(), |
| 29 | + ...customDictionary.keys(), |
| 30 | + ]); |
| 31 | + for (const word of words) { |
| 32 | + const entry = customDictionary.get(word) ?? globalDictionary.get(word)!; |
| 33 | + if (entry.definitions.length > 0) { |
| 34 | + dictionary.set(word, entry); |
| 35 | + } |
| 36 | + } |
| 37 | + redefineSet( |
| 38 | + contentWordSet, |
| 39 | + ({ type }) => !["filler", "particle definition"].includes(type), |
| 40 | + ); |
| 41 | + redefineSetWithType(prepositionSet, "preposition"); |
| 42 | + redefineSet( |
| 43 | + preverbSet, |
| 44 | + (definition) => |
| 45 | + (definition.type === "verb" && definition.predicateType != null) || |
| 46 | + definition.type === "modal verb", |
| 47 | + ); |
| 48 | + redefineSetWithType(fillerSet, "filler"); |
| 49 | + redefineSetWithType(numeralSet, "numeral"); |
| 50 | + redefineSet(tokiPonaWordSet, () => true); |
| 51 | +} |
| 52 | +function redefineSet( |
| 53 | + set: Set<string>, |
| 54 | + filter: (definition: Definition) => boolean, |
| 55 | +) { |
| 56 | + set.clear(); |
| 57 | + for (const [word, { definitions }] of dictionary) { |
| 58 | + if (definitions.some(filter)) { |
| 59 | + set.add(word); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +function redefineSetWithType(set: Set<string>, type: Definition["type"]) { |
| 64 | + redefineSet(set, ({ type: compareType }) => compareType === type); |
| 65 | +} |
0 commit comments