Class AbstractDL

java.lang.Object
opennlp.dl.AbstractDL
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
DocumentCategorizerDL, NameFinderDL, SentenceVectorsDL

public abstract class AbstractDL extends Object implements AutoCloseable
Base class for OpenNLP deep-learning classes using ONNX Runtime.
  • Field Details

  • Method Details

    • loadVocab

      public Map<String,Integer> loadVocab(File vocabFile) throws IOException
      Loads a vocabulary File from disk. A file with an opening brace as its first non-whitespace character is read as JSON, in one of two layouts: one object that maps each token to a non-negative integer ID, as in vocab.json, or a Hugging Face tokenizer.json of a WordPiece model, whose model.vocab object supplies the tokens and whose added_tokens absent from model.vocab are added with their ids. Any other file is read as plain text with one token per line, the line number being the ID, as in vocab.txt. An empty line in a plain text file is skipped but still counts toward the IDs of the lines after it; a line that holds only whitespace is a token. A byte order mark at the start of the file is not content in either format.
      Parameters:
      vocabFile - The vocabulary file.
      Returns:
      A map of vocabulary words to IDs.
      Throws:
      IOException - Thrown if the vocabulary file cannot be opened or read.
      opennlp.tools.util.InvalidFormatException - Thrown if a JSON vocabulary is malformed, contains a value that is not a non-negative integer, or is a tokenizer.json of another model type than WordPiece or with an added token that conflicts with the vocabulary. The message names the file and the offset, the token, or the unsupported member.
    • close

      public void close() throws ai.onnxruntime.OrtException
      Closes the ONNX OrtSession owned by this instance.

      The OrtEnvironment is deliberately not closed: OrtEnvironment.getEnvironment() returns a process-wide singleton shared by every deep-learning component, so closing it here would tear down the environment other live components still depend on.

      This method is idempotent: calling close() more than once, or calling it on a never-used but successfully constructed instance, is a no-op after the first successful close attempt. The underlying OrtSession.close() is only invoked once.

      Specified by:
      close in interface AutoCloseable
      Throws:
      ai.onnxruntime.OrtException - Thrown if the close attempt fails in the native layer.