From ab08fdc6f0c9e5c7e27f053da59c619c6d9e643b Mon Sep 17 00:00:00 2001 From: Peter Gromov Date: Mon, 18 Jan 2021 22:54:22 +0100 Subject: [PATCH] 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). --- .../lucene/analysis/hunspell/Stemmer.java | 85 ++++++------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Stemmer.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Stemmer.java index c0f22994388..0f3143a551a 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Stemmer.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Stemmer.java @@ -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 applyAffix( + private List applyAffix( char[] strippedWord, int length, int affix, @@ -627,76 +627,47 @@ 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) { - // 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)); + } 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, + length, + affix, + flag, + prefixFlag, + recursionDepth + 1, + doPrefix, + true, + prefix, + circumfix, + caseVariant)); } return stems;