Class AbstractModelReader
java.lang.Object
opennlp.tools.ml.model.AbstractModelReader
- Direct Known Subclasses:
GenericModelReader, GISModelReader, NaiveBayesModelReader, PerceptronModelReader
An abstract, basic implementation of a model reader.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DataReader(package private) static final intUpper bound on count fields read from a model file.static final StringSystem 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.protected intThe number of predicates contained in a model. -
Constructor Summary
ConstructorsConstructorDescriptionInitializes aAbstractModelReadervia aFile.AbstractModelReader(DataReader dataReader) Initializes aAbstractModelReadervia aDataReader. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidChecks the model type via the the underlyingDataReader.abstract AbstractModelConstructs amodel.getModel()protected int[][]protected String[]protected Context[]getParameters(int[][] outcomePatterns) Reads the parameters from a file and populates an array ofContextobjects.protected String[]private static intdoubleImplement as needed for the format the model is stored in.intreadInt()Implement as needed for the format the model is stored in.readUTF()Implement as needed for the format the model is stored in.
-
Field Details
-
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 to10_000_000if absent or invalid.- See Also:
-
MAX_ENTRIES
static final int MAX_ENTRIESUpper bound on count fields read from a model file. Prevents OOM on crafted inputs with oversized array size declarations. Configurable via theMAX_ENTRIES_PROPERTYsystem property. -
NUM_PREDS
protected int NUM_PREDSThe number of predicates contained in a model. -
dataReader
-
-
Constructor Details
-
AbstractModelReader
Initializes aAbstractModelReadervia aFile.- Parameters:
f- TheFilethat references the model to be read.- Throws:
IOException- Thrown if IO errors occurred.
-
AbstractModelReader
Initializes aAbstractModelReadervia aDataReader.- Parameters:
dataReader- TheDataReaderthat references the model to be read.
-
-
Method Details
-
initMaxEntries
private static int initMaxEntries() -
readInt
Implement as needed for the format the model is stored in.- Returns:
- Reads in an
intvalue from the underlyingDataReader. - Throws:
IOException
-
readDouble
Implement as needed for the format the model is stored in.- Returns:
- Reads in a
doublevalue from the underlyingDataReader. - Throws:
IOException
-
readUTF
Implement as needed for the format the model is stored in.- Returns:
- Reads in an
UTF-encoded Stringvalue from the underlyingDataReader. - Throws:
IOException
-
getModel
- Returns:
- Retrieves the read
AbstractModelinstance. - Throws:
IOException- Thrown if IO errors occurred constructing the model.
-
checkModelType
Checks the model type via the the underlyingDataReader.- Throws:
IOException- Thrown if IO errors occurred checking the model type.
-
constructModel
Constructs amodel.- Returns:
- A
AbstractModelreconstructed from a model's (read) attributes. - Throws:
IOException- Thrown if IO errors occurred during (re-)construction.
-
getOutcomes
- Returns:
- Reads and retrieves the
outcome labelsfrom the model. - Throws:
IOException- Thrown if IO errors occurred.IllegalArgumentException- Thrown if the outcome count is negative or exceedsMAX_ENTRIES.
-
getOutcomePatterns
- Returns:
- Reads and retrieves the
outcome patternsfrom the model. - Throws:
IOException- Thrown if IO errors occurred.IllegalArgumentException- Thrown if the outcome pattern count is negative or exceedsMAX_ENTRIES.
-
getPredicates
- Returns:
- Reads and retrieves the
predicatesfrom the model. - Throws:
IOException- Thrown if IO errors occurred.IllegalArgumentException- Thrown if the predicate count is negative or exceedsMAX_ENTRIES.
-
getParameters
Reads the parameters from a file and populates an array ofContextobjects.- 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 index0, and the index of each outcome which make up this pattern in indices1-n.- Returns:
- An array of
Contextobjects. - Throws:
IOException- Thrown when the model file does not match the outcome patterns or can not be read.
-