Class TokenizerCharacterPolicy

java.lang.Object
opennlp.tools.tokenize.TokenizerCharacterPolicy
All Implemented Interfaces:
Predicate<CharSequence>

public final class TokenizerCharacterPolicy extends Object implements Predicate<CharSequence>
An immutable policy for testing token eligibility against explicit character sets.

A token consists of one or more letters or digits. Each letter or digit may be followed by zero or more marks. A mark cannot start a token. The supplied sets express a caller's tokenization policy; this class does not infer Unicode character categories.

This predicate tests a complete candidate without finding token boundaries or changing its text.

Since:
3.0.0
  • Field Details

  • Constructor Details

  • Method Details

    • of

      public static TokenizerCharacterPolicy of(CodePointSet letters, CodePointSet digits, CodePointSet marks)
      Creates a policy from explicit, pairwise-disjoint code point sets. Whitespace separates tokens and so cannot be part of one; every other non-surrogate code point is left to the caller.
      Parameters:
      letters - Code points treated as letters.
      digits - Code points treated as digits.
      marks - Code points permitted after a letter, a digit, or another mark.
      Returns:
      The immutable policy.
      Throws:
      IllegalArgumentException - Thrown if a set is null, letters and digits are both empty, a code point occurs in more than one set, or a set contains a surrogate code point or UnicodeWhitespace member.
    • ascii

      public static TokenizerCharacterPolicy ascii()
      Returns a policy for ASCII letters and digits, with no marks.
      Returns:
      a policy for ASCII letters and digits, with no marks
    • parse

      public static TokenizerCharacterPolicy parse(String text)
      Parses the text form written by toString(): the three fields letters=, ;digits= and ;marks= in that order, each followed by zero or more comma separated hex code points (0041) or inclusive ranges (0041-005A). Hex digits may use either case; no prefix or whitespace is accepted.
      Parameters:
      text - The text form to read.
      Returns:
      The policy the text describes.
      Throws:
      IllegalArgumentException - Thrown if text is null or not of that form, or if the sets it describes are rejected by of(CodePointSet, CodePointSet, CodePointSet).
    • test

      public boolean test(CharSequence input)

      Returns true if the entire input is a non-empty token accepted by this policy. Malformed UTF-16 input (an unpaired or reversed surrogate) returns false.

      Specified by:
      test in interface Predicate<CharSequence>
      Throws:
      IllegalArgumentException - Thrown if input is null.
    • getLetters

      public CodePointSet getLetters()
      Returns the immutable letter set.
      Returns:
      the immutable letter set
    • getDigits

      public CodePointSet getDigits()
      Returns the immutable digit set.
      Returns:
      the immutable digit set
    • getMarks

      public CodePointSet getMarks()
      Returns the immutable mark set.
      Returns:
      the immutable mark set
    • equals

      public boolean equals(Object o)

      Two policies are equal if their letter, digit and mark sets are equal.

      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()

      Consistent with equals(Object): computed from the letter, digit and mark sets.

      Overrides:
      hashCode in class Object
    • toString

      public String toString()

      Writes the text form read by parse(String), for example letters=0041-005A,0061-007A;digits=0030-0039;marks= for ascii(): runs of adjacent code points become ranges, and each code point is written with at least four upper-case hex digits.

      Overrides:
      toString in class Object
    • appendRanges

      private static void appendRanges(StringBuilder text, CodePointSet set)
      Appends the members of set as comma separated code points and ranges.
    • appendHex

      private static void appendHex(StringBuilder text, int codePoint)
      Appends codePoint as upper-case hex with at least MIN_HEX_DIGITS digits.
    • parseRanges

      private static CodePointSet parseRanges(String text, int from, int to)
      Reads the comma separated code points and ranges in text[from, to).
    • parseRange

      private static CodePointSet parseRange(String text, int from, int to)
      Reads one code point or one inclusive range in text[from, to).
    • parseHex

      private static int parseHex(String text, int from, int to)
      Reads the hex code point in text[from, to).
    • malformed

      private static IllegalArgumentException malformed(String text, String detail)
      Returns the exception for a text form that cannot be read.
      Returns:
      the exception for a text form that cannot be read
    • validateSet

      private static void validateSet(String name, CodePointSet set)
      Throws if set contains a surrogate code point or UnicodeWhitespace member.
    • requireDisjoint

      private static void requireDisjoint(String firstName, CodePointSet first, String secondName, CodePointSet second)
      Throws if first and second share a code point.