Attention!! It will help a lot to know Polish or any other natural language with strong flexion, preferably with a case system (like German for instance), to answer this question. In particular, Polish declension system is very similar to systems of other Slavic languages like: Russian, Czech, Serbian etc.
Have a look at this Polish, unfinished, declinator: declinator.com I am planning to extend it to other languages, namely Russian and Latin, but for now I am struggling with Polish.
Besides having a large database of declinations for hundreds of nouns I support declining nouns which do not exist. The best solution I came up with until now is simply checking the endings of the nouns so that they can be declined accordingly.
In my code it comes down to this calculateDeclination
method. I call it if the noun is not in the database. The entrails of the method look like this:
if (areLast2Letters(word, "il"))
declinator = new KamilDeclinator(word);
else if (areLast2Letters(word, "sk"))
declinator = new DyskDeclinator(word);
else if (isLastLetter(word, 'm'))
declinator = new RealizmDeclinator(word);
etc. These are only first three of tens of else if
clauses this method has.
A code of an exemplary declinator looks like this:
import static declining.utils.StringUtils.*;
public class RealizmDeclinator extends realizm_XuXowiX_XemXieXieDeclinator{
public RealizmDeclinator(String noun) {
super(noun);
}
@Override
protected String calculateStem() {
return word;
}
@Override
public String calculateLocative() {
return swap2ndFromEnd(stem, "ź") + "ie";
}
@Override
public String calculateVocative() {
return swap2ndFromEnd(stem, "ź") + "ie";
}
}
So here is the question, is there any other, more elegant algorithm for declining Polish words? Does it have to have so many if else clauses? Do I have to write so many declinators for each type of noun?
This problem showed me how simple and incredibly numerous are Polish declension rules. It made my algorithm boring and monotonous. Hopefully, one of you can help me make it interesting and concise!
Cheers