Class DownloadUtil

java.lang.Object
opennlp.tools.util.DownloadUtil

public class DownloadUtil extends Object
This class facilitates the downloading of pretrained OpenNLP models.
  • Constructor Details

    • DownloadUtil

      public DownloadUtil()
  • Method Details

    • downloadModel

      public static <T extends BaseModel> T downloadModel(String language, opennlp.tools.models.ModelType modelType, Class<T> type) throws IOException
      Triggers a download for the specified ModelType.
      Type Parameters:
      T - The generic type which is a subclass of BaseModel.
      Parameters:
      language - The ISO language code of the requested model.
      modelType - The type of model.
      type - The class of the resulting model.
      Returns:
      A model instance of type T.
      Throws:
      IOException - Thrown if IO errors occurred or the model is invalid.
      IllegalArgumentException - Thrown if the checksum file of the model is blank.
      IllegalStateException - Thrown if the computed hash sum of the model file does not match the expected one.
    • downloadModel

      public static <T extends BaseModel> T downloadModel(URL url, Class<T> type) throws IOException
      Downloads a model from a URL.

      The model is saved to an .opennlp/ directory located in the user's home directory. This directory will be created if it does not already exist. If a model to be downloaded already exists in that directory, the model will not be re-downloaded, but it is verified against its SHA-512 checksum before it is loaded.

      Type Parameters:
      T - The generic type which is a subclass of BaseModel.
      Parameters:
      url - The model's URL.
      type - The class of the resulting model T.
      Returns:
      A model instance of type T.
      Throws:
      IOException - Thrown if the model cannot be downloaded.
      IllegalArgumentException - Thrown if the checksum file of the model is blank.
      IllegalStateException - Thrown if the computed hash sum of the model file does not match the expected one.
    • getAvailableModels

      public static Map<String, Map<opennlp.tools.models.ModelType, URL>> getAvailableModels()
      Lists the models published in the model index. A copy of the last index read is kept in the download directory and used when the index cannot be read. An empty result is not kept, so the next call reads the index again.
      Returns:
      The model URLs per language and ModelType. Empty if neither the index nor its local copy lists models.