Class LatticeTokenizer

java.lang.Object
opennlp.tools.tokenize.lattice.LatticeTokenizer
All Implemented Interfaces:
Tokenizer

public final class LatticeTokenizer extends Object implements Tokenizer
Dictionary-driven segmentation for languages written without spaces: a Viterbi search over the word lattice of a MecabDictionary, minimizing the sum of word costs and connection costs. This is the segmentation approach behind Japanese and Korean morphological analysis. A single decoder serves both because the supplied dictionary provides the language-specific data.

Unknown text is handled through the dictionary's character categories: where the lexicon has no entry, or a category always invokes them, unknown-word candidates are generated per category template, grouping runs of same-category characters when the category requests it. A multi-category run continues while successive assignments overlap. Whitespace cannot join or appear as a morpheme. Every reported span is in original text coordinates.

analyze(String) returns full morphemes with their dictionary features; the Tokenizer view reports just the surfaces and spans.

The tokenizer reads only immutable dictionary state and is safe to share between threads.

Since:
3.0.0
  • Field Details

    • BOUNDARY_CONTEXT

      private static final int BOUNDARY_CONTEXT
      The context id of the beginning and end of text.
      See Also:
    • dictionary

      private final MecabDictionary dictionary
  • Constructor Details

    • LatticeTokenizer

      public LatticeTokenizer(MecabDictionary dictionary)
      Initializes the tokenizer.
      Parameters:
      dictionary - The dictionary to segment with. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if dictionary is null.
  • Method Details

    • analyze

      public List<Morpheme> analyze(String text)
      Segments a text into morphemes with their dictionary features.
      Parameters:
      text - The text to segment. Must not be null.
      Returns:
      The morphemes in text order, spans in original coordinates, whitespace omitted. Never null; empty for empty or all-whitespace input.
      Throws:
      IllegalArgumentException - Thrown if text is null.
      IllegalStateException - Thrown if the dictionary offers no candidate at some position, which a unk.def without a DEFAULT template does.
    • tokenize

      public String[] tokenize(String text)
      Splits a string into its atomic parts.

      Reports the segmented surfaces, whitespace omitted.

      Specified by:
      tokenize in interface Tokenizer
      Parameters:
      text - The string to be tokenized.
      Returns:
      The String[] with the individual tokens as the array elements.
      Throws:
      IllegalArgumentException - Thrown if text is null.
      IllegalStateException - Thrown if the dictionary offers no candidate at some position; see analyze(String).
    • tokenizePos

      public Span[] tokenizePos(String text)
      Finds the boundaries of atomic parts in a string.

      Reports the segmented spans in original text coordinates, whitespace omitted.

      Specified by:
      tokenizePos in interface Tokenizer
      Parameters:
      text - The string to be tokenized.
      Returns:
      The spans (offsets into s) for each token as the individuals array elements.
      Throws:
      IllegalArgumentException - Thrown if text is null.
      IllegalStateException - Thrown if the dictionary offers no candidate at some position; see analyze(String).
    • decode

      private void decode(String text, int from, int to, List<Morpheme> morphemes)
      Runs the Viterbi search over one whitespace-free stretch of text.
      Parameters:
      text - The text being segmented.
      from - The stretch start.
      to - The exclusive stretch end.
      morphemes - Receives the cheapest path's morphemes, in text order.
      Throws:
      IllegalStateException - Thrown if no path reaches the end of the stretch.
    • relax

      private void relax(LatticeTokenizer.Node candidate, LatticeTokenizer.Node predecessors)
      Connects a candidate to the cheapest predecessor ending where it starts.
      Parameters:
      candidate - The node to give a path cost and a predecessor.
      predecessors - The head of the chain of nodes ending where the candidate starts, or null when it starts at the stretch start.
    • computeCategoryRuns

      private void computeCategoryRuns(String text, int from, int to, CategoryTable.CategoryAssignment[] categoryAt, int[] runEndAt)
      Computes the per-position categories and connected-category run end for one stretch, in one right-to-left pass over the code points. Positions inside a surrogate sequence have a null assignment; candidates do not start there.
      Parameters:
      text - The text being segmented.
      from - The stretch start.
      to - The exclusive stretch end.
      categoryAt - Receives each position's categories, indexed by position - from.
      runEndAt - Receives each position's exclusive connected-category run end, indexed the same way.
    • candidates

      private void candidates(String text, int from, int to, int offset, CategoryTable.CategoryAssignment positionCategories, int positionRunEnd, List<LatticeTokenizer.Node> candidates)
      Gathers lexicon matches and unknown-word candidates starting at one position.
      Parameters:
      text - The text being segmented.
      from - The stretch start.
      to - The exclusive stretch end, which no candidate may reach past.
      offset - The candidate start, relative to from.
      positionCategories - The categories of that position, or null for a position inside a surrogate sequence.
      positionRunEnd - The exclusive end of the overlapping-category run starting there. Used only when positionCategories is not null.
      candidates - Receives the candidates. Must be empty on entry.
      Throws:
      IllegalStateException - Thrown if neither the lexicon, the position's category, nor the DEFAULT template offers a candidate.
    • addUnknown

      private void addUnknown(List<LatticeTokenizer.Node> candidates, String text, int position, int runEnd, MecabDictionary.Category category, List<MecabDictionary.WordEntry> templates)
      Emits unknown-word candidates per the category's grouping and length settings.

      Candidates remain inside a run connected by overlapping category assignments. Lengths count code points, not UTF-16 code units.

      Parameters:
      candidates - Receives the candidates.
      text - The text being segmented.
      position - The position the candidates start at.
      runEnd - The exclusive end of the connected-category run starting at position.
      category - The category of that run.
      templates - The category's unknown-word templates.