Class FeedforwardNetwork.Serializer

java.lang.Object
opennlp.tools.depparse.FeedforwardNetwork.Serializer
All Implemented Interfaces:
ArtifactSerializer<FeedforwardNetwork>
Enclosing class:
FeedforwardNetwork

static final class FeedforwardNetwork.Serializer extends Object implements ArtifactSerializer<FeedforwardNetwork>
Reads and writes a network as a FeedforwardDependencyModel artifact: a four-byte magic, an int format version, the three vocabularies, the transition inventory, the embedding size, and the weight matrices and vectors. Symbols are stored as an int byte length followed by UTF-8 bytes; a symbol may have up to ResourceLimits.MAX_ENTRIES bytes.

Counts read from the stream are bounded by ResourceLimits.MAX_ENTRIES, and the float values of all matrices and vectors together by ResourceLimits.MAX_MATRIX_CELLS; matrix rows are allocated one at a time while reading.

  • Field Details

    • MAGIC

      static final byte[] MAGIC
      The bytes every serialized network starts with.
    • FORMAT_VERSION

      static final int FORMAT_VERSION
      The format version this serializer reads and writes.
      See Also:
  • Constructor Details

    • Serializer

      Serializer()
  • Method Details

    • create

      public FeedforwardNetwork create(InputStream in) throws IOException
      Creates an artifact from the provided InputStream.

      Note: The InputStream remains open.

      Specified by:
      create in interface ArtifactSerializer<FeedforwardNetwork>
      Parameters:
      in - A valid, open InputStream ready to read from.
      Returns:
      A valid artifact.
      Throws:
      InvalidFormatException - Thrown if the content is not a network in a supported version, a count or symbol length is out of range, the embedding size or hidden layer width is zero, the structure is inconsistent, a value is not finite, or the stream ends early. Malformed UTF-8 in a symbol is replaced, not rejected.
      IOException - Thrown if IO errors occurred during creation.
    • read

      private static FeedforwardNetwork read(DataInputStream data) throws IOException
      Reads the network body after any wrapping has been set up.
      Parameters:
      data - The input to read from.
      Returns:
      The network. Never null.
      Throws:
      IOException - Thrown if reading fails or the content is invalid.
    • serialize

      public void serialize(FeedforwardNetwork artifact, OutputStream out) throws IOException
      Serializes an artifact to the provided OutputStream.

      Note: The OutputStream remains open.

      Specified by:
      serialize in interface ArtifactSerializer<FeedforwardNetwork>
      Parameters:
      artifact - A valid artifact.
      out - A valid, open OutputStream ready to write to.
      Throws:
      IllegalArgumentException - Thrown if artifact or out is null.
      IOException - Thrown if IO errors occurred during serialization.
    • writeVocabulary

      private static void writeVocabulary(DataOutputStream data, Map<String,Integer> ids) throws IOException
      Writes one vocabulary as its size followed by (symbol, id) pairs in ascending id order, so that serializing the same network yields the same bytes on every JVM.
      Parameters:
      data - The output to write to.
      ids - The vocabulary to write.
      Throws:
      IOException - Thrown if writing fails.
    • readVocabulary

      private static Map<String,Integer> readVocabulary(DataInputStream data, String label, long[] remainingEntries) throws IOException
      Reads one vocabulary written by writeVocabulary(java.io.DataOutputStream, java.util.Map<java.lang.String, java.lang.Integer>). The map grows while reading instead of being sized from the header.
      Parameters:
      data - The input to read from.
      label - The vocabulary name used in error messages.
      remainingEntries - The one-element budget of vocabulary entries across all vocabularies, decremented in place.
      Returns:
      The symbol to id map. Never null.
      Throws:
      IOException - Thrown if reading fails, the size exceeds the budget, or a symbol repeats.
    • writeSymbol

      private static void writeSymbol(DataOutputStream data, String symbol) throws IOException
      Writes a symbol as its UTF-8 byte length followed by the bytes.
      Parameters:
      data - The output to write to.
      symbol - The symbol to write.
      Throws:
      IOException - Thrown if writing fails.
    • readSymbol

      private static String readSymbol(DataInputStream data, String label) throws IOException
      Parameters:
      data - The input to read from.
      label - The symbol name used in error messages.
      Returns:
      The symbol. Never null.
      Throws:
      IOException - Thrown if reading fails or the length is out of range.
    • writeMatrix

      private static void writeMatrix(DataOutputStream data, float[][] matrix) throws IOException
      Writes a rectangular matrix as its dimensions followed by its values in row order.
      Parameters:
      data - The output to write to.
      matrix - The matrix to write.
      Throws:
      IOException - Thrown if writing fails.
    • readMatrix

      private static float[][] readMatrix(DataInputStream data, long expectedRows, long expectedColumns, long[] remainingCells, String label) throws IOException
      Reads a matrix written by writeMatrix(java.io.DataOutputStream, float[][]), allocating one row at a time.
      Parameters:
      data - The input to read from.
      expectedRows - The required row count, or a negative value to accept any positive count within the limits.
      expectedColumns - The required column count.
      remainingCells - The one-element float budget, decremented in place.
      label - The matrix name used in error messages.
      Returns:
      The matrix. Never null.
      Throws:
      IOException - Thrown if reading fails, a dimension is out of range or does not match, the budget is exceeded, or a value is not finite.
    • writeVector

      private static void writeVector(DataOutputStream data, float[] vector) throws IOException
      Writes a vector as its length followed by its values.
      Parameters:
      data - The output to write to.
      vector - The vector to write.
      Throws:
      IOException - Thrown if writing fails.
    • readVector

      private static float[] readVector(DataInputStream data, long expectedLength, long[] remainingCells, String label) throws IOException
      Parameters:
      data - The input to read from.
      expectedLength - The required length.
      remainingCells - The one-element float budget, decremented in place.
      label - The vector name used in error messages.
      Returns:
      The vector. Never null.
      Throws:
      IOException - Thrown if reading fails, the length does not match, the budget is exceeded, or a value is not finite.
    • readCount

      private static int readCount(DataInputStream data, String label) throws IOException
      Reads a count and checks it against ResourceLimits.MAX_ENTRIES.
      Parameters:
      data - The input to read from.
      label - The count name used in error messages.
      Returns:
      The count, never negative.
      Throws:
      IOException - Thrown if reading fails or the value is out of range.
    • reserveCells

      private static void reserveCells(long[] remaining, long count, String label) throws IOException
      Reserves float cells before allocating a matrix or vector.
      Parameters:
      remaining - The one-element budget, decremented in place.
      count - The number of values about to be allocated.
      label - The structure name used in error messages.
      Throws:
      IOException - Thrown if count exceeds the remaining budget.
    • readFiniteFloat

      private static float readFiniteFloat(DataInputStream data, String label) throws IOException
      Reads one weight.
      Parameters:
      data - The input to read from.
      label - The structure name used in error messages.
      Returns:
      The weight.
      Throws:
      IOException - Thrown if reading fails or the value is NaN or infinite.