(de)(pronouns) add backwards compatibility for compressed format (can be later used for simpler migration)

This commit is contained in:
Valentyne Stigloher 2024-04-26 13:51:44 +02:00
parent 604c6ed3c1
commit 2d44aec739
3 changed files with 36 additions and 5 deletions

View File

@ -181,9 +181,9 @@ export const buildPronoun = (
const pronounsWithAliases = addAliasesToPronouns(pronouns);
const pronounStr = path.split(',');
let pronounStr = path.split(',');
let base = null;
let base: Pronoun | null | undefined = null;
for (const option of pronounStr[0].split('&')) {
if (!base) {
base = pronounsWithAliases[option];
@ -194,7 +194,38 @@ export const buildPronoun = (
let baseArray = base ? base.toArray() : null;
// i know, it's ugly… didn't think about BC much and now it's a huge mess…
const pronounStrLen = pronounStr.map((x) => x.startsWith('!') ? parseInt(x.substring(1)) : 1).reduce((c, a) => c + a, 0);
if (config.locale === 'pl' && baseArray && pronounStrLen < 31) {
if (config.locale === 'de') {
// only migrate the four original morphemes as the generator has not supported more morphemes
const oldMorphemeVersions = [
['pronoun_n', 'pronoun_d', 'pronoun_a', 'possessive_determiner_m_n'],
['pronoun_n', 'pronoun_d', 'pronoun_a', 5, 'possessive_determiner_m_n', 15]
.flatMap((morphemeOrIgnoredCount) => {
if (typeof morphemeOrIgnoredCount === 'string') {
return [morphemeOrIgnoredCount];
}
return new Array(morphemeOrIgnoredCount).fill(null);
}),
];
for (const oldMorphemeVersion of oldMorphemeVersions) {
if (pronounStrLen === oldMorphemeVersion.length + 2) {
const baseArrayWithDowngradedMorphemes = oldMorphemeVersion.map((morpheme) => {
if (morpheme === null || !base) {
return null;
}
return base.morphemes[morpheme];
}).concat(baseArray ? baseArray.slice(baseArray.length - 2) : ['0', '']);
const uncompressed = Compressor.uncompress(pronounStr, baseArrayWithDowngradedMorphemes, config.locale);
pronounStr = MORPHEMES.map((morpheme) => {
const index = oldMorphemeVersion.indexOf(morpheme);
if (index >= 0) {
return uncompressed[index];
}
return null;
}).concat(uncompressed.slice(uncompressed.length - 2));
break;
}
}
} else if (config.locale === 'pl' && baseArray && pronounStrLen < 31) {
baseArray.splice(baseArray.length - 10, 1);
if (pronounStrLen < 30) {
baseArray = [

View File

@ -662,7 +662,7 @@ export class Pronoun {
if (data.length !== MORPHEMES.length + extraFields ||
data[0].length === 0 ||
data[data.length - 1].length > Pronoun.DESCRIPTION_MAXLENGTH ||
data.slice(1, data.length - extraFields).filter((s) => s.length > 24).length
data.slice(1, data.length - extraFields).filter((s) => s?.length > 24).length
) {
return null;
}

View File

@ -38,7 +38,7 @@ export default class Compressor {
const uncompressed = [];
let i = 0;
for (const piece of data) {
const m = piece.match(/^!(\d+)$/);
const m = piece?.match(/^!(\d+)$/);
if (!m) {
uncompressed.push(piece);
i++;