You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@opennlp.apache.org by GitBox <gi...@apache.org> on 2022/12/16 10:52:50 UTC

[GitHub] [opennlp] mawiesne opened a new pull request, #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

mawiesne opened a new pull request, #459:
URL: https://github.com/apache/opennlp/pull/459

   Change
   -
   - adds missing JavaDoc
   - improves existing documentation for clarity
   - removes superfluous text
   - adds 'final' modifier where useful and applicable
   - adds 'Override' annotation where useful and applicable
   - fixes several (funny) typos
   - reworks `DownloadUtil` by introducing a generic type parameter to avoid unnecessary casts in Model classes
   
   Tasks
   -
   Thank you for contributing to Apache OpenNLP.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [x] Is there a JIRA ticket associated with this PR? Is it referenced 
        in the commit message?
   
   - [x] Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [x] Has your PR been rebased against the latest commit within the target branch (typically master)?
   
   - [x] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [x] Have you ensured that the full suite of tests is executed via mvn clean install at the root opennlp folder?
   - [ ] Have you written or updated unit tests to verify your changes?
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
   - [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file in opennlp folder?
   - [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found in opennlp folder?
   
   ### For documentation related changes:
   - [x] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions for build issues and submit an update to your PR as soon as possible.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] jzonthemtn merged pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
jzonthemtn merged PR #459:
URL: https://github.com/apache/opennlp/pull/459


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
mawiesne commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1050636858


##########
opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java:
##########
@@ -41,20 +42,23 @@ static void setOSGiAvailable() {
 
   // Pass in the type (interface) of the class to load
   /**
-   * Instantiates an user provided extension to OpenNLP.
+   * Instantiates a user provided extension to OpenNLP.
    * <p>
    * The extension is either loaded from the class path or if running
    * inside an OSGi environment via an OSGi service.
    * <p>
-   * Initially it tries using the public default
-   * constructor. If it is not found, it will check if the class follows the singleton
-   * pattern: a static field named <code>INSTANCE</code> that returns an object of the type
-   * <code>T</code>.
+   * Initially, the load is conducted using the public no-arg constructor.
+   * If no such constructor is not found, it is checked if the class follows the
+   * {@code Singleton} pattern: a static field named {@code INSTANCE} that
+   * returns an object of the type {@link T}.
    *
-   * @param clazz
-   * @param extensionClassName
+   * @param clazz A reference to {@link Class<T>}.
+   * @param extensionClassName The (fully-qualified) name of the class
+   *                           by which the extension shall be loaded.
    *
    * @return the instance of the extension class
+   *
+   * @throws ExtensionNotLoadedException Thrown if the load operation failed.
    */
   // TODO: Throw custom exception if loading fails ...

Review Comment:
   I was wondering too. Maybe @kinow or @jzonthemtn can clarify here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
mawiesne commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1052960333


##########
opennlp-tools/src/main/java/opennlp/tools/util/DownloadUtil.java:
##########
@@ -50,68 +45,81 @@ public enum ModelType {
     CHUNKER("chunker"),
     PARSER("parser-chunking");
 
-    private String name;
+    private final String name;
 
     ModelType(String name) {
       this.name = name;
     }
   }
 
-  private static final String baseUrl = "https://dlcdn.apache.org/opennlp/";
+  private static final String BASE_URL = "https://dlcdn.apache.org/opennlp/";
+  private static final String MODELS_UD_MODELS_1_0 = "models/ud-models-1.0/";
 
-  public static Map<String, Map<ModelType, String>> available_models = new HashMap<>();
+  public static final Map<String, Map<ModelType, String>> available_models = new HashMap<>();
 
   static {
 
     final Map<ModelType, String> frenchModels = new HashMap<>();
     frenchModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-1.0-1.9.3fr-ud-ftb-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-1.0-1.9.3fr-ud-ftb-sentence-1.0-1.9.3.bin");
     frenchModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-fr-ud-ftb-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-fr-ud-ftb-pos-1.0-1.9.3.bin");
     frenchModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
     available_models.put("fr", frenchModels);
 
     final Map<ModelType, String> germanModels = new HashMap<>();
     germanModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-de-ud-gsd-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-de-ud-gsd-sentence-1.0-1.9.3.bin");
     germanModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-de-ud-gsd-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-de-ud-gsd-pos-1.0-1.9.3.bin");
     germanModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-de-ud-gsd-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-de-ud-gsd-tokens-1.0-1.9.3.bin");
     available_models.put("de", germanModels);
 
     final Map<ModelType, String> englishModels = new HashMap<>();
     englishModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin");
     englishModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-pos-1.0-1.9.3.bin");
     englishModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
     available_models.put("en", englishModels);
 
     final Map<ModelType, String> italianModels = new HashMap<>();
     italianModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
     italianModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-it-ud-vit-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-it-ud-vit-pos-1.0-1.9.3.bin");
     italianModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
     available_models.put("it", italianModels);
 
     final Map<ModelType, String> dutchModels = new HashMap<>();
     dutchModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/opennlp-nl-ud-alpino-sentence-1.0-1.9.3.bin");
+        BASE_URL + "models/opennlp-nl-ud-alpino-sentence-1.0-1.9.3.bin");
     dutchModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-nl-ud-alpino-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-nl-ud-alpino-pos-1.0-1.9.3.bin");
     dutchModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-nl-ud-alpino-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-nl-ud-alpino-tokens-1.0-1.9.3.bin");
     available_models.put("nl", dutchModels);
 
   }
 
-  public static BaseModel downloadModel(String language, ModelType modelType, Class<?> type)
-          throws IOException {
+  /**
+   * Triggers a download for the {@link DownloadUtil.ModelType}

Review Comment:
   Kind of proof that I'm a human worker. 😄
   
   Will fix. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] rzo1 commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
rzo1 commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1050621640


##########
opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java:
##########
@@ -41,20 +42,23 @@ static void setOSGiAvailable() {
 
   // Pass in the type (interface) of the class to load
   /**
-   * Instantiates an user provided extension to OpenNLP.
+   * Instantiates a user provided extension to OpenNLP.
    * <p>
    * The extension is either loaded from the class path or if running
    * inside an OSGi environment via an OSGi service.
    * <p>
-   * Initially it tries using the public default
-   * constructor. If it is not found, it will check if the class follows the singleton
-   * pattern: a static field named <code>INSTANCE</code> that returns an object of the type
-   * <code>T</code>.
+   * Initially, the load is conducted using the public no-arg constructor.
+   * If no such constructor is not found, it is checked if the class follows the
+   * {@code Singleton} pattern: a static field named {@code INSTANCE} that
+   * returns an object of the type {@link T}.
    *
-   * @param clazz
-   * @param extensionClassName
+   * @param clazz A reference to {@link Class<T>}.
+   * @param extensionClassName The (fully-qualified) name of the class
+   *                           by which the extension shall be loaded.
    *
    * @return the instance of the extension class
+   *
+   * @throws ExtensionNotLoadedException Thrown if the load operation failed.
    */
   // TODO: Throw custom exception if loading fails ...

Review Comment:
   Wonder, if `ExtensionNotLoadedException` isn't the custom exception mentioned in this TODO? The Javadoc states: 
   
   >  Exception indicates that an OpenNLP extension could not be loaded.



##########
opennlp-tools/src/main/java/opennlp/tools/util/featuregen/BrownCluster.java:
##########
@@ -36,34 +36,40 @@
 /**
  *
  * Class to load a Brown cluster document: word\tword_class\tprob
- * http://metaoptimize.com/projects/wordreprs/
- *
+ * <p>
+ * See: <a href="http://metaoptimize.com/projects/wordreprs/">

Review Comment:
   Link seems dead (at least for me). Getting a "Origin is unreachable Error code 523" from cloudflare.
   It isn't archived in the InternetArchive either. I was only able to find a copy via `archive.ph`: https://archive.ph/p82oc
   
   I think the related research paper is: https://dl.acm.org/doi/10.5555/1858681.1858721
   
   Maybe we can add a reference and mark the link as "broken" ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] kinow commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
kinow commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1052035017


##########
opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java:
##########
@@ -41,20 +42,23 @@ static void setOSGiAvailable() {
 
   // Pass in the type (interface) of the class to load
   /**
-   * Instantiates an user provided extension to OpenNLP.
+   * Instantiates a user provided extension to OpenNLP.
    * <p>
    * The extension is either loaded from the class path or if running
    * inside an OSGi environment via an OSGi service.
    * <p>
-   * Initially it tries using the public default
-   * constructor. If it is not found, it will check if the class follows the singleton
-   * pattern: a static field named <code>INSTANCE</code> that returns an object of the type
-   * <code>T</code>.
+   * Initially, the load is conducted using the public no-arg constructor.
+   * If no such constructor is not found, it is checked if the class follows the
+   * {@code Singleton} pattern: a static field named {@code INSTANCE} that
+   * returns an object of the type {@link T}.
    *
-   * @param clazz
-   * @param extensionClassName
+   * @param clazz A reference to {@link Class<T>}.
+   * @param extensionClassName The (fully-qualified) name of the class
+   *                           by which the extension shall be loaded.
    *
    * @return the instance of the extension class
+   *
+   * @throws ExtensionNotLoadedException Thrown if the load operation failed.
    */
   // TODO: Throw custom exception if loading fails ...

Review Comment:
   No objections from me. If a user or another dev has a suggestion how to improve it we can always make another TODO or issue. And having a TODO that others don't understand probably confuses more than helps, so +1 to removing it. Thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
mawiesne commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1052961165


##########
opennlp-tools/src/main/java/opennlp/tools/util/DownloadUtil.java:
##########
@@ -144,29 +158,20 @@ public static BaseModel downloadModel(URL url, Class<?> type) throws IOException
     final Path localFile = Paths.get(homeDirectory.toString(), filename);
 
     if (!Files.exists(localFile)) {
-
       System.out.println("Downloading model " + url + " to " + localFile);
 
       try (final InputStream in = url.openStream()) {
         Files.copy(in, localFile, StandardCopyOption.REPLACE_EXISTING);
       }
 
       System.out.println("Download complete.");
-
     }
 
-    if (type == TokenizerModel.class) {
-      return new TokenizerModel(localFile);
-    } else if (type == ChunkerModel.class) {
-      return new ChunkerModel(localFile);
-    } else if (type == SentenceModel.class) {
-      return new SentenceModel(localFile);
-    } else if (type == POSModel.class) {
-      return new POSModel(localFile);
-    } else {
-      return new TokenNameFinderModel(localFile);
+    try {
+      return type.getConstructor(Path.class).newInstance(localFile);

Review Comment:
   Will check if this would have any benefits here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
mawiesne commented on PR #459:
URL: https://github.com/apache/opennlp/pull/459#issuecomment-1358912696

   Thanks for your feedback, Bruno!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] kinow commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
kinow commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1051661032


##########
opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java:
##########
@@ -41,20 +42,23 @@ static void setOSGiAvailable() {
 
   // Pass in the type (interface) of the class to load
   /**
-   * Instantiates an user provided extension to OpenNLP.
+   * Instantiates a user provided extension to OpenNLP.
    * <p>
    * The extension is either loaded from the class path or if running
    * inside an OSGi environment via an OSGi service.
    * <p>
-   * Initially it tries using the public default
-   * constructor. If it is not found, it will check if the class follows the singleton
-   * pattern: a static field named <code>INSTANCE</code> that returns an object of the type
-   * <code>T</code>.
+   * Initially, the load is conducted using the public no-arg constructor.
+   * If no such constructor is not found, it is checked if the class follows the
+   * {@code Singleton} pattern: a static field named {@code INSTANCE} that
+   * returns an object of the type {@link T}.
    *
-   * @param clazz
-   * @param extensionClassName
+   * @param clazz A reference to {@link Class<T>}.
+   * @param extensionClassName The (fully-qualified) name of the class
+   *                           by which the extension shall be loaded.
    *
    * @return the instance of the extension class
+   *
+   * @throws ExtensionNotLoadedException Thrown if the load operation failed.
    */
   // TODO: Throw custom exception if loading fails ...

Review Comment:
   I don't think that's the exception mentioned, as the TODO was added in this commit where the code was throwing `ExtensionNotLoadedException` - https://github.com/apache/opennlp/commit/21eba24bc2b8ef274251f7c4f146969e7a5a7da0#diff-f8d40ab6a05ba05556cd8b555ec4f60b4c499fdd450e05dc3eb61f9e9d940125R37-R60



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
mawiesne commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1051953520


##########
opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java:
##########
@@ -41,20 +42,23 @@ static void setOSGiAvailable() {
 
   // Pass in the type (interface) of the class to load
   /**
-   * Instantiates an user provided extension to OpenNLP.
+   * Instantiates a user provided extension to OpenNLP.
    * <p>
    * The extension is either loaded from the class path or if running
    * inside an OSGi environment via an OSGi service.
    * <p>
-   * Initially it tries using the public default
-   * constructor. If it is not found, it will check if the class follows the singleton
-   * pattern: a static field named <code>INSTANCE</code> that returns an object of the type
-   * <code>T</code>.
+   * Initially, the load is conducted using the public no-arg constructor.
+   * If no such constructor is not found, it is checked if the class follows the
+   * {@code Singleton} pattern: a static field named {@code INSTANCE} that
+   * returns an object of the type {@link T}.
    *
-   * @param clazz
-   * @param extensionClassName
+   * @param clazz A reference to {@link Class<T>}.
+   * @param extensionClassName The (fully-qualified) name of the class
+   *                           by which the extension shall be loaded.
    *
    * @return the instance of the extension class
+   *
+   * @throws ExtensionNotLoadedException Thrown if the load operation failed.
    */
   // TODO: Throw custom exception if loading fails ...

Review Comment:
   @kinow Thanks for helping out here.
   
   In discussion with @rzo1, we opted to remove the existing TODO as it is "ancient" in a terms of what the code today does. The catch-block around line 99/100 must be empty, as afterwards another attempt is made to compensate for this via OSGi loading mechanism. Therefore, the `ExtensionNotLoadedException` will be thrown any way, if that compensation attempt also fails. In our (re)view of the code, there is no hard need to introduce other exceptions here. Thus, I'll remove the forgotten TODO.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] kinow commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
kinow commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1052679956


##########
opennlp-tools/src/main/java/opennlp/tools/util/DownloadUtil.java:
##########
@@ -50,68 +45,81 @@ public enum ModelType {
     CHUNKER("chunker"),
     PARSER("parser-chunking");
 
-    private String name;
+    private final String name;
 
     ModelType(String name) {
       this.name = name;
     }
   }
 
-  private static final String baseUrl = "https://dlcdn.apache.org/opennlp/";
+  private static final String BASE_URL = "https://dlcdn.apache.org/opennlp/";
+  private static final String MODELS_UD_MODELS_1_0 = "models/ud-models-1.0/";
 
-  public static Map<String, Map<ModelType, String>> available_models = new HashMap<>();
+  public static final Map<String, Map<ModelType, String>> available_models = new HashMap<>();
 
   static {
 
     final Map<ModelType, String> frenchModels = new HashMap<>();
     frenchModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-1.0-1.9.3fr-ud-ftb-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-1.0-1.9.3fr-ud-ftb-sentence-1.0-1.9.3.bin");
     frenchModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-fr-ud-ftb-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-fr-ud-ftb-pos-1.0-1.9.3.bin");
     frenchModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
     available_models.put("fr", frenchModels);
 
     final Map<ModelType, String> germanModels = new HashMap<>();
     germanModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-de-ud-gsd-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-de-ud-gsd-sentence-1.0-1.9.3.bin");
     germanModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-de-ud-gsd-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-de-ud-gsd-pos-1.0-1.9.3.bin");
     germanModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-de-ud-gsd-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-de-ud-gsd-tokens-1.0-1.9.3.bin");
     available_models.put("de", germanModels);
 
     final Map<ModelType, String> englishModels = new HashMap<>();
     englishModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin");
     englishModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-pos-1.0-1.9.3.bin");
     englishModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-en-ud-ewt-tokens-1.0-1.9.3.bin");
     available_models.put("en", englishModels);
 
     final Map<ModelType, String> italianModels = new HashMap<>();
     italianModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/ud-models-1.0/opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
     italianModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-it-ud-vit-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-it-ud-vit-pos-1.0-1.9.3.bin");
     italianModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-it-ud-vit-sentence-1.0-1.9.3.bin");
     available_models.put("it", italianModels);
 
     final Map<ModelType, String> dutchModels = new HashMap<>();
     dutchModels.put(ModelType.SENTENCE_DETECTOR,
-        baseUrl + "models/opennlp-nl-ud-alpino-sentence-1.0-1.9.3.bin");
+        BASE_URL + "models/opennlp-nl-ud-alpino-sentence-1.0-1.9.3.bin");
     dutchModels.put(ModelType.POS,
-        baseUrl + "models/ud-models-1.0/opennlp-nl-ud-alpino-pos-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-nl-ud-alpino-pos-1.0-1.9.3.bin");
     dutchModels.put(ModelType.TOKENIZER,
-        baseUrl + "models/ud-models-1.0/opennlp-nl-ud-alpino-tokens-1.0-1.9.3.bin");
+        BASE_URL + MODELS_UD_MODELS_1_0 + "opennlp-nl-ud-alpino-tokens-1.0-1.9.3.bin");
     available_models.put("nl", dutchModels);
 
   }
 
-  public static BaseModel downloadModel(String language, ModelType modelType, Class<?> type)
-          throws IOException {
+  /**
+   * Triggers a download for the {@link DownloadUtil.ModelType}

Review Comment:
   Missing period.



##########
opennlp-tools/src/main/java/opennlp/tools/util/DownloadUtil.java:
##########
@@ -144,29 +158,20 @@ public static BaseModel downloadModel(URL url, Class<?> type) throws IOException
     final Path localFile = Paths.get(homeDirectory.toString(), filename);
 
     if (!Files.exists(localFile)) {
-
       System.out.println("Downloading model " + url + " to " + localFile);
 
       try (final InputStream in = url.openStream()) {
         Files.copy(in, localFile, StandardCopyOption.REPLACE_EXISTING);
       }
 
       System.out.println("Download complete.");
-
     }
 
-    if (type == TokenizerModel.class) {
-      return new TokenizerModel(localFile);
-    } else if (type == ChunkerModel.class) {
-      return new ChunkerModel(localFile);
-    } else if (type == SentenceModel.class) {
-      return new SentenceModel(localFile);
-    } else if (type == POSModel.class) {
-      return new POSModel(localFile);
-    } else {
-      return new TokenNameFinderModel(localFile);
+    try {
+      return type.getConstructor(Path.class).newInstance(localFile);

Review Comment:
   Or we could provide the constructor as a function, I think, e.g. https://www.baeldung.com/java-8-double-colon-operator#1-create-a-new-instance



##########
opennlp-tools/src/main/java/opennlp/tools/util/DownloadUtil.java:
##########
@@ -144,29 +158,20 @@ public static BaseModel downloadModel(URL url, Class<?> type) throws IOException
     final Path localFile = Paths.get(homeDirectory.toString(), filename);
 
     if (!Files.exists(localFile)) {
-
       System.out.println("Downloading model " + url + " to " + localFile);
 
       try (final InputStream in = url.openStream()) {
         Files.copy(in, localFile, StandardCopyOption.REPLACE_EXISTING);
       }
 
       System.out.println("Download complete.");
-
     }
 
-    if (type == TokenizerModel.class) {
-      return new TokenizerModel(localFile);
-    } else if (type == ChunkerModel.class) {
-      return new ChunkerModel(localFile);
-    } else if (type == SentenceModel.class) {
-      return new SentenceModel(localFile);
-    } else if (type == POSModel.class) {
-      return new POSModel(localFile);
-    } else {
-      return new TokenNameFinderModel(localFile);
+    try {
+      return type.getConstructor(Path.class).newInstance(localFile);

Review Comment:
   :ok_man: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [opennlp] mawiesne commented on a diff in pull request #459: OPENNLP-1413 Enhance JavaDoc in opennlp.tools.util package

Posted by GitBox <gi...@apache.org>.
mawiesne commented on code in PR #459:
URL: https://github.com/apache/opennlp/pull/459#discussion_r1054097099


##########
opennlp-tools/src/main/java/opennlp/tools/util/DownloadUtil.java:
##########
@@ -144,29 +158,20 @@ public static BaseModel downloadModel(URL url, Class<?> type) throws IOException
     final Path localFile = Paths.get(homeDirectory.toString(), filename);
 
     if (!Files.exists(localFile)) {
-
       System.out.println("Downloading model " + url + " to " + localFile);
 
       try (final InputStream in = url.openStream()) {
         Files.copy(in, localFile, StandardCopyOption.REPLACE_EXISTING);
       }
 
       System.out.println("Download complete.");
-
     }
 
-    if (type == TokenizerModel.class) {
-      return new TokenizerModel(localFile);
-    } else if (type == ChunkerModel.class) {
-      return new ChunkerModel(localFile);
-    } else if (type == SentenceModel.class) {
-      return new SentenceModel(localFile);
-    } else if (type == POSModel.class) {
-      return new POSModel(localFile);
-    } else {
-      return new TokenNameFinderModel(localFile);
+    try {
+      return type.getConstructor(Path.class).newInstance(localFile);

Review Comment:
   @kinow I checked, but this won't work due to `T` not being a `@FunctionalInterface`.  For this to work, we'd need to introduce a functional interface `Model` first. For now, I'll leave it _as is_ for this PR. Feel free to investigate deeper or open a separate issue for this, if you think, it could be reworked and offers benefits in other classes/situations.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org