Class BaseModel

java.lang.Object
opennlp.tools.util.model.BaseModel
All Implemented Interfaces:
Serializable, ArtifactProvider
Direct Known Subclasses:
ChunkerModel, DoccatModel, LanguageDetectorModel, LemmatizerModel, ParserModel, POSModel, SentenceModel, TokenizerModel, TokenNameFinderModel

public abstract class BaseModel extends Object implements ArtifactProvider, Serializable
This is a common base model which can be used by the components' specific model classes.
See Also:
  • Field Details

  • Method Details

    • getManifestProperty

      public final String getManifestProperty(String key)
      Specified by:
      getManifestProperty in interface ArtifactProvider
      Parameters:
      key - A string identifying an element.
      Returns:
      Retrieves the value for the given key from the manifest.properties.
    • getLanguage

      public final String getLanguage()
      Specified by:
      getLanguage in interface ArtifactProvider
      Returns:
      Retrieves the language code of the material which was used to train a model or 'x-unspecified' if non was set.
    • getVersion

      public final Version getVersion()
      Returns:
      Retrieves the OpenNLP Version which was used to create the model.
    • serialize

      public final void serialize(OutputStream out) throws IOException
      Serializes the model to the given OutputStream.
      Parameters:
      out - The OutputStream to write the model to.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalStateException - Thrown if loadArtifactSerializers() was not called in a subclass constructor.
    • serialize

      public final void serialize(File f) throws IOException
      Serializes the model to the specified File.
      Parameters:
      f - The write-accessible File to write the model to.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalStateException - Thrown if loadArtifactSerializers() was not called in a subclass constructor.
    • serialize

      public final void serialize(Path p) throws IOException
      Serializes the model to the specified Path.
      Parameters:
      p - The write-accessible Path to write the model to.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalStateException - Thrown if loadArtifactSerializers() was not called in a subclass constructor.
    • getArtifact

      public <T> T getArtifact(String key)
      Specified by:
      getArtifact in interface ArtifactProvider
      Returns:
      Gets an artifact by an identifying key or name.
    • isLoadedFromSerialized

      public boolean isLoadedFromSerialized()
      Description copied from interface: ArtifactProvider
      Indicates if this provider was loaded from a serialized form.

      It is useful, for example, during the validation of artifacts: Skip the time-consuming ones if those were already validated during the serialization process.

      Specified by:
      isLoadedFromSerialized in interface ArtifactProvider
      Returns:
      true if this model was loaded from a serialized form, false otherwise.
    • deserialize

      public static <T extends BaseModel> T deserialize(Class<T> modelClass, InputStream in) throws IOException, ClassNotFoundException
      Deserializes a BaseModel from the given InputStream using the standard Serializable mechanism, with DESERIALIZE_FILTER applied 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 calling new ObjectInputStream(in).readObject() directly, since a filter installed inside readObject(java.io.ObjectInputStream) cannot protect the top-level object - the JDK only allows an ObjectInputFilter to be set on a stream before the first object is read.

      Type Parameters:
      T - The type of the expected model.
      Parameters:
      modelClass - The expected BaseModel subclass.
      in - The InputStream to read from. Must not be null.
      Returns:
      A valid BaseModel instance of type T.
      Throws:
      IOException - Thrown if IO errors occurred, including a rejection by DESERIALIZE_FILTER.
      ClassNotFoundException - Thrown if required classes are not found.