Class LatticeTokenizer
- All Implemented Interfaces:
Tokenizer
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classOne lattice node: a candidate morpheme with its best path cost so far. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe context id of the beginning and end of text.private final MecabDictionary -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddUnknown(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.Segments a text into morphemes with their dictionary features.private voidcandidates(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.private voidcomputeCategoryRuns(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.private voidRuns the Viterbi search over one whitespace-free stretch of text.private voidrelax(LatticeTokenizer.Node candidate, LatticeTokenizer.Node predecessors) Connects a candidate to the cheapest predecessor ending where it starts.String[]Splits a string into its atomic parts.Span[]tokenizePos(String text) Finds the boundaries of atomic parts in a string.
-
Field Details
-
BOUNDARY_CONTEXT
private static final int BOUNDARY_CONTEXTThe context id of the beginning and end of text.- See Also:
-
dictionary
-
-
Constructor Details
-
LatticeTokenizer
Initializes the tokenizer.- Parameters:
dictionary- The dictionary to segment with. Must not benull.- Throws:
IllegalArgumentException- Thrown ifdictionaryisnull.
-
-
Method Details
-
analyze
Segments a text into morphemes with their dictionary features.- Parameters:
text- The text to segment. Must not benull.- Returns:
- The morphemes in text order, spans in original coordinates, whitespace
omitted. Never
null; empty for empty or all-whitespace input. - Throws:
IllegalArgumentException- Thrown iftextisnull.IllegalStateException- Thrown if the dictionary offers no candidate at some position, which aunk.defwithout aDEFAULTtemplate does.
-
tokenize
Splits a string into its atomic parts.Reports the segmented surfaces, whitespace omitted.
- Specified by:
tokenizein interfaceTokenizer- Parameters:
text- The string to be tokenized.- Returns:
- The String[] with the individual tokens as the array elements.
- Throws:
IllegalArgumentException- Thrown iftextisnull.IllegalStateException- Thrown if the dictionary offers no candidate at some position; seeanalyze(String).
-
tokenizePos
Finds the boundaries of atomic parts in a string.Reports the segmented spans in original text coordinates, whitespace omitted.
- Specified by:
tokenizePosin interfaceTokenizer- Parameters:
text- The string to be tokenized.- Returns:
- The
spans (offsets intofor each token as the individuals array elements.s) - Throws:
IllegalArgumentException- Thrown iftextisnull.IllegalStateException- Thrown if the dictionary offers no candidate at some position; seeanalyze(String).
-
decode
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
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, ornullwhen 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 anullassignment; 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 byposition - 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 tofrom.positionCategories- The categories of that position, ornullfor a position inside a surrogate sequence.positionRunEnd- The exclusive end of the overlapping-category run starting there. Used only whenpositionCategoriesis notnull.candidates- Receives the candidates. Must be empty on entry.- Throws:
IllegalStateException- Thrown if neither the lexicon, the position's category, nor theDEFAULTtemplate 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 atposition.category- The category of that run.templates- The category's unknown-word templates.
-