I am writing a program to combine the basic form of a word with suffix arrays following the choices from a form group (Angular 9) to receive the declination.
This is the flow:
- The user inputs a word.
- The user chooses the predefined grammar categories of the word (genus, class, animacy and others).
- According to the choices of the user the input is mapped with an array of the corresponding suffixes.
Now, my problem is that I can not wrap my head around the problem to find a more dynamic solution than writing an if-statement for every possible combination of choices and the corresponding array of suffixes.
Right now I compare the values array from the user choices with every predefined combination array. You will see in a second that this solution leads to a lot of repetitive code and I ask you to help me to find a better solution.
Two (of many possible) predefined conditions of choices plus the fitting suffix arrays
conditionOne = ['inanimate', 'm', '2', 'hard'];
conditionOneSuffixes = ['', 'а', 'у', 'а', 'ом', 'е', 'ы', 'ов', 'ам', 'ов', 'ами', 'ах'];
conditionTwo = ['animate', 'm', '2', 'hard'];
conditionTwoSuffixes = ['', 'а', 'у', 'а', 'ом', 'е', 'ы', 'ов', 'ам', 'ов', 'ами', 'ах'];
My first function to save the choices as values from angular reactive forms
setValues($event: string) {
this.values = [];
this.values = Object.values(this.substantiveFormGroup.value); // the user choices from the form group saved to an array
this.setDeclination(this.values);
}
My second function to map the fitting suffix array on the input (only two possible if statements)
setDeclination(values) {
if (
this.conditionOne.every(i => values // the predefinded array of choices
.includes(i))) {
this.declination = this.conditionOneSuffixes.map( // the corresponding suffix array
suffixes => this.substantiveFormGroup.get("input").value + suffixes
);
}
else if (
this.conditionTwo.every(i => values // another predefinded array of choices
.includes(i))) {
this.declination = this.conditionTwoSuffixes.map( // the corresponding suffix array
suffixes => this.substantiveFormGroup.get("input").value + suffixes
);
}
// else if (another condition) { and so on...}
else this.declination = [];
}
What would be a better, less repetitive solution?
Thank you for your time.