Package opennlp.tools.tokenize
Class BPEModel
java.lang.Object
opennlp.tools.util.model.BaseModel
opennlp.tools.tokenize.BPEModel
- All Implemented Interfaces:
Serializable,ArtifactProvider
The
BPEModel stores learned BPE merge operations and can be
serialized and deserialized for reuse.
A model is created by the BPETokenizerTrainer and contains an ordered
list of BPETokenizer.SymbolPair merge operations that define the BPE
vocabulary. The model is persisted as a standard OpenNLP ZIP package with a
bpe.merges artifact containing the merge rules.
Usage:
// Create via training
BPETokenizerTrainer trainer = new BPETokenizerTrainer();
BPEModel model = trainer.train(corpus, 10000, "en");
// Save to disk
model.serialize(Path.of("bpe-en.bin"));
// Load from disk
BPEModel loaded = new BPEModel(Path.of("bpe-en.bin"));
// Use for tokenization
BPETokenizer tokenizer = new BPETokenizer(loaded);
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe component name for this model type.private static final longFields inherited from class opennlp.tools.util.model.BaseModel
artifactMap, FACTORY_NAME, MANIFEST_ENTRY, toolFactory, TRAINING_CUTOFF_PROPERTY, TRAINING_EVENTHASH_PROPERTY, TRAINING_ITERATIONS_PROPERTY -
Constructor Summary
ConstructorsConstructorDescriptionBPEModel(InputStream in) Initializes aBPEModelfrom anInputStream.BPEModel(List<BPETokenizer.SymbolPair> merges, Map<String, String> manifestInfoEntries, BPETokenizerFactory factory) Creates aBPEModelfrom trained merge rules. -
Method Summary
Modifier and TypeMethodDescriptionprotected Class<? extends BaseToolFactory> Subclasses should override this method if their module has a defaultBaseToolFactorysubclass.protected voidValidates the parsed artifacts.Methods inherited from class opennlp.tools.util.model.BaseModel
checkArtifactMap, createArtifactSerializers, createArtifactSerializers, deserialize, getArtifact, getArtifactSerializer, getLanguage, getManifestProperty, getVersion, isLoadedFromSerialized, serialize, serialize, serialize, setManifestProperty, skipEntryForSerialization
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
COMPONENT_NAME
The component name for this model type.- See Also:
-
-
Constructor Details
-
BPEModel
public BPEModel(List<BPETokenizer.SymbolPair> merges, Map<String, String> manifestInfoEntries, BPETokenizerFactory factory) Creates aBPEModelfrom trained merge rules.- Parameters:
merges- The ordered list of merge operations. Must not benull.manifestInfoEntries- Additional manifest info.factory- TheBPETokenizerFactory.
-
BPEModel
Initializes aBPEModelfrom anInputStream.- Parameters:
in- TheInputStreamfor loading the model.- Throws:
IOException- Thrown if IO errors occurred.
-
BPEModel
- Parameters:
modelFile- TheFilefor loading the model.- Throws:
IOException- Thrown if IO errors occurred.
-
BPEModel
- Parameters:
modelPath- ThePathfor loading the model.- Throws:
IOException- Thrown if IO errors occurred.
-
BPEModel
- Parameters:
modelURL- TheURLfor loading the model.- Throws:
IOException- Thrown if IO errors occurred.
-
-
Method Details
-
validateArtifactMap
Description copied from class:BaseModelValidates the parsed artifacts. If something is not valid, subclasses should throw anInvalidFormatException.Note: Subclasses should generally invoke
super.validateArtifactMapat the beginning of this method.- Overrides:
validateArtifactMapin classBaseModel- Throws:
InvalidFormatException- Thrown if artifacts were found to be inconsistent.
-
getDefaultFactory
Description copied from class:BaseModelSubclasses should override this method if their module has a defaultBaseToolFactorysubclass.- Overrides:
getDefaultFactoryin classBaseModel- Returns:
- The default
BaseToolFactoryfor the component, ornullif none.
-
getFactory
- Returns:
- The active
BPETokenizerFactory.
-
getMerges
- Returns:
- An unmodifiable, ordered list of BPE merge operations stored in this model.
-