Class ShrinkCharSequenceNormalizer

java.lang.Object
opennlp.tools.util.normalizer.ShrinkCharSequenceNormalizer
All Implemented Interfaces:
Serializable, CharSequenceNormalizer

public class ShrinkCharSequenceNormalizer extends Object implements CharSequenceNormalizer
A CharSequenceNormalizer implementation that shrinks repeated whitespace and repeated characters in text, in three steps:
  1. Each run of two or more ASCII whitespace characters (tab, line feed, vertical tab, form feed, carriage return, or space) collapses to a single space. A lone whitespace character is kept as it is.
  2. Each run of three or more repeats of one code point shrinks to two copies of its first occurrence, comparing repeats case-insensitively over ASCII; a run never starts on a line terminator. For example, "coooool" becomes "cool".
  3. Every leading and trailing character at or below U+0020 is dropped, the same rule String.trim() applies.
See Also:
  • Field Details

  • Constructor Details

    • ShrinkCharSequenceNormalizer

      public ShrinkCharSequenceNormalizer()
  • Method Details

    • getInstance

      public static ShrinkCharSequenceNormalizer getInstance()
      Returns the shared, stateless instance.
      Returns:
      the shared, stateless instance
    • normalize

      public CharSequence normalize(CharSequence text)
      Normalizes a sequence of characters.
      Specified by:
      normalize in interface CharSequenceNormalizer
      Parameters:
      text - The CharSequence to normalize. Must not be null.
      Returns:
      The normalized CharSequence.
    • shrinkWhitespace

      private CharSequence shrinkWhitespace(CharSequence text)
      Replaces each run of two or more ASCII whitespace characters with a single space; a lone whitespace character stays as it is.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when no run was found, otherwise the shrunk copy.
    • shrinkRepeatedCodePoints

      private CharSequence shrinkRepeatedCodePoints(CharSequence text)
      Shrinks each run of three or more repeats of one code point to two copies of its first occurrence. The run's first code point must not be a line terminator, every repeat advances by the first occurrence's char count while comparing whole code points ASCII case-insensitively, and a failed attempt resumes at the next char.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when no run was found, otherwise the shrunk copy.