Class CharClass

java.lang.Object
opennlp.tools.util.normalizer.CharClass

public final class CharClass extends Object
A configurable class of Unicode code points and the cursor based operations over it.

A CharClass pairs a CodePointSet of member code points with a single canonical ASCII replacement code point. Whitespace and dashes are the two built-in presets (whitespace(), dashes()); any other class is one more configured instance with no new engine code.

Every operation is a single forward cursor pass over the text.

Instances are immutable and thread-safe.

Since:
3.0.0
  • Field Details

    • WHITESPACE

      private static final CharClass WHITESPACE
    • DASHES

      private static final CharClass DASHES
    • members

      private final CodePointSet members
    • replacement

      private final int replacement
  • Constructor Details

    • CharClass

      private CharClass(CodePointSet members, int replacement)
  • Method Details

    • of

      public static CharClass of(CodePointSet members, int replacement)
      Creates a class from a member set and a replacement code point.
      Parameters:
      members - The member code points.
      replacement - The canonical code point used by normalize(CharSequence) and collapse(CharSequence).
      Returns:
      The class.
      Throws:
      IllegalArgumentException - Thrown if members is null or replacement is not a valid code point.
    • whitespace

      public static CharClass whitespace()
      Returns the whitespace preset: the Unicode White_Space set, replacement U+0020.
      Returns:
      the whitespace preset: the Unicode White_Space set, replacement U+0020
    • dashes

      public static CharClass dashes()
      Returns the dash preset: the Unicode Dash set excluding the mathematical minus signs, replacement U+002D.
      Returns:
      the dash preset: the Unicode Dash set excluding the mathematical minus signs, replacement U+002D
    • withAdditional

      public CharClass withAdditional(CodePointSet extra)
      Returns a copy of this class whose member set is extended with extra (for example, user-defined code points loaded from CodePointSet.fromFile(java.nio.file.Path, java.lang.String)).
      Parameters:
      extra - The additional member code points.
      Returns:
      A new CharClass; this instance is unchanged.
      Throws:
      IllegalArgumentException - Thrown if extra is null.
    • members

      public CodePointSet members()
      Returns the member code points of this class.
      Returns:
      the member code points of this class
    • replacement

      public int replacement()
      Returns the canonical replacement code point.
      Returns:
      the canonical replacement code point
    • contains

      public boolean contains(int codePoint)
      Tests membership.
      Parameters:
      codePoint - The code point to test.
      Returns:
      true if the code point is a member of this class.
    • splitSpans

      public List<Span> splitSpans(CharSequence text)
      Splits text into the maximal runs of non-member code points, as character spans into the original text. Runs of members are delimiters and produce no empty spans.
      Parameters:
      text - The text to split.
      Returns:
      The token spans, in order.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • split

      public String[] split(CharSequence text)
      Splits text into the maximal runs of non-member code points.
      Parameters:
      text - The text to split.
      Returns:
      The tokens, in order, with no empty entries.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • normalize

      public String normalize(CharSequence text)
      Replaces each member code point with the replacement, one for one.

      When no code point of text is a member, the text is returned unchanged as its string form, which is not a copy for String input.

      Parameters:
      text - The text to normalize.
      Returns:
      The normalized text.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • collapse

      public String collapse(CharSequence text)
      Collapses each maximal run of member code points to a single replacement.

      Edges are not trimmed: a leading or trailing run becomes a single replacement, and a string made up entirely of members collapses to one replacement (for whitespace, a single space, not the empty string). Use trim(CharSequence) to drop edge members, or collapse and then trim to do both.

      When no code point of text is a member, the text is returned unchanged as its string form, which is not a copy for String input.

      Parameters:
      text - The text to collapse.
      Returns:
      The collapsed text.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • collapsePreserving

      public String collapsePreserving(CharSequence text, CodePointSet keep, int keepReplacement)
      Collapses runs of members like collapse(CharSequence), but emits keepReplacement instead of the usual replacement for any run that contains a code point in keep.
      Parameters:
      text - The text to collapse.
      keep - The member code points whose presence in a run preserves structure.
      keepReplacement - The replacement emitted for a run that contains a keep member.
      Returns:
      The collapsed text.
      Throws:
      IllegalArgumentException - Thrown if text or keep is null, or keepReplacement is not a valid code point.
    • trim

      public String trim(CharSequence text)
      Removes leading and trailing member code points.
      Parameters:
      text - The text to trim.
      Returns:
      The trimmed text.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • removeAll

      public String removeAll(CharSequence text)
      Removes every member code point.

      When no code point of text is a member, the text is returned unchanged as its string form, which is not a copy for String input.

      Parameters:
      text - The text to filter.
      Returns:
      The text with all members removed.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • normalizeAligned

      public AlignedText normalizeAligned(CharSequence text)
      Like normalize(CharSequence) but also produces the Alignment back to the original text.
      Parameters:
      text - The text to normalize.
      Returns:
      The normalized text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • collapseAligned

      public AlignedText collapseAligned(CharSequence text)
      Like collapse(CharSequence) but also produces the Alignment back to the original text. Each collapsed run maps to the run's whole original extent.
      Parameters:
      text - The text to collapse.
      Returns:
      The collapsed text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • collapsePreservingAligned

      public AlignedText collapsePreservingAligned(CharSequence text, CodePointSet keep, int keepReplacement)
      Like collapsePreserving(CharSequence, CodePointSet, int) but also produces the Alignment back to the original text.
      Parameters:
      text - The text to collapse.
      keep - The member code points whose presence in a run preserves structure.
      keepReplacement - The replacement emitted for a run that contains a keep member.
      Returns:
      The collapsed text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text or keep is null, or keepReplacement is not a valid code point.
    • collapseParagraphPreserving

      public String collapseParagraphPreserving(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement)
      Collapses runs of members like collapse(CharSequence), but emits paragraphReplacement when a run contains two or more logical line breaks, or the usual replacement when it contains at most one. A carriage return immediately followed by a line feed counts as one break, not two.
      Parameters:
      text - The text to collapse.
      lineBreaks - The member code points that count as a line break when tallying a run.
      paragraphReplacement - The replacement emitted for a run with two or more logical breaks.
      Returns:
      The collapsed text.
      Throws:
      IllegalArgumentException - Thrown if text or lineBreaks is null, or paragraphReplacement is not a valid code point.
    • collapseParagraphPreservingAligned

      public AlignedText collapseParagraphPreservingAligned(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement)
      Like collapseParagraphPreserving(CharSequence, CodePointSet, int) but also produces the Alignment back to the original text.
      Parameters:
      text - The text to collapse.
      lineBreaks - The member code points that count as a line break when tallying a run.
      paragraphReplacement - The replacement emitted for a run with two or more logical breaks.
      Returns:
      The collapsed text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text or lineBreaks is null, or paragraphReplacement is not a valid code point.
    • trimAligned

      public AlignedText trimAligned(CharSequence text)
      Like trim(CharSequence) but also produces the Alignment back to the original text. The trimmed leading and trailing members appear as deletions, so a span never reports through them.
      Parameters:
      text - The text to trim.
      Returns:
      The trimmed text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • removeAllAligned

      public AlignedText removeAllAligned(CharSequence text)
      Like removeAll(CharSequence) but also produces the Alignment back to the original text. Every removed member appears as a deletion, so a span never reports through one.
      Parameters:
      text - The text to filter.
      Returns:
      The filtered text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • substitute

      public static String substitute(CharSequence text, IntFunction<String> substitution)
      Applies a per-code-point substitution: each code point for which substitution returns a non-null string is replaced by that string, and the rest are copied through.

      When substitution returns null for every code point of text, the text is returned unchanged as its string form, which is not a copy for String input. The mapper is still applied exactly once per code point.

      Parameters:
      text - The text to transform.
      substitution - The replacement for a code point, or null to copy it through.
      Returns:
      The transformed text.
      Throws:
      IllegalArgumentException - Thrown if text or substitution is null.
    • substituteAligned

      public static AlignedText substituteAligned(CharSequence text, IntFunction<String> substitution)
      Like substitute(CharSequence, IntFunction) but also produces the Alignment back to the original text. Each replaced code point maps to its replacement string as one block.
      Parameters:
      text - The text to transform.
      substitution - The replacement for a code point, or null to copy it through.
      Returns:
      The transformed text and its alignment.
      Throws:
      IllegalArgumentException - Thrown if text or substitution is null.
    • firstMember

      private int firstMember(CharSequence text)
      Finds the index of the first member code point, so callers can return member-free text uncopied.
      Parameters:
      text - The text to scan.
      Returns:
      The index of the first member code point, or text.length() when the text contains no member.
    • collapseRuns

      private String collapseRuns(CharSequence text, CharClass.RunReplacement runReplacement, Alignment.Builder alignment)
      Collapses each maximal run of member code points to the code point chosen by runReplacement and copies every other code point.
      Parameters:
      text - The text to collapse.
      runReplacement - Chooses the replacement for each run.
      alignment - Receives the edits of the pass, or null if no alignment is needed.
      Returns:
      The collapsed text.
    • collapseRunsAligned

      private AlignedText collapseRunsAligned(CharSequence text, CharClass.RunReplacement runReplacement)
      Collapses runs like collapseRuns(CharSequence, RunReplacement, Alignment.Builder) and pairs the result with its Alignment back to text.
      Parameters:
      text - The text to collapse.
      runReplacement - Chooses the replacement for each run.
      Returns:
      The collapsed text and its alignment, as AlignedText.
    • preservingReplacement

      private CharClass.RunReplacement preservingReplacement(CharSequence text, CodePointSet keep, int keepReplacement)
      Returns the CharClass.RunReplacement of the collapsePreserving operations: keepReplacement for a run that contains a keep code point, the usual replacement otherwise.
    • paragraphReplacement

      private CharClass.RunReplacement paragraphReplacement(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement)
      Returns the CharClass.RunReplacement of the collapseParagraphPreserving operations: paragraphReplacement for a run with two or more logical line breaks, the usual replacement otherwise.
    • containsAny

      private static boolean containsAny(CharSequence text, int from, int to, CodePointSet set)
      Tests whether a code point of set occurs in a range of text.
      Parameters:
      text - The text to scan.
      from - The index where the range starts.
      to - The index where the range ends, exclusive.
      set - The CodePointSet of code points to look for.
      Returns:
      true if any code point in the range is in set.
    • skipRun

      private int skipRun(CharSequence text, int runStart)
      Advances past a run of member code points.
      Parameters:
      text - The text to scan.
      runStart - The index where the member run starts.
      Returns:
      The index of the first non-member code point at or after runStart, or text.length() when the run extends to the end of the text.
    • skipRunBackward

      private int skipRunBackward(CharSequence text, int floor, int runEnd)
      Moves back past a run of member code points that ends at runEnd.
      Parameters:
      text - The text to scan.
      floor - The lowest index the scan may reach.
      runEnd - The index after the last code point of the run.
      Returns:
      The index after the last non-member code point before runEnd, or floor when every code point in [floor, runEnd) is a member.
    • countLogicalLineBreaks

      private int countLogicalLineBreaks(CharSequence text, int runStart, int runEnd, CodePointSet lineBreaks)
      Counts logical line breaks in a whitespace run. A carriage return immediately followed by a line feed counts as one break.
      Parameters:
      text - The text containing the run.
      runStart - The index where the whitespace run starts.
      runEnd - The index of the first code point after the run.
      lineBreaks - The code points that count as a line break when tallying the run.
      Returns:
      The number of logical line breaks in text[runStart..runEnd).