Class HunspellStemmer

java.lang.Object
opennlp.tools.stemmer.hunspell.HunspellStemmer
All Implemented Interfaces:
Stemmer

@ThreadSafe public final class HunspellStemmer extends Object implements Stemmer
A dictionary-backed Stemmer over a HunspellDictionary: a surface form is reduced to the dictionary words it can be derived from by removing one suffix, one prefix, a cross-product combination of both, or an additional affix licensed by a continuation class. Compound and break-separated forms return the stems of their recognized parts.

stem(CharSequence) returns the first analysis, preferring the word's own dictionary entry; stemAll(CharSequence) returns every distinct analysis. A word with no analysis is returned unchanged, so the stemmer degrades to identity on unknown input. Title-case and all-uppercase forms also use permitted case variants, subject to the dictionary's case restrictions. Entries the dictionary marks as virtual stems (NEEDAFFIX), compound-only parts (ONLYINCOMPOUND), or forbidden words (FORBIDDENWORD) do not count as standalone analyses.

Compound part search stops after 2048 part-licensing attempts. The budget applies separately to each capitalization form of the input and to each word-break search. Once it is used up, further compound analyses are skipped. The Stemmer interface leaves thread safety to the implementation. This implementation reads only the immutable dictionary state, so a single instance is safe to share between threads.

Since:
3.0.0
  • Field Details

    • PART_CHECK_BUDGET

      private static final int PART_CHECK_BUDGET
      Maximum candidate checks per compound search or recursive word-break search.
      See Also:
    • MAX_COMPOUND_PARTS

      private static final int MAX_COMPOUND_PARTS
      Maximum parts in one compound analysis.
      See Also:
    • MAX_CASE_VARIANTS

      private static final int MAX_CASE_VARIANTS
      Maximum case variants considered for CHECKSHARPS.
      See Also:
    • MAX_ANALYSES

      private static final int MAX_ANALYSES
      Maximum distinct morphological readings returned for one input.
      See Also:
    • dictionary

      private final HunspellDictionary dictionary
  • Constructor Details

    • HunspellStemmer

      public HunspellStemmer(HunspellDictionary dictionary)
      Initializes the stemmer.
      Parameters:
      dictionary - The dictionary to analyze against. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if dictionary is null.
  • Method Details

    • stem

      public CharSequence stem(CharSequence word)
      Stems word.

      Returns the first analysis, which prefers the word's own dictionary entry.

      Specified by:
      stem in interface Stemmer
      Parameters:
      word - The input word. Must not be null.
      Returns:
      The stemmed form.
    • stemAll

      public List<CharSequence> stemAll(CharSequence word)
      Returns all stem forms for word. Defaults to a single-element list from Stemmer.stem(CharSequence). Dictionary-based engines may override this to return multiple roots; delegating wrappers must forward it to preserve the full list.

      Returns every distinct analysis, or a single-element list of the unchanged word when it has none.

      Specified by:
      stemAll in interface Stemmer
      Parameters:
      word - The input word. Must not be null.
      Returns:
      all stem forms for word
    • analyze

      List<String> analyze(CharSequence word)
      Returns morphological analyses as space-separated Hunspell fields. Each result describes a complete accepted reading. Entries without an explicit st: field use the dictionary entry as their stem. Compound components begin with pa:; entry and affix fields follow in application order. Results preserve dictionary field text without output conversion.
      Parameters:
      word - The input to analyze. Must not be null.
      Returns:
      An immutable list of distinct analyses, or an empty list for unknown input. At most 2048 analyses are returned.
      Throws:
      IllegalArgumentException - Thrown if word is null.
    • findWord

      private List<String> findWord(String input, boolean morphological)
      Finds the readings of a complete input. Trailing periods are removed first so that abbreviations match their entries; when the shortened form has no reading, one period is restored for entries listed with it.
      Parameters:
      input - The input after conversion.
      morphological - Whether results contain morphological fields.
      Returns:
      Recognized stems or analyses, or an empty list.
    • findStems

      private List<String> findStems(String input, int depth, int[] budget, Map<String,List<String>> cache, boolean morphological)
      Finds recognized stems without returning identity for unrecognized pieces.
      Parameters:
      input - The normalized input or a break-separated component.
      depth - The current break depth.
      budget - The remaining break attempts.
      cache - Results for completed pieces of this input.
      morphological - Whether results contain morphological fields.
      Returns:
      Recognized stems, or an empty list.
    • hyphenatedFirstPart

      private List<String> hyphenatedFirstPart(String text, boolean morphological)
      Finds the readings of the part of a Hungarian word before a hyphen, which is accepted as a listed word ending in the hyphen or as a compound under the moving rule.
      Parameters:
      text - The part before the hyphen.
      morphological - Whether results contain morphological fields.
      Returns:
      Recognized stems or analyses, or an empty list.
    • variants

      private List<String> variants(String surface)
      Collects the case variants to analyze: the surface form first, then its lowercase form when the two differ. A capitalized word with a further inner capital, such as a mixed-case word at the start of a sentence, is also tried with a lowercase initial. An all-uppercase word containing an apostrophe is also tried with the part after the apostrophe capitalized, for the elided articles of Catalan, French, and Italian. Ordering matters because the first analysis found wins in stem(CharSequence).
      Parameters:
      surface - The surface form.
      Returns:
      The variants in analysis order. Never null or empty.
    • initialUpper

      private String initialUpper(String word)
      Converts the initial code point to uppercase with the dictionary's case mapping.
      Parameters:
      word - The nonempty word.
      Returns:
      The capitalized form.
    • addSharpVariants

      private void addSharpVariants(String word, int from, Set<String> variants)
      Collects sharp-s alternatives with a fixed search limit.
      Parameters:
      word - The lowercase candidate.
      from - The next character position to search.
      variants - The resulting forms.
    • analyze

      private void analyze(String word, HunspellStemmer.Analysis analyses)
      Adds every analysis of one case variant to the result set: the word's own dictionary entry, single suffix removal, twofold suffix removal through continuation classes, single prefix removal, and cross-product removal of one prefix together with one suffix and an optional continuation suffix. Insertion order into the set fixes the preference order reported by stemAll(CharSequence).
      Parameters:
      word - The case variant to analyze.
      analyses - The mutable, insertion-ordered set collecting the stems found.
    • decompose

      private void decompose(String word, String surface, HunspellStemmer.Results analyses)
      Searches compound components after standalone analysis fails. Entries and affixes must permit the selected component positions and junctions. Recognized spaced forms prevent concatenation. The candidate budget applies to compound decomposition; output follows component order.
      Parameters:
      word - The case variant to decompose.
      surface - The surface form the variant was derived from; character case at junctions is checked using this input for CHECKCOMPOUNDCASE.
      analyses - The mutable, insertion-ordered set collecting the part stems.
    • rejectsCompoundText

      private boolean rejectsCompoundText(String text)
      Applies the text-level compound checks to the text every compound level splits: a CHECKCOMPOUNDREP replacement or a space inserted at any position must not produce a recognized non-compound form.
      Parameters:
      text - The complete input or the remainder a compound level splits.
      Returns:
      true if a check forbids splitting the text.
    • searchTriples

      private void searchTriples(String word, String surface, int from, List<HunspellStemmer.Junction> junctions, HunspellStemmer.Results analyses, int[] budget)
      Restores simplified triple letters at independently checked boundaries.
      Parameters:
      word - The current text.
      surface - The aligned case source.
      from - The next position eligible for restoration.
      junctions - The required boundaries accumulated so far.
      analyses - The destination for stems.
      budget - The remaining search attempts.
    • searchSpelling

      private void searchSpelling(String word, String surface, HunspellStemmer.Results analyses, int[] budget, List<HunspellStemmer.Junction> junctions)
      Searches one spelling, optionally requiring a restored compound junction.
      Parameters:
      word - The spelling with any compound substitution expanded.
      surface - The aligned case source.
      analyses - The destination for stems.
      budget - The remaining candidate checks across forms.
      junctions - Required boundaries for spelling substitutions.
    • searchRule

      private void searchRule(String word, String surface, int from, HunspellCompoundRule rule, List<HunspellStemmer.CompoundPart> parts, List<int[]> flags, HunspellStemmer.Results analyses, int[] budget)
      Searches compound-rule paths without combining flags from different homonyms.
      Parameters:
      word - The input case variant.
      surface - The input used for case restrictions.
      from - The current UTF-16 offset.
      rule - The compound pattern.
      parts - The selected readings.
      flags - The flags corresponding to the readings.
      analyses - The destination for stems.
      budget - The remaining candidate checks.
    • ruleParts

      private List<HunspellStemmer.CompoundPart> ruleParts(String part, String surface, boolean last)
      Finds selected dictionary entries for a compound-rule part.
      Parameters:
      part - The part's case variant.
      surface - The part's supplied case.
      last - Whether suffix removal is permitted at this position.
      Returns:
      The permitted readings.
    • addRulePart

      private void addRulePart(String part, String root, String surface, boolean last, List<HunspellStemmer.CompoundPart> result, HunspellDictionary.Affix... affixes)
      Adds homonyms permitted by their blocking and affix flags.
      Parameters:
      part - The component text.
      root - The restored entry spelling.
      surface - The supplied part spelling.
      last - Whether the part closes the compound.
      result - The candidate destination.
      affixes - The applied suffixes.
    • search

      private void search(String word, String caseSource, int[] codePointOffsets, int fromPoint, List<HunspellStemmer.CompoundPart> parts, HunspellStemmer.Results analyses, int[] budget, List<HunspellStemmer.Junction> junctions, byte[] remainderChecks)
      Extends a partial decomposition with the part starting at fromPoint, trying every admissible length and recursing on the remainder. The boundary into this part honors the CHECKCOMPOUNDCASE and CHECKCOMPOUNDTRIPLE declarations, a part repeating its left neighbor honors CHECKCOMPOUNDDUP, and a completed decomposition flushes every part's stems into the analyses in part order.
      Parameters:
      word - The case variant under decomposition.
      caseSource - The character-case source for junction checks, the surface form when its offsets align with the variant.
      codePointOffsets - UTF-16 offsets for each code point boundary.
      fromPoint - The code point index where the next part starts.
      parts - The selected part readings.
      analyses - The mutable, insertion-ordered set collecting the part stems.
      budget - The remaining part-licensing attempts, counted down in place.
      junctions - The required boundaries for substitutions.
      remainderChecks - The cached outcome of rejectsCompoundText(String) per code point index: 0 unknown, 1 allowed, 2 rejected.
    • rejectsJunction

      private boolean rejectsJunction(HunspellStemmer.CompoundPart left, HunspellStemmer.CompoundPart right, HunspellDictionary.CompoundPattern allowed, boolean last)
      Applies the junction declarations. CHECKCOMPOUNDDUP forbids the closing part from repeating the part before it; only the two parts joined at each level are compared, so an earlier repetition is not checked. A junction restored from a pattern replacement must satisfy that pattern's flag conditions and is exempt from the other patterns; any other junction is forbidden when some pattern matches it.
      Parameters:
      left - The preceding part.
      right - The candidate part.
      allowed - The rule allowing this substituted junction, or null.
      last - Whether the candidate closes the compound.
      Returns:
      true if a declaration forbids this part here.
    • violatesBoundaryChecks

      private boolean violatesBoundaryChecks(String word, String caseSource, int from, boolean allowTriple)
      Applies the character-level boundary declarations at the junction before from: CHECKCOMPOUNDCASE forbids an uppercase character on either side of the junction, and CHECKCOMPOUNDTRIPLE forbids the same character three times in a row across it.
      Parameters:
      word - The case variant under decomposition.
      caseSource - The character-case source for the uppercase judgment.
      from - The index the junction sits before; greater than zero.
      allowTriple - Whether this junction restores a simplified repeated letter.
      Returns:
      true if a declaration forbids this junction.
    • partReadings

      private List<HunspellStemmer.CompoundPart> partReadings(String part, String surface, HunspellDictionary.CompoundPosition position, boolean first, boolean last, boolean movingRule)
      Collects direct and affixed readings for a compound component. Affixes at internal boundaries require the permit flag. If no reading is found, a capitalized dictionary entry is also considered.
      Parameters:
      part - The part's surface text.
      surface - The supplied case.
      position - The component position.
      first - Whether the part opens the word.
      last - Whether the part closes the word.
      movingRule - Whether the Hungarian moving rule relaxes the opening parts.
      Returns:
      The permitted readings in discovery order.
    • collectPartReadings

      private void collectPartReadings(String part, String surface, HunspellDictionary.CompoundPosition position, boolean first, boolean last, boolean movingRule, List<HunspellStemmer.CompoundPart> readings)
      Collects direct entries and permitted affix combinations for a component.
      Parameters:
      part - The part spelling to look up.
      surface - The supplied case.
      position - The part's place in the compound.
      first - Whether the part opens the word.
      last - Whether the part closes the word.
      movingRule - Whether an opening entry may also qualify through the hardwired Hungarian flags.
      readings - The destination for selected readings.
    • collectCrossPartReadings

      private void collectCrossPartReadings(String part, String surface, HunspellDictionary.Affix prefix, HunspellDictionary.CompoundPosition position, boolean first, boolean last, List<HunspellStemmer.CompoundPart> readings)
      Searches suffix combinations after removing a compound component's prefix.
      Parameters:
      part - The component text.
      surface - The supplied case.
      prefix - The prefix rule.
      position - The component position.
      first - Whether this is the opening component.
      last - Whether this is the closing component.
      readings - The destination for accepted readings.
    • collectCrossPart

      private void collectCrossPart(String part, String surface, String intermediate, HunspellDictionary.Affix prefix, HunspellDictionary.Affix suffix, HunspellDictionary.CompoundPosition position, boolean last, List<HunspellStemmer.CompoundPart> readings)
      Validates a compound component with a prefix and suffix.
      Parameters:
      part - The component text.
      surface - The supplied case.
      intermediate - The text after prefix removal.
      prefix - The prefix rule.
      suffix - The suffix rule.
      position - The component position.
      last - Whether this is the closing component.
      readings - The destination for accepted readings.
    • collectAffixedPartReading

      private void collectAffixedPartReading(String part, String surface, HunspellDictionary.Affix affix, HunspellDictionary.CompoundPosition position, boolean first, boolean last, List<HunspellStemmer.CompoundPart> readings)
      Adds the stem of one affixed part reading when the rule and the stem's entry admit it at the position.
      Parameters:
      part - The part spelling under analysis.
      surface - The supplied case.
      affix - The rule to undo.
      position - The part's place in the compound.
      first - Whether this is the opening component.
      last - Whether this is the closing component.
      readings - The destination for selected readings.
    • collectDoublePart

      private void collectDoublePart(String part, String surface, String intermediate, HunspellDictionary.Affix inner, HunspellDictionary.Affix outer, HunspellDictionary.CompoundPosition position, boolean last, List<HunspellStemmer.CompoundPart> readings)
      Finds a compound part with continuation-linked affixes.
      Parameters:
      part - The component text.
      surface - The supplied case.
      intermediate - The form after outer affix removal.
      inner - The inner rule.
      outer - The outer rule.
      position - The compound position.
      last - Whether this is the final part.
      readings - The destination for readings.
    • collectCompoundSuffixAfterPrefixes

      private void collectCompoundSuffixAfterPrefixes(String part, String surface, String text, HunspellDictionary.Affix inner, HunspellDictionary.Affix outer, HunspellDictionary.Affix suffix, HunspellDictionary.CompoundPosition position, boolean last, List<HunspellStemmer.CompoundPart> readings)
      Checks a suffix after continuation-linked compound prefixes.
      Parameters:
      part - The component text.
      surface - The supplied case.
      text - The text after removing the prefixes.
      inner - The inner prefix.
      outer - The outer prefix.
      suffix - The suffix rule.
      position - The component position.
      last - Whether this is the final component.
      readings - The result destination.
    • collectCrossDoublePart

      private void collectCrossDoublePart(String part, String surface, String text, HunspellDictionary.Affix inner, HunspellDictionary.Affix outer, HunspellDictionary.Affix prefix, HunspellDictionary.CompoundPosition position, List<HunspellStemmer.CompoundPart> readings)
      Removes an inner compound suffix after a prefix and outer suffix.
      Parameters:
      part - The component text.
      surface - The supplied case.
      text - The text after removing the outer suffix and prefix.
      inner - The inner suffix.
      outer - The outer suffix.
      prefix - The prefix rule.
      position - The component position.
      readings - The result destination.
    • addCompoundCrossDouble

      private void addCompoundCrossDouble(String part, String surface, String root, HunspellDictionary.Affix inner, HunspellDictionary.Affix outer, HunspellDictionary.Affix cross, HunspellDictionary.CompoundPosition position, List<HunspellStemmer.CompoundPart> readings)
      Validates a continuation sequence combined with an opposite-end affix.
      Parameters:
      part - The component text.
      surface - The supplied case.
      root - The restored entry text.
      inner - The inner rule in the continuation sequence.
      outer - The outer rule in the continuation sequence.
      cross - The opposite-end rule.
      position - The component position.
      readings - The result destination.
    • isNoncompoundForm

      private boolean isNoncompoundForm(String word)
      Checks a direct or affixed form without compound recursion.
      Parameters:
      word - The candidate spelling.
      Returns:
      Whether a non-compound reading exists.
    • removeAffixInCompound

      private String removeAffixInCompound(String part, HunspellDictionary.Affix affix, boolean suffix)
      Undoes one affix rule on a compound part. Unlike the standalone removals, a rule that neither adds nor removes material is undone here, to its own spelling with the condition checked, because dictionaries position compound parts through exactly such zero rules.
      Parameters:
      part - The part spelling under analysis.
      affix - The rule to undo.
      suffix - Whether the rule is a suffix rule.
      Returns:
      The candidate stem, or null when the rule does not apply.
    • undoSuffix

      private void undoSuffix(String word, HunspellDictionary.Affix suffix, HunspellStemmer.Analysis analyses)
      Undoes one suffix rule and, through continuation classes, one further suffix on the intermediate stem, adding dictionary-confirmed analyses. A rule that applies only inside compounds or requires the matching circumfix member is not undone because no prefix accompanies this path. A rule requiring a further affix produces no single-removal analysis. A rule that adds and strips no material is undone like any other, which recognizes a virtual stem it completes and reports its fields.
      Parameters:
      word - The case variant under analysis.
      suffix - The suffix rule to undo.
      analyses - The mutable, insertion-ordered set collecting the stems found.
    • undoInnerSuffix

      private void undoInnerSuffix(String stem, HunspellDictionary.Affix outer, HunspellDictionary.Affix inner, HunspellStemmer.Analysis analyses)
      Undoes the inner suffix of a twofold removal when the rule's continuation classes allow the outer one. The continuation-linked combination satisfies a NEEDAFFIX marker on either rule.
      Parameters:
      stem - The intermediate stem after the outer removal.
      outer - The already-undone outer suffix rule.
      inner - The candidate inner suffix rule.
      analyses - The mutable, insertion-ordered set collecting the stems found.
    • undoPrefix

      private void undoPrefix(String word, HunspellDictionary.Affix prefix, HunspellStemmer.Analysis analyses)
      Undoes one prefix rule and, for cross-product rules, one further suffix on the intermediate stem, adding dictionary-confirmed analyses. A rule that applies only inside compounds is not undone at all. A rule marked as needing a further affix or the matching circumfix member produces no single-removal analysis. A rule that adds and strips no material is undone like any other. A valid cross-product suffix can combine with either kind of rule.
      Parameters:
      word - The case variant under analysis.
      prefix - The prefix rule to undo.
      analyses - The mutable, insertion-ordered set collecting the stems found.
    • undoCrossProductSuffix

      private void undoCrossProductSuffix(String stem, HunspellDictionary.Affix prefix, HunspellDictionary.Affix suffix, HunspellStemmer.Analysis analyses)
      Undoes the suffix half of a cross-product removal when both rules opted in. The two rules must agree on circumfixing: a circumfix-marked affix is only valid with a marked affix of the other kind, so a pair of which exactly one is marked mixes an ordinary affix into a circumfix and is rejected.
      Parameters:
      stem - The intermediate stem after the prefix removal.
      prefix - The already-undone prefix rule.
      suffix - The candidate suffix rule.
      analyses - The mutable, insertion-ordered set collecting the stems found.
    • undoCrossProductInnerSuffix

      private void undoCrossProductInnerSuffix(String stem, HunspellDictionary.Affix prefix, HunspellDictionary.Affix outer, HunspellDictionary.Affix inner, HunspellStemmer.Analysis analyses)
      Undoes an inner suffix after a prefix and an outer suffix have been removed. The inner suffix must license the outer suffix through the continuation flags. The suffix combination satisfies NEEDAFFIX markers in the derivation.
      Parameters:
      stem - The intermediate stem after the prefix and outer suffix removal.
      prefix - The already-undone prefix rule.
      outer - The already-undone outer suffix rule.
      inner - The candidate inner suffix rule.
      analyses - The mutable, insertion-ordered set collecting the stems found.
    • undoInnerPrefix

      private void undoInnerPrefix(String stem, HunspellDictionary.Affix outer, HunspellDictionary.Affix inner, HunspellStemmer.Analysis analyses)
      Removes a continuation-linked inner prefix in COMPLEXPREFIXES mode.
      Parameters:
      stem - The form after the outer prefix was removed.
      outer - The outer prefix.
      inner - The inner prefix.
      analyses - The result context.
    • undoDoublePrefixSuffix

      private void undoDoublePrefixSuffix(String word, HunspellDictionary.Affix inner, HunspellDictionary.Affix outer, HunspellDictionary.Affix suffix, HunspellStemmer.Analysis analyses)
      Removes the suffix following a continuation-linked prefix combination.
      Parameters:
      word - The form after prefix removal.
      inner - The inner prefix.
      outer - The outer prefix.
      suffix - The candidate suffix.
      analyses - The result context.
    • removeSuffix

      private String removeSuffix(String word, HunspellDictionary.Affix suffix)
      Undoes one suffix rule: cuts the affix material off the end of the word, restores the strip string the rule removed on application, and checks the rule's condition against the restored stem. A strip-only rule, whose affix material is empty, is undone by restoring its strip string alone. Rules that neither add nor remove material are handled by removeSuffixAllowingIdentity(String, Affix), and candidates that would leave an empty stem are rejected. A word the affix material covers entirely reverses a full-strip application, which hunspell only performs when the affix file declares FULLSTRIP; without that declaration the rule does not apply.
      Parameters:
      word - The surface form.
      suffix - The rule to undo.
      Returns:
      The candidate stem, or null when the rule does not apply.
    • removeSuffixAllowingIdentity

      private String removeSuffixAllowingIdentity(String word, HunspellDictionary.Affix suffix)
      Undoes a suffix in a continuation sequence, including a rule that changes no material. An identity rule still has to satisfy the condition.
      Parameters:
      word - The surface form at this point in the sequence.
      suffix - The rule to undo.
      Returns:
      The candidate stem, or null when the rule does not apply.
    • removePrefixAllowingIdentity

      private String removePrefixAllowingIdentity(String word, HunspellDictionary.Affix prefix)
      Undoes a prefix in a continuation sequence, including a rule that changes no material. An identity rule still has to satisfy the condition.
      Parameters:
      word - The surface form at this point in the sequence.
      prefix - The rule to undo.
      Returns:
      The candidate stem, or null when the rule does not apply.
    • isIdentityRule

      private boolean isIdentityRule(HunspellDictionary.Affix affix)
      Checks whether an affix rule adds and strips no material.
      Parameters:
      affix - The rule to inspect.
      Returns:
      true if applying the rule does not change the spelling.
    • removePrefix

      private String removePrefix(String word, HunspellDictionary.Affix prefix)
      Undoes one prefix rule: cuts the affix material off the start of the word, restores the strip string the rule removed on application, and checks the rule's condition against the restored stem. A strip-only rule, whose affix material is empty, is undone by restoring its strip string alone. Rules that neither add nor remove material are handled by removePrefixAllowingIdentity(String, Affix), and candidates that would leave an empty stem are rejected. A word the affix material covers entirely reverses a full-strip application, which hunspell only performs when the affix file declares FULLSTRIP; without that declaration the rule does not apply.
      Parameters:
      word - The surface form.
      prefix - The rule to undo.
      Returns:
      The candidate stem, or null when the rule does not apply.