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