Skip to content

Commit 1412923

Browse files
committed
Merge refreshed PR apache#1165 into OPENNLP-1833 gRPC helper
# Conflicts: # opennlp-api/src/main/java/opennlp/tools/tokenize/BertTokenizer.java # opennlp-api/src/main/java/opennlp/tools/tokenize/SubwordPiece.java # opennlp-api/src/main/java/opennlp/tools/tokenize/SubwordTokenizer.java # opennlp-api/src/main/java/opennlp/tools/tokenize/WordpieceEncoder.java # opennlp-api/src/main/java/opennlp/tools/tokenize/WordpieceTokenizer.java # opennlp-core/opennlp-ml/opennlp-dl/src/main/java/opennlp/dl/AbstractDL.java # opennlp-core/opennlp-ml/opennlp-dl/src/main/java/opennlp/dl/vectors/SentenceVectorsDL.java # opennlp-core/opennlp-ml/opennlp-dl/src/test/java/opennlp/dl/CreateTokenizerTest.java # opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/tokenize/BertTokenizerTest.java # opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/tokenize/WordpieceEncoderReferenceSequencesTest.java # opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/tokenize/WordpieceEncoderTest.java
2 parents 9469221 + 141160e commit 1412923

18 files changed

Lines changed: 1154 additions & 807 deletions

File tree

opennlp-api/src/main/java/opennlp/tools/tokenize/BertNormalization.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
final class BertNormalization {
2626

27+
/** Default maximum word length used by BERT wordpiece tokenizers. */
28+
static final int DEFAULT_MAX_WORD_CODE_POINTS = 100;
29+
2730
private BertNormalization() {
2831
}
2932

@@ -70,6 +73,12 @@ static boolean isWhitespace(int codePoint) {
7073
return Character.getType(codePoint) == Character.SPACE_SEPARATOR;
7174
}
7275

76+
/** Tests whether a code point is a Unicode line or paragraph separator. */
77+
static boolean isLineOrParagraphSeparator(int codePoint) {
78+
final int type = Character.getType(codePoint);
79+
return type == Character.LINE_SEPARATOR || type == Character.PARAGRAPH_SEPARATOR;
80+
}
81+
7382
/**
7483
* A punctuation character in the BERT sense: any non-alphanumeric ASCII
7584
* character that is not whitespace, or any Unicode punctuation category.

opennlp-api/src/main/java/opennlp/tools/tokenize/BertTokenizer.java

Lines changed: 0 additions & 116 deletions
This file was deleted.

opennlp-api/src/main/java/opennlp/tools/tokenize/SubwordPiece.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@
1616
*/
1717
package opennlp.tools.tokenize;
1818

19-
import opennlp.tools.util.Span;
20-
2119
/**
22-
* One subword unit produced by a {@link SubwordTokenizer}, with its vocabulary representation
20+
* One subword unit produced by a {@link SubwordTokenizer}, including the model representation
2321
* and source range.
2422
*
25-
* <p>The piece string is in the tokenizer's normalized form, so it is generally not a substring of
26-
* the input. {@code start} and {@code end} are UTF-16 offsets into the original text, so the
27-
* surface that produced this piece is {@code text.subSequence(start, end)}. Pieces without source
28-
* text, such as control symbols or the fill bytes of a byte-fallback expansion,
29-
* report an empty span with {@code start == end}.</p>
23+
* <p>The piece string is in the tokenizer's normalized form and need not equal the input.
24+
* {@code start} and {@code end} are UTF-16 offsets into the original text, so the
25+
* surface associated with this piece is {@code text.subSequence(start, end)}. A span can include
26+
* adjacent source characters when normalization reorders characters. Pieces without source text,
27+
* such as control symbols, report an empty span with {@code start == end}.</p>
3028
*
3129
* @param piece The piece in the vocabulary's normalized form; must not be {@code null} or empty.
3230
* @param id The non-negative vocabulary id of the piece.
3331
* @param start The inclusive start offset in the original text.
34-
* @param end The exclusive end offset in the original text; not less than {@code start}.
32+
* @param end The exclusive end offset in the original text; at least {@code start}.
33+
* @since 3.0.0
3534
*/
3635
public record SubwordPiece(String piece, int id, int start, int end) {
3736

3837
/**
3938
* Instantiates a {@link SubwordPiece}.
4039
*
41-
* @throws IllegalArgumentException Thrown if {@code piece} is {@code null} or empty, {@code id} is
42-
* negative, or the span is negative or inverted.
40+
* @throws IllegalArgumentException Thrown if {@code piece} is {@code null} or empty,
41+
* {@code id} is negative, or the span is negative or inverted.
4342
*/
4443
public SubwordPiece {
45-
if (piece == null || piece.isEmpty()) {
46-
throw new IllegalArgumentException("piece must not be null or empty");
44+
if (piece == null) {
45+
throw new IllegalArgumentException("piece must not be null");
46+
}
47+
if (piece.isEmpty()) {
48+
throw new IllegalArgumentException("piece must not be empty");
4749
}
4850
if (id < 0) {
4951
throw new IllegalArgumentException("id must not be negative");
@@ -52,12 +54,8 @@ public record SubwordPiece(String piece, int id, int start, int end) {
5254
throw new IllegalArgumentException("start must not be negative");
5355
}
5456
if (end < start) {
55-
throw new IllegalArgumentException("end must not be less than start");
57+
throw new IllegalArgumentException("end must be at least start");
5658
}
5759
}
5860

59-
/** {@return the original-text span of this piece as a {@link Span}} */
60-
public Span span() {
61-
return new Span(start, end);
62-
}
6361
}

opennlp-api/src/main/java/opennlp/tools/tokenize/SubwordTokenizer.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@
1919
import java.util.List;
2020

2121
/**
22-
* Splits text into subword units against a fixed vocabulary, reporting for every unit its
23-
* vocabulary id and the exact span of the original text it covers.
22+
* Splits text into subword units from a fixed model vocabulary, reporting the model id and
23+
* original-text span for each unit.
2424
*
25-
* <p>The segmentation is vocabulary-driven rather than linguistic, and each piece is in the
26-
* model's normalized form, so a piece is generally not a substring of the input. The offsets in
27-
* each {@link SubwordPiece} always refer to the caller's original text.</p>
25+
* <p>Segmentation follows model entries, not linguistic token boundaries. Each piece is in the
26+
* model's normalized form and need not equal the input. Offsets in each {@link SubwordPiece}
27+
* refer to the original input text.</p>
28+
*
29+
* <p>An implementation may include model control pieces with empty source spans. Their presence
30+
* and placement are part of that tokenizer's contract, not this interface.</p>
2831
*
2932
* <p>Thread safety is implementation specific.</p>
33+
*
34+
* @since 3.0.0
3035
*/
3136
public interface SubwordTokenizer {
3237

3338
/**
3439
* Encodes text into subword pieces.
3540
*
3641
* @param text The text to encode; must not be {@code null}.
37-
* @return The pieces in text order; empty when no units can be encoded.
42+
* @return The pieces in model order; may be empty.
3843
* @throws IllegalArgumentException Thrown if {@code text} is {@code null}.
3944
*/
4045
List<SubwordPiece> encode(CharSequence text);
@@ -43,7 +48,7 @@ public interface SubwordTokenizer {
4348
* Encodes text into vocabulary ids.
4449
*
4550
* @param text The text to encode; must not be {@code null}.
46-
* @return The ids in text order; empty when no units can be encoded.
51+
* @return The ids from {@link #encode(CharSequence)}, in the same order.
4752
* @throws IllegalArgumentException Thrown if {@code text} is {@code null}.
4853
*/
4954
default int[] encodeToIds(CharSequence text) {
@@ -59,7 +64,7 @@ default int[] encodeToIds(CharSequence text) {
5964
* Encodes text into piece strings in the vocabulary's normalized form.
6065
*
6166
* @param text The text to encode; must not be {@code null}.
62-
* @return The pieces in text order; empty when no units can be encoded.
67+
* @return The piece strings from {@link #encode(CharSequence)}, in the same order.
6368
* @throws IllegalArgumentException Thrown if {@code text} is {@code null}.
6469
*/
6570
default String[] encodeToPieces(CharSequence text) {

0 commit comments

Comments
 (0)