Class AbstractModelReader

java.lang.Object
opennlp.tools.ml.model.AbstractModelReader
Direct Known Subclasses:
GenericModelReader, GISModelReader, NaiveBayesModelReader, PerceptronModelReader

public abstract class AbstractModelReader extends Object
An abstract, basic implementation of a model reader.
  • Field Details

    • MAX_ENTRIES_PROPERTY

      public static final String MAX_ENTRIES_PROPERTY
      System property for overriding the maximum number of entries (outcomes, predicates, outcome patterns, chunk counts) that may be read from a model file or training data. Set at JVM startup, e.g. -DOPENNLP_MAX_ENTRIES=5000000. Falls back to 10_000_000 if absent or invalid.
      See Also:
    • MAX_ENTRIES

      static final int MAX_ENTRIES
      Upper bound on count fields read from a model file. Prevents OOM on crafted inputs with oversized array size declarations. Configurable via the MAX_ENTRIES_PROPERTY system property.
    • NUM_PREDS

      protected int NUM_PREDS
      The number of predicates contained in a model.
    • dataReader

      protected DataReader dataReader
  • Constructor Details

  • Method Details

    • initMaxEntries

      private static int initMaxEntries()
    • readInt

      public int readInt() throws IOException
      Implement as needed for the format the model is stored in.
      Returns:
      Reads in an int value from the underlying DataReader.
      Throws:
      IOException
    • readDouble

      public double readDouble() throws IOException
      Implement as needed for the format the model is stored in.
      Returns:
      Reads in a double value from the underlying DataReader.
      Throws:
      IOException
    • readUTF

      public String readUTF() throws IOException
      Implement as needed for the format the model is stored in.
      Returns:
      Reads in an UTF-encoded String value from the underlying DataReader.
      Throws:
      IOException
    • getModel

      public AbstractModel getModel() throws IOException
      Returns:
      Retrieves the read AbstractModel instance.
      Throws:
      IOException - Thrown if IO errors occurred constructing the model.
    • checkModelType

      public abstract void checkModelType() throws IOException
      Checks the model type via the the underlying DataReader.
      Throws:
      IOException - Thrown if IO errors occurred checking the model type.
    • constructModel

      public abstract AbstractModel constructModel() throws IOException
      Constructs a model.
      Returns:
      A AbstractModel reconstructed from a model's (read) attributes.
      Throws:
      IOException - Thrown if IO errors occurred during (re-)construction.
    • getOutcomes

      protected String[] getOutcomes() throws IOException
      Returns:
      Reads and retrieves the outcome labels from the model.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalArgumentException - Thrown if the outcome count is negative or exceeds MAX_ENTRIES.
    • getOutcomePatterns

      protected int[][] getOutcomePatterns() throws IOException
      Returns:
      Reads and retrieves the outcome patterns from the model.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalArgumentException - Thrown if the outcome pattern count is negative or exceeds MAX_ENTRIES.
    • getPredicates

      protected String[] getPredicates() throws IOException
      Returns:
      Reads and retrieves the predicates from the model.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalArgumentException - Thrown if the predicate count is negative or exceeds MAX_ENTRIES.
    • getParameters

      protected Context[] getParameters(int[][] outcomePatterns) throws IOException
      Reads the parameters from a file and populates an array of Context objects.
      Parameters:
      outcomePatterns - The outcome patterns for the model. The first index refers to which outcome pattern (a set of outcomes that occurs with a context) is being specified. The second index specifies the number of contexts which use this pattern at index 0, and the index of each outcome which make up this pattern in indices 1-n.
      Returns:
      An array of Context objects.
      Throws:
      IOException - Thrown when the model file does not match the outcome patterns or can not be read.