Skip to content

Commit ed49d47

Browse files
committed
impr(polyglot): Add new structure (wordsMap) to PolyglotWordset to get words from any language by uniform distribution (@IliyaZinoviev)
1 parent 9a76dbd commit ed49d47

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,58 @@ class PseudolangWordGenerator extends Wordset {
146146
}
147147

148148
export class PolyglotWordset extends Wordset {
149-
public wordsWithLanguage: Map<string, Language>;
150-
public languageProperties: Map<Language, JSONData.LanguageProperties>;
149+
readonly wordsMap: Map<Language, Wordset>;
150+
readonly languageProperties: Map<Language, JSONData.LanguageProperties>;
151+
private currLang: Language;
152+
readonly langs: Language[];
151153

152154
constructor(
153-
wordsWithLanguage: Map<string, Language>,
155+
wordsMap: Map<Language, Wordset>,
154156
languageProperties: Map<Language, JSONData.LanguageProperties>,
155157
) {
156-
// build and shuffle the word array
157-
const wordArray = Array.from(wordsWithLanguage.keys());
158-
Arrays.shuffle(wordArray);
159-
super(wordArray);
160-
this.wordsWithLanguage = wordsWithLanguage;
158+
super([]);
161159
this.languageProperties = languageProperties;
160+
this.langs = Array.from(languageProperties.keys());
161+
this.wordsMap = wordsMap;
162+
this.resetIndexes();
163+
this.length = Array.from(this.wordsMap.values()).reduce(
164+
(sum, ws) => sum + ws.words.length,
165+
0,
166+
);
167+
this.currLang = this.langs[0] as Language;
168+
}
169+
170+
get currentLanguage(): Language {
171+
return this.currLang;
172+
}
173+
174+
override resetIndexes(): void {
175+
this.wordsMap.forEach((ws, _) => {
176+
ws.resetIndexes();
177+
});
178+
}
179+
180+
private uniformLang(): Language {
181+
const index = Math.floor(Math.random() * this.langs.length);
182+
this.currLang = this.langs[index] as Language;
183+
return this.currLang;
184+
}
185+
186+
private getWordset(): Wordset {
187+
const lang = this.uniformLang();
188+
return this.wordsMap.get(lang) as Wordset;
189+
}
190+
191+
override randomWord(mode: FunboxWordsFrequency): string {
192+
return this.getWordset().randomWord(mode);
193+
}
194+
195+
override shuffledWord(): string {
196+
return this.getWordset().shuffledWord();
197+
}
198+
199+
override nextWord(): string {
200+
return this.getWordset().nextWord();
162201
}
163202
}
164203

@@ -764,13 +803,10 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
764803
]),
765804
);
766805

767-
const wordsWithLanguage = new Map(
768-
languages.flatMap((lang) =>
769-
lang.words.map((word) => [word, lang.name]),
770-
),
806+
const wordsMap: Map<Language, Wordset> = new Map(
807+
languages.map((lang) => [lang.name, new Wordset(lang.words)]),
771808
);
772-
773-
return new PolyglotWordset(wordsWithLanguage, languageProperties);
809+
return new PolyglotWordset(wordsMap, languageProperties);
774810
},
775811
},
776812
};

frontend/src/ts/test/words-generator.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ async function applyBritishEnglishToWord(
388388
function applyLazyModeToWord(word: string, language: LanguageObject): string {
389389
// polyglot mode, use the word's actual language
390390
if (currentWordset && currentWordset instanceof PolyglotWordset) {
391-
const langName = currentWordset.wordsWithLanguage.get(word);
391+
const langName = currentWordset.currentLanguage;
392392
const langProps = langName
393393
? currentWordset.languageProperties.get(langName)
394394
: undefined;
@@ -508,9 +508,8 @@ async function getQuoteWordList(
508508
// because it will be reversed again in the generateWords function
509509
if (wordOrder === "reverse") {
510510
return currentWordset.words.reverse();
511-
} else {
512-
return currentWordset.words;
513511
}
512+
return currentWordset.words;
514513
}
515514
const languageToGet = language.name.startsWith("swiss_german")
516515
? "german"
@@ -655,17 +654,13 @@ export async function generateWords(
655654

656655
const funbox = findSingleActiveFunboxWithFunction("withWords");
657656
if (funbox) {
658-
const result = await funbox.functions.withWords(wordList);
657+
currentWordset = await funbox.functions.withWords(wordList);
659658
// PolyglotWordset if polyglot otherwise Wordset
660-
if (result instanceof PolyglotWordset) {
661-
const polyglotResult = result;
662-
currentWordset = polyglotResult;
659+
if (currentWordset instanceof PolyglotWordset) {
663660
// set allLigatures if any language in languageProperties has ligatures true
664661
ret.allLigatures = Array.from(
665-
polyglotResult.languageProperties.values(),
662+
currentWordset.languageProperties.values(),
666663
).some((props) => !!props.ligatures);
667-
} else {
668-
currentWordset = result;
669664
}
670665
} else {
671666
currentWordset = await withWords(wordList);
@@ -901,9 +896,9 @@ export async function getNextWord(
901896

902897
const usingFunboxWithGetWord = isFunboxActiveWithFunction("getWord");
903898
const randomWordLanguage =
904-
(currentWordset instanceof PolyglotWordset
905-
? currentWordset.wordsWithLanguage.get(randomWord)
906-
: Config.language) ?? Config.language; // Fall back to Config language if per-word language is unavailable
899+
currentWordset instanceof PolyglotWordset
900+
? currentWordset.currentLanguage
901+
: Config.language; // Fall back to Config language if per-word language is unavailable
907902

908903
if (
909904
Config.mode !== "custom" &&

0 commit comments

Comments
 (0)