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

  • Constructor Details

    • BaseModel

      private BaseModel(String componentName, boolean isLoadedFromSerialized)
    • BaseModel

      protected BaseModel(String componentName, String languageCode, Map<String,String> manifestInfoEntries, BaseToolFactory factory)
      Initializes a BaseModel instance. The subclass constructor should call the method checkArtifactMap() to check the artifact map is in a valid state.

      Subclasses will have access to custom artifacts and serializers provided by the specified factory.

      Parameters:
      componentName - The component name to create the model for.
      languageCode - The ISO language code to configure. Must not be null.
      manifestInfoEntries - Mapping for additional information in the manifest.
      factory - The factory to use.
    • BaseModel

      protected BaseModel(String componentName, String languageCode, Map<String,String> manifestInfoEntries)
      Initializes a BaseModel instance. The subclass constructor should call the method checkArtifactMap() to check the artifact map is in a valid state.
      Parameters:
      componentName - The component name to create the model for.
      languageCode - The ISO language code to configure. Must not be null.
      manifestInfoEntries - Mapping for additional information in the manifest.
    • BaseModel

      protected BaseModel(String componentName, InputStream in) throws IOException
      Initializes a BaseModel instance. The subclass constructor should call the method checkArtifactMap() to check the artifact map is in a valid state.
      Parameters:
      componentName - The component name to create the model for.
      in - A valid, open InputStream to read the model from.
      Throws:
      IOException - Thrown if IO errors occurred.
    • BaseModel

      protected BaseModel(String componentName, File modelFile) throws IOException
      Initializes a BaseModel instance. The subclass constructor should call the method checkArtifactMap() to check the artifact map is in a valid state.
      Parameters:
      componentName - The component name to create the model for.
      modelFile - A valid, accessible File to read the model from.
      Throws:
      IOException - Thrown if IO errors occurred.
    • BaseModel

      protected BaseModel(String componentName, Path modelPath) throws IOException
      Initializes a BaseModel instance. The subclass constructor should call the method checkArtifactMap() to check the artifact map is in a valid state.
      Parameters:
      componentName - The component name to create the model for.
      modelPath - A valid, accessible Path to read the model from.
      Throws:
      IOException - Thrown if IO errors occurred.
    • BaseModel

      protected BaseModel(String componentName, URL modelURL) throws IOException
      Initializes a BaseModel instance. The subclass constructor should call the method checkArtifactMap() to check the artifact map is in a valid state.
      Parameters:
      componentName - The component name to create the model for.
      modelURL - A valid, accessible URL to read the model from.
      Throws:
      IOException - Thrown if IO errors occurred.
  • Method Details

    • loadModel

      private void loadModel(InputStream in) throws IOException
      Throws:
      IOException
    • initializeFactory

      private void initializeFactory() throws InvalidFormatException
      Throws:
      InvalidFormatException
    • getDefaultFactory

      protected Class<? extends BaseToolFactory> getDefaultFactory()
      Subclasses should override this method if their module has a default BaseToolFactory subclass.
      Returns:
      The default BaseToolFactory for the component, or null if none.
    • loadArtifactSerializers

      private void loadArtifactSerializers()
    • finishLoadingArtifacts

      private void finishLoadingArtifacts(InputStream in) throws IOException
      Finishes loading the artifacts now that it knows all serializers.
      Throws:
      IOException
    • getEntryExtension

      private String getEntryExtension(String entry) throws InvalidFormatException
      Extracts the "." extension from an entry name.
      Parameters:
      entry - The entry name which contains the extension
      Returns:
      The extension.
      Throws:
      InvalidFormatException - Thrown if no extension can be extracted
    • getArtifactSerializer

      protected ArtifactSerializer<?> getArtifactSerializer(String resourceName)
      Parameters:
      resourceName - The identifying name of a resource to retrieve an {link ArtifactSerializer} for.
      Returns:
      Retrieves an artifact serialize.
    • createArtifactSerializers

      protected static Map<String, ArtifactSerializer<?>> createArtifactSerializers()
      Creates and registers default artifact serializes.
      Returns:
      A Map with all registered artifact serializes.
    • createArtifactSerializers

      protected void createArtifactSerializers(Map<String, ArtifactSerializer<?>> serializers)
      Registers all ArtifactSerializer for their artifact file name extensions. The registered serializers are used to create and serialize resources in the model package.

      Override this method to register custom serializers.

      Note: Subclasses should generally invoke super.createArtifactSerializers at the beginning of this method.

      This method is called during construction.

      Parameters:
      serializers - The key of the map is the file extension used to look up an ArtifactSerializer.
    • createBaseArtifactSerializers

      private void createBaseArtifactSerializers(Map<String, ArtifactSerializer<?>> serializers)
    • validateArtifactMap

      protected void validateArtifactMap() throws InvalidFormatException
      Validates the parsed artifacts. If something is not valid, subclasses should throw an InvalidFormatException.

      Note: Subclasses should generally invoke super.validateArtifactMap at the beginning of this method.

      Throws:
      InvalidFormatException - Thrown if artifacts were found to be inconsistent.
    • checkArtifactMap

      protected void checkArtifactMap()
      Checks the artifact map.

      A subclass should call this method from a constructor which accepts the individual artifact map items, to validate that these items form a valid model.

      Throws:
      IllegalArgumentException - Thrown if the artifacts are not valid.
      IllegalStateException - Thrown if finishLoadingArtifacts(InputStream) was not called by a subclass.
    • 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.
    • setManifestProperty

      protected final void setManifestProperty(String key, String value)
      Sets a given value for a given key to the manifest.properties mapping.
      Parameters:
      key - The identifying key.
      value - The value to set at key.
    • 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.
    • writeObject

      private void writeObject(ObjectOutputStream out) throws IOException
      Throws:
      IOException
    • 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 an allowlist 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(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 the allowlist filter.
      ClassNotFoundException - Thrown if required classes are not found.
    • readObject

      private void readObject(ObjectInputStream in) throws IOException
      Throws:
      IOException
    • skipEntryForSerialization

      protected boolean skipEntryForSerialization(Map.Entry<String,Object> entry)
      Parameters:
      entry - the entry to check
      Returns:
      true, if the given entry should be skipped for serialization.