(nouns) filter example sentences missing required morphemes or declensions

This commit is contained in:
Valentyne Stigloher 2025-04-13 22:17:47 +02:00
parent 5ecb113ddc
commit 5aa83aa1f0
10 changed files with 23 additions and 22 deletions

View File

@ -24,18 +24,13 @@ const example = computed(() => {
const pronoun = ref<Pronoun | undefined>();
const exampleValues = computed(() => {
if (pronoun.value === undefined) {
return undefined;
}
return { morphemeValues: pronoun.value.toMorphemeValues(props.counter) };
});
const exampleValues = computed(() => pronoun.value?.toExampleValues(props.counter));
const suitablePronounExamples = computed((): PronounExample[] => {
return props.exampleCategory.examples.filter((pronounExample) => {
return props.pronounsChoice.some((pronoun) => {
const example = pronounExample.example(pronoun, props.counter);
return example.requiredMorphemesPresent(pronoun.toMorphemeValues(props.counter));
return example.areRequiredExampleValuesPresent(pronoun.toExampleValues(props.counter));
});
});
});
@ -64,7 +59,7 @@ const selectDifferentExample = (): void => {
const selectPronounForExample = (forceDifferent: boolean): void => {
const suitablePronouns = props.pronounsChoice.filter((otherPronoun) => {
return example.value.requiredMorphemesPresent(otherPronoun.toMorphemeValues()) &&
return example.value.areRequiredExampleValuesPresent(otherPronoun.toExampleValues()) &&
(!forceDifferent || otherPronoun !== toRaw(pronoun.value));
});
pronoun.value = randomItem(suitablePronouns);

View File

@ -30,7 +30,7 @@ const pronunciation = computed(() => {
const visibleExamples = computed(() => {
return props.examples?.filter((example) => {
return example.requiredMorphemesPresent(props.exampleValues.morphemeValues) &&
return example.areRequiredExampleValuesPresent(props.exampleValues) &&
[...props.highlightsMorphemes.values()].some((morpheme) => example.hasMorpheme(morpheme));
});
});

View File

@ -33,7 +33,7 @@ const examples = computed((): Example[] => {
<template>
<MorphemeWithPronunciation
:morpheme
:example-values="{ morphemeValues: pronoun.toMorphemeValues(counter) }"
:example-values="pronoun.toExampleValues(counter)"
:highlights-morphemes
:examples
/>

View File

@ -193,7 +193,7 @@ const grammarTables = computed((): GrammarTableDefinition[] => {
return props.comprehensive ? comprehensiveGrammarTables : simpleGrammarTables;
});
const exampleValues = computed(() => ({ morphemeValues: props.selectedPronoun.toMorphemeValues(props.counter) }));
const exampleValues = computed(() => props.selectedPronoun.toExampleValues(props.counter));
const pronounExamples = await loadPronounExamples();

View File

@ -203,7 +203,7 @@ const grammarTables: GrammarTableDefinition[] = [
},
];
const exampleValues = computed(() => ({ morphemeValues: props.selectedPronoun.toMorphemeValues(props.counter) }));
const exampleValues = computed(() => props.selectedPronoun.toExampleValues(props.counter));
const pronounExamples = await loadPronounExamples();

View File

@ -38,7 +38,11 @@ const exampleValues = computed(() => {
});
const examples = computed(() => {
return nounConventions?.examples.map((example) => Example.parse(example));
if (exampleValues.value === undefined) {
return undefined;
}
return nounConventions?.examples.map((example) => Example.parse(example))
.filter((example) => example.areRequiredExampleValuesPresent(exampleValues.value!));
});
</script>

View File

@ -36,8 +36,7 @@ export const getPronounExamples = async (event: H3Event): Promise<PronounExample
export const addExamples = (pronoun: Pronoun, examples: PronounExamplesData[]): string[] => {
return buildList(function* () {
for (const example of examples) {
const exampleValues = { morphemeValues: pronoun.toMorphemeValues() };
yield buildExample(example).example(pronoun).toSpellingString(exampleValues);
yield buildExample(example).example(pronoun).toSpellingString(pronoun.toExampleValues());
}
});
};

View File

@ -4,7 +4,7 @@ import { buildDict, buildList, escapeControlSymbols } from './helpers.ts';
import type { Translator } from './translator.ts';
import type { NounTemplatesData } from '~/locale/data.ts';
import type { Example } from '~/src/language/examples.ts';
import type { Example, ExampleValues } from '~/src/language/examples.ts';
import { getBaseMorpheme, MorphemeValues } from '~/src/language/morphemes.ts';
import { gendersWithNumerus } from '~/src/nouns.ts';
@ -507,6 +507,10 @@ export class Pronoun {
})));
}
toExampleValues(counter = 0): ExampleValues {
return { morphemeValues: this.toMorphemeValues(counter) };
}
isInterchangable(morpheme: string): boolean {
return (this.morphemes[getBaseMorpheme(morpheme)] || '').includes('&');
}

View File

@ -85,9 +85,8 @@ export class Example {
.some((part) => part.morpheme === morpheme);
}
requiredMorphemesPresent(morphemeValues: MorphemeValues): boolean {
return this.parts.filter((part) => typeof part !== 'string' && part.type === 'morpheme')
.every((part) => morphemeValues.getSpelling(part.morpheme) !== undefined);
areRequiredExampleValuesPresent(exampleValues: ExampleValues): boolean {
return this.parts.every((part, index) => this.getSpelling(index, exampleValues) !== undefined);
}
getSpelling(index: number, exampleValues: ExampleValues): string | undefined {

View File

@ -53,17 +53,17 @@ describe('morphemes of examples', () => {
describe('required morphemes for an example', () => {
test('are present if all morphemes are present', () => {
expect(inlineMorphemeExample.requiredMorphemesPresent(pronouns.they.toMorphemeValues()))
expect(inlineMorphemeExample.areRequiredExampleValuesPresent(pronouns.they.toExampleValues()))
.toBe(true);
});
test('are present even if one morpheme is empty', () => {
const pronoun = generatedPronouns.aerWithEmptyPossessivePronoun(config);
expect(inlineMorphemeExample.requiredMorphemesPresent(pronoun.toMorphemeValues()))
expect(inlineMorphemeExample.areRequiredExampleValuesPresent(pronoun.toExampleValues()))
.toBe(true);
});
test('are missing if one morpheme is null', () => {
const pronoun = generatedPronouns.aerWithUnsetPossessivePronoun(config);
expect(inlineMorphemeExample.requiredMorphemesPresent(pronoun.toMorphemeValues()))
expect(inlineMorphemeExample.areRequiredExampleValuesPresent(pronoun.toExampleValues()))
.toBe(false);
});
});