LUCENE-9671: Hunspell: shorten Stemmer.applyAffix (#2209)

Call stem() recursively just once with different arguments depending on various conditions. 

NOTE: committing in directly as this is a refactoring, not a functional change (no CHANGES.txt entry).
This commit is contained in:
Peter Gromov 2021-01-18 22:54:22 +01:00 committed by GitHub
parent 8505d4d416
commit ab08fdc6f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 57 deletions

View File

@ -552,7 +552,7 @@ final class Stemmer {
* @param prefix true if we are removing a prefix (false if it's a suffix)
* @return List of stems for the word, or an empty list if none are found
*/
List<CharsRef> applyAffix(
private List<CharsRef> applyAffix(
char[] strippedWord,
int length,
int affix,
@ -627,61 +627,34 @@ final class Stemmer {
circumfix = Dictionary.hasFlag(appendFlags, (char) dictionary.circumfix);
}
if (crossProduct) {
if (crossProduct && recursionDepth <= 1) {
boolean doPrefix;
if (recursionDepth == 0) {
if (prefix) {
prefixFlag = flag;
doPrefix = dictionary.complexPrefixes && dictionary.twoStageAffix;
// we took away the first prefix.
// COMPLEXPREFIXES = true: combine with a second prefix and another suffix
// COMPLEXPREFIXES = false: combine with a suffix
stems.addAll(
stem(
strippedWord,
length,
affix,
flag,
flag,
++recursionDepth,
dictionary.complexPrefixes && dictionary.twoStageAffix,
true,
true,
circumfix,
caseVariant));
} else if (!dictionary.complexPrefixes && dictionary.twoStageAffix) {
doPrefix = false;
// we took away a suffix.
// COMPLEXPREFIXES = true: we don't recurse! only one suffix allowed
// COMPLEXPREFIXES = false: combine with another suffix
stems.addAll(
stem(
strippedWord,
length,
affix,
flag,
prefixFlag,
++recursionDepth,
false,
true,
false,
circumfix,
caseVariant));
} else {
return stems;
}
} else if (recursionDepth == 1) {
} else {
doPrefix = false;
if (prefix && dictionary.complexPrefixes) {
prefixFlag = flag;
// we took away the second prefix: go look for another suffix
stems.addAll(
stem(
strippedWord,
length,
affix,
flag,
flag,
++recursionDepth,
false,
true,
true,
circumfix,
caseVariant));
} else if (!prefix && !dictionary.complexPrefixes && dictionary.twoStageAffix) {
} else if (prefix || dictionary.complexPrefixes || !dictionary.twoStageAffix) {
return stems;
}
// we took away a prefix, then a suffix: go look for another suffix
}
stems.addAll(
stem(
strippedWord,
@ -689,15 +662,13 @@ final class Stemmer {
affix,
flag,
prefixFlag,
++recursionDepth,
false,
recursionDepth + 1,
doPrefix,
true,
false,
prefix,
circumfix,
caseVariant));
}
}
}
return stems;
}