rzo1 commented on code in PR #633:
URL: https://github.com/apache/opennlp/pull/633#discussion_r1667397156


##########
opennlp-tools/src/main/java/opennlp/tools/util/featuregen/DictionaryFeatureGeneratorFactory.java:
##########
@@ -31,30 +33,39 @@
 public class DictionaryFeatureGeneratorFactory
     extends GeneratorFactory.AbstractXmlFeatureGeneratorFactory {
 
+  private static final String DICT = "dict";
+
   public DictionaryFeatureGeneratorFactory() {
     super();
   }
 
   @Override
   public AdaptiveFeatureGenerator create() throws InvalidFormatException {
-    // if resourceManager is null, we don't instantiate
-    if (resourceManager == null) {
-      return null;
-    }
-
-    String dictResourceKey = getStr("dict");
-    Object dictResource = resourceManager.getResource(dictResourceKey);
-    if (!(dictResource instanceof Dictionary)) {
-      throw new InvalidFormatException("No dictionary resource for key: " + 
dictResourceKey);
+    Dictionary dict;
+    if (resourceManager == null) { // load the dictionary directly
+      String dictResourcePath = getStr(DICT);
+      ClassLoader cl = Thread.currentThread().getContextClassLoader();
+      try (InputStream is = cl.getResourceAsStream(dictResourcePath)) {

Review Comment:
   `is` can be NULL after this call (if `dictResourcePath` isn't found). Didn't 
Look into create(...) how it is handled.



##########
opennlp-tools/src/main/java/opennlp/tools/util/featuregen/DictionaryFeatureGeneratorFactory.java:
##########
@@ -31,30 +33,39 @@
 public class DictionaryFeatureGeneratorFactory
     extends GeneratorFactory.AbstractXmlFeatureGeneratorFactory {
 
+  private static final String DICT = "dict";
+
   public DictionaryFeatureGeneratorFactory() {
     super();
   }
 
   @Override
   public AdaptiveFeatureGenerator create() throws InvalidFormatException {
-    // if resourceManager is null, we don't instantiate
-    if (resourceManager == null) {
-      return null;
-    }
-
-    String dictResourceKey = getStr("dict");
-    Object dictResource = resourceManager.getResource(dictResourceKey);
-    if (!(dictResource instanceof Dictionary)) {
-      throw new InvalidFormatException("No dictionary resource for key: " + 
dictResourceKey);
+    Dictionary dict;
+    if (resourceManager == null) { // load the dictionary directly
+      String dictResourcePath = getStr(DICT);
+      ClassLoader cl = Thread.currentThread().getContextClassLoader();
+      try (InputStream is = cl.getResourceAsStream(dictResourcePath)) {
+        dict = ((DictionarySerializer) 
getArtifactSerializerMapping().get(dictResourcePath)).create(is);
+      } catch (IOException e) {
+        throw new InvalidFormatException("No dictionary resource at: " + 
dictResourcePath, e);
+      }
+    } else { // get the dictionary via a resourceManager lookup
+      String dictResourceKey = getStr(DICT);
+      Object dictResource = resourceManager.getResource(dictResourceKey);
+      if (dictResource instanceof Dictionary) {

Review Comment:
   Since we are Java 17, we can omit the cast in the subsequent line by inline 
casting in the instanceof.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to