You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by kr...@apache.org on 2026/09/04 00:40:56 UTC
(opennlp) 08/08: Merge refreshed PR #1165 into OPENNLP-1833 gRPC helper
This is an automated email from the ASF dual-hosted git repository.
krickert pushed a commit to branch OPENNLP-1833-grpc-helper
in repository https://gitbox.apache.org/repos/asf/opennlp.git
commit 2b5ebbc9d7fe3f60fa2f9fdb0de29a4f227e95e7
Merge: 5695d5b49 96d2781cb
Author: Kristian Rickert <kr...@apache.org>
AuthorDate: Thu Sep 3 19:29:41 2026 -0400
Merge refreshed PR #1165 into OPENNLP-1833 gRPC helper
.../java/opennlp/tools/tokenize/BertTokenizer.java | 25 ++--
.../java/opennlp/tools/tokenize/SubwordPiece.java | 27 ++--
.../opennlp/tools/tokenize/SubwordTokenizer.java | 22 +--
.../opennlp/tools/tokenize/WordpieceEncoder.java | 151 +++++++++------------
.../src/main/java/opennlp/dl/AbstractDL.java | 7 +-
.../opennlp/tools/tokenize/BertTokenizerTest.java | 4 +-
.../WordpieceEncoderReferenceSequencesTest.java | 2 +-
.../tools/tokenize/WordpieceEncoderTest.java | 66 +++++----
opennlp-docs/src/docbkx/tokenizer.xml | 49 +++++++
9 files changed, 199 insertions(+), 154 deletions(-)
diff --cc opennlp-docs/src/docbkx/tokenizer.xml
index 9a513f795,80cc11b3f..418275209
--- a/opennlp-docs/src/docbkx/tokenizer.xml
+++ b/opennlp-docs/src/docbkx/tokenizer.xml
@@@ -538,49 -538,54 +538,98 @@@ wordTokenizer.tokenize("The quick brow
</programlisting>
</para>
</section>
+ <section xml:id="tools.tokenizer.lattice">
+ <title>Lattice tokenization for CJK</title>
+ <para>
+ Languages written without spaces need a dictionary-backed segmenter.
+ <code>LatticeTokenizer</code> scores paths over a MeCab-format dictionary and
+ emits the cheapest segmentation with spans in original text coordinates.
+ <code>UnigramSegmenter</code> does the same from a plain frequency lexicon.
+ Install a dictionary archive with <code>MecabDictionaryInstaller</code>, load it
+ as a <code>MecabDictionary</code>, and tokenize. Remote archives require an
+ expected SHA-512 digest; an application-supplied opt-in catalog
+ (<code>installFromCatalog</code>) also needs
+ <code>-Dopennlp.download.remote=true</code>.
+ <code>matrix.def</code> must list a cost for every declared cell; matrix
+ dimensions plus lexicon size are bounded by the shared
+ <code>ResourceLimits.MAX_ENTRIES</code> limit and the matrix cell count
+ by <code>ResourceLimits.MAX_MATRIX_CELLS</code>.
+ Fetching, verification, and unpacking go through the bounded
+ <code>ResourceInstaller</code> path described in the model loading chapter,
+ whose ceilings default to a 1 GiB download, 4 GiB unpacked, and 100000
+ archive entries and can be raised at JVM startup via
+ <code>opennlp.download.max.bytes</code>,
+ <code>opennlp.install.max.total.bytes</code>, and
+ <code>opennlp.install.max.entries</code> for larger dictionaries such as
+ UniDic. Installed files are flattened to their base names; a file that
+ already exists in the target is refused, so refreshing a dictionary means
+ removing its old files first.
+ Load also rejects an <code>unk.def</code> template for a category
+ <code>char.def</code> never defined, and accepts MeCab-quoted CSV fields.
+ <code>LatticeUsageExampleTest</code> asserts the install-load-tokenize and
+ lexicon flows shown here.
+ <programlisting language="java">
+<![CDATA[MecabDictionaryInstaller.install(archiveUri, dictionaryDirectory, sha512);
+LatticeTokenizer tokenizer =
+ new LatticeTokenizer(MecabDictionary.load(dictionaryDirectory));
+
+String[] tokens = tokenizer.tokenize(text);
+Span[] spans = tokenizer.tokenizePos(text);
+List<Morpheme> morphemes = tokenizer.analyze(text);
+
+UnigramSegmenter segmenter = UnigramSegmenter.load(Path.of("words.txt"));
+String[] words = segmenter.tokenize(text);]]>
+ </programlisting>
+ </para>
+ </section>
</section>
+ <section xml:id="tools.tokenizer.subword">
+ <title>Subword Tokenization</title>
+ <para>
+ Neural models commonly use subword units from a fixed vocabulary. The
+ <code>SubwordTokenizer</code> interface encodes text as <code>SubwordPiece</code> values.
+ Each value contains the normalized piece string, vocabulary id, and UTF-16 span in the
+ original text. The span remains valid when model normalization changes the piece text.
+ <code>encodeToIds</code> and <code>encodeToPieces</code> return only the ids or strings.
+ Thread safety is implementation specific.
+ </para>
+ <para>
+ The interface, <code>SubwordPiece</code>, and vocabulary-based
+ <code>WordpieceEncoder</code> are in <code>opennlp-api</code>. Implementations that read
+ trained model formats can implement the same contract. The SentencePiece implementation is in
+ <link xlink:href="https://github.com/apache/opennlp-addons">opennlp-addons</link>
+ and does not bundle a model.
+ </para>
+ <para>
+ A piece without source text, such as a control symbol, has an empty span.
+ <code>SubwordPiece.span</code> returns the range as a <code>Span</code>.
+ </para>
+ <section xml:id="tools.tokenizer.subword.wordpiece">
+ <title>WordPiece</title>
+ <para>
+ <code>WordpieceEncoder</code> in <code>opennlp.tools.tokenize</code> runs the full
+ BERT tokenization pipeline: control character removal, whitespace normalization, CJK
+ isolation, optional lower casing with accent stripping, punctuation isolation, and
+ greedy longest-match wordpiece segmentation. It is constructed from the model's
+ vocabulary, conventionally the lines of a <code>vocab.txt</code> file, where a piece's
+ id is its line number.
+ <programlisting language="java">
+ <![CDATA[List<String> vocabulary = Files.readAllLines(Path.of("vocab.txt"));
+
+ WordpieceEncoder encoder = new WordpieceEncoder(vocabulary); // uncased model
+ WordpieceEncoder cased = new WordpieceEncoder(vocabulary, false); // cased model
+
+ List<SubwordPiece> pieces = encoder.encode("OpenNLP encodes text for BERT models.");
+ int[] ids = encoder.encodeToIds("OpenNLP encodes text for BERT models.");]]>
+ </programlisting>
+ Each encoding is framed by the classification and separator pieces, which use empty
+ spans at the text's boundaries, and words the vocabulary cannot cover become the
+ unknown piece. Models with other special tokens or with non-contiguous ids are
+ supported through the constructors taking explicit special tokens or a piece-to-id
+ map. <code>BertTokenizer</code> is deprecated and delegates tokenization to
+ <code>WordpieceEncoder</code>. New code can pass the vocabulary as a <code>List</code>
+ or a piece-to-id map and call <code>encodeToPieces</code> for the piece sequence.
+ </para>
+ </section>
+ </section>
</chapter>