Package opennlp.tools.util.model
Class BaseModel
java.lang.Object
opennlp.tools.util.model.BaseModel
- All Implemented Interfaces:
Serializable,opennlp.tools.util.model.ArtifactProvider
- Direct Known Subclasses:
BPEModel,ChunkerModel,DoccatModel,LanguageDetectorModel,LemmatizerModel,ParserModel,POSModel,SentenceModel,SentimentModel,TokenizerModel,TokenNameFinderModel
public abstract class BaseModel
extends Object
implements opennlp.tools.util.model.ArtifactProvider, Serializable
This is a common base model which can be used by the components' specific
model classes.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends BaseModel>
Tdeserialize(Class<T> modelClass, InputStream in) Deserializes aBaseModelfrom the givenInputStreamusing the standardSerializablemechanism, withDESERIALIZE_FILTERapplied to reject any class outside the allowlist - including the top-level object itself.<T> TgetArtifact(String key) final Stringfinal Stringfinal Versionbooleanfinal voidSerializes the model to the specifiedFile.final voidserialize(OutputStream out) Serializes the model to the givenOutputStream.final voidSerializes the model to the specifiedPath.
-
Field Details
-
TRAINING_CUTOFF_PROPERTY
- See Also:
-
TRAINING_ITERATIONS_PROPERTY
- See Also:
-
TRAINING_EVENTHASH_PROPERTY
- See Also:
-
-
Method Details
-
getManifestProperty
- Specified by:
getManifestPropertyin interfaceopennlp.tools.util.model.ArtifactProvider
-
getLanguage
- Specified by:
getLanguagein interfaceopennlp.tools.util.model.ArtifactProvider
-
getVersion
- Returns:
- Retrieves the OpenNLP
Versionwhich was used to create the model.
-
serialize
Serializes the model to the givenOutputStream.- Parameters:
out- TheOutputStreamto write the model to.- Throws:
IOException- Thrown if IO errors occurred.IllegalStateException- Thrown ifloadArtifactSerializers()was not called in a subclass constructor.
-
serialize
Serializes the model to the specifiedFile.- Parameters:
f- The write-accessibleFileto write the model to.- Throws:
IOException- Thrown if IO errors occurred.IllegalStateException- Thrown ifloadArtifactSerializers()was not called in a subclass constructor.
-
serialize
Serializes the model to the specifiedPath.- Parameters:
p- The write-accessiblePathto write the model to.- Throws:
IOException- Thrown if IO errors occurred.IllegalStateException- Thrown ifloadArtifactSerializers()was not called in a subclass constructor.
-
getArtifact
- Specified by:
getArtifactin interfaceopennlp.tools.util.model.ArtifactProvider
-
isLoadedFromSerialized
public boolean isLoadedFromSerialized()- Specified by:
isLoadedFromSerializedin interfaceopennlp.tools.util.model.ArtifactProvider
-
deserialize
public static <T extends BaseModel> T deserialize(Class<T> modelClass, InputStream in) throws IOException, ClassNotFoundException Deserializes aBaseModelfrom the givenInputStreamusing the standardSerializablemechanism, withDESERIALIZE_FILTERapplied to reject any class outside the allowlist - including the top-level object itself.Prefer this method (or the
BaseModel(String, InputStream|File|Path|URL)constructors) over callingnew ObjectInputStream(in).readObject()directly, since a filter installed insidereadObject(java.io.ObjectInputStream)cannot protect the top-level object - the JDK only allows anObjectInputFilterto be set on a stream before the first object is read.- Type Parameters:
T- The type of the expected model.- Parameters:
modelClass- The expectedBaseModelsubclass.in- TheInputStreamto read from. Must not benull.- Returns:
- A valid
BaseModelinstance of typeT. - Throws:
IOException- Thrown if IO errors occurred, including a rejection byDESERIALIZE_FILTER.ClassNotFoundException- Thrown if required classes are not found.
-