fix(rewrite): Allow for pronoun form variants

This commit is contained in:
tecc 2023-09-20 02:15:34 +02:00
parent e854a89303
commit 33dd2cbb7d
No known key found for this signature in database
GPG Key ID: 622EEC5BAE5EBD3A

View File

@ -85,6 +85,13 @@ export function isLocaleActive(localeCode: string): boolean {
return getActiveLocaleDescriptions().some((v) => v.code === localeCode);
}
export interface PronounVariant {
written: string;
pronounced: string;
}
export interface PronounForm extends PronounVariant {
variants?: Array<PronounVariant>;
}
export interface Pronoun {
keys: Array<string>;
description?: string;
@ -98,7 +105,7 @@ export interface Pronoun {
thirdForm?: string;
smallForm?: string;
forms: Record<string, { written: string; pronounced: string }>;
forms: Record<string, PronounForm>;
canonicalName: string;
}
@ -113,11 +120,37 @@ export function parsePronounFromParts(
const pronounForms: Pronoun["forms"] = {};
for (let i = 0; i < forms.length; i++) {
const form = parts[i].split("|");
pronounForms[forms[i]] = {
written: form[0],
pronounced: form[1],
const [written, pronounced] = parts[i].split("|");
const writtenVariants = written.split("&");
const pronouncedVariants = pronounced?.split("&") ?? [];
const current: PronounForm = {
written: writtenVariants.shift()!,
pronounced: pronouncedVariants.shift()!,
};
if (writtenVariants.length || pronouncedVariants.length) {
const length = Math.max(
writtenVariants.length,
pronouncedVariants.length
);
const lastWritten = writtenVariants.length - 1,
lastPronounced = pronouncedVariants.length - 1;
const variants: PronounForm["variants"] = [];
for (let i = 0; i < length; i++) {
variants.push({
written:
writtenVariants[Math.max(i, lastWritten)] ??
current.written,
pronounced:
pronouncedVariants[Math.max(i, lastPronounced)] ??
current.pronounced,
});
}
current.variants = variants;
}
pronounForms[forms[i]] = current;
}
return {
@ -341,21 +374,26 @@ export class Locale {
// I make no guarantee regarding the accuracy.
const variants: Set<string> = new Set();
const firstMorpheme =
pronoun.forms[this.morphemes[0]].written.split("&");
function allVariants(form: PronounForm): Array<string> {
return [
form.written,
...(form.variants?.map((v) => v.written) ?? []),
];
}
const firstMorpheme = allVariants(pronoun.forms[this.morphemes[0]]);
if (this.morphemes.length === 1) {
return firstMorpheme;
}
const secondMorpheme =
pronoun.forms[this.morphemes[1]].written.split("&");
const secondMorpheme = allVariants(pronoun.forms[this.morphemes[1]]);
const thirdMorpheme =
this.morphemes.length > 2
? pronoun.forms[this.morphemes[2]].written.split("&")
? allVariants(pronoun.forms[this.morphemes[2]])
: [];
let thirdFormMorpheme = pronoun.thirdForm
? pronoun.forms[pronoun.thirdForm].written.split("&")
? allVariants(pronoun.forms[pronoun.thirdForm])
: [];
let thirdMorphemeThreeForms = thirdMorpheme;