This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new d1102a1  File adapter names usage fixes. YAML and JSON readers usage 
fixes.
d1102a1 is described below

commit d1102a17a61b539611fb8c5d2b57d281123936ed
Author: Sergey Kamov <[email protected]>
AuthorDate: Sat Nov 21 10:44:47 2020 +0300

    File adapter names usage fixes. YAML and JSON readers usage fixes.
---
 .../apache/nlpcraft/model/NCModelFileAdapter.java  | 52 ++++++++++++----------
 .../nlpcraft/model/impl/json/NCModelJson.java      | 20 ++++-----
 .../sqlgen/impl/NCSqlModelGeneratorImpl.scala      |  2 +-
 .../probe/mgrs/deploy/NCDeployManager.scala        |  4 +-
 4 files changed, 41 insertions(+), 37 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelFileAdapter.java 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelFileAdapter.java
index 0bb6e5b..c607677 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelFileAdapter.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCModelFileAdapter.java
@@ -17,9 +17,9 @@
 
 package org.apache.nlpcraft.model;
 
+import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.*;
 import com.fasterxml.jackson.dataformat.yaml.*;
-import com.google.gson.*;
 import org.apache.nlpcraft.common.*;
 import org.apache.nlpcraft.common.util.*;
 import org.apache.nlpcraft.model.impl.json.*;
@@ -67,9 +67,16 @@ abstract public class NCModelFileAdapter extends 
NCModelAdapter {
 
     private final String origin;
 
-    /** */
-    private static final Gson GSON = new Gson();
-    
+    private final static ObjectMapper MAPPER_YAML = new ObjectMapper(new 
YAMLFactory());
+    private final static ObjectMapper MAPPER_JSON = new ObjectMapper();
+
+    static {
+        MAPPER_JSON.enable(JsonParser.Feature.ALLOW_COMMENTS);
+
+        
MAPPER_JSON.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
+        
MAPPER_YAML.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
+    }
+
     /**
      * Creates new model loading its configuration from given file path. Only 
<code>.js</code>,
      * <code>.json</code>, <code>.yml</code> and <code>.yaml</code> files are 
supported. File path can be
@@ -105,8 +112,8 @@ abstract public class NCModelFileAdapter extends 
NCModelAdapter {
         this.proxy = proxy;
         this.suspWords = convert(proxy.getSuspiciousWords(), null);
         this.enabledToks = convert(proxy.getEnabledBuiltInTokens(), 
NCModelView.DFLT_ENABLED_BUILTIN_TOKENS);
-        this.addStopwords = convert(proxy.getAdditionalStopwords(), null);
-        this.exclStopwords = convert(proxy.getExcludedStopwords(), null);
+        this.addStopwords = convert(proxy.getAdditionalStopWords(), null);
+        this.exclStopwords = convert(proxy.getExcludedStopWords(), null);
         this.elems = convertElements(proxy.getElements());
         this.macros = convertMacros(proxy.getMacros());
         this.metadata = convertMeta(proxy.getMetadata());
@@ -170,26 +177,23 @@ abstract public class NCModelFileAdapter extends 
NCModelAdapter {
      * @throws NCException
      */
     private static NCModelJson readModel(String path, InputStream in, String 
pathLow) {
-        if (pathLow.endsWith("yaml") || pathLow.endsWith("yml")) {
-            ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
-    
-            try {
-                return mapper.readValue(in, NCModelJson.class);
-            }
-            catch (Exception e) {
-                throw new NCException("Failed to load YAML: " + path, e);
-            }
-        }
+        ObjectMapper m;
+
+        if (pathLow.endsWith("yaml") || pathLow.endsWith("yml"))
+            m = MAPPER_YAML;
         else if (pathLow.endsWith("js") || pathLow.endsWith("json")) {
-            try (Reader reader = new BufferedReader(new 
InputStreamReader(in))) {
-                return GSON.fromJson(reader, NCModelJson.class);
-            }
-            catch (Exception e) {
-                throw new NCException("Failed to load JSON: " + path, e);
-            }
+            m = MAPPER_JSON;
+        }
+        else {
+            throw new NCException("Unsupported model configuration file type 
(.yaml, .yml, .js or .json only): " + path);
+        }
+
+        try {
+            return m.readValue(in, NCModelJson.class);
+        }
+        catch (Exception e) {
+            throw new NCException("Failed to load: " + path, e);
         }
-        
-        throw new NCException("Unsupported model configuration file type 
(.yaml, .yml, .js or .json only): " + path);
     }
 
     /**
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/json/NCModelJson.java 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/json/NCModelJson.java
index acf829a..d1e0f90 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/json/NCModelJson.java
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/json/NCModelJson.java
@@ -32,8 +32,8 @@ public class NCModelJson {
     private Map<String, Object> metadata;
     private NCMacroJson[] macros;
     private NCElementJson[] elements;
-    private String[] additionalStopwords;
-    private String[] excludedStopwords;
+    private String[] additionalStopWords;
+    private String[] excludedStopWords;
     private String[] suspiciousWords;
     private String[] enabledBuiltInTokens;
     private String[] intents;
@@ -97,17 +97,17 @@ public class NCModelJson {
     public void setElements(NCElementJson[] elements) {
         this.elements = elements;
     }
-    public String[] getAdditionalStopwords() {
-        return additionalStopwords;
+    public String[] getAdditionalStopWords() {
+        return additionalStopWords;
     }
-    public void setAdditionalStopwords(String[] additionalStopwords) {
-        this.additionalStopwords = additionalStopwords;
+    public void setAdditionalStopWords(String[] additionalStopWords) {
+        this.additionalStopWords = additionalStopWords;
     }
-    public String[] getExcludedStopwords() {
-        return excludedStopwords;
+    public String[] getExcludedStopWords() {
+        return excludedStopWords;
     }
-    public void setExcludedStopwords(String[] excludedStopwords) {
-        this.excludedStopwords = excludedStopwords;
+    public void setExcludedStopWords(String[] excludedStopWords) {
+        this.excludedStopWords = excludedStopWords;
     }
     public String[] getSuspiciousWords() {
         return suspiciousWords;
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
index cef8aeb..3783925 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
@@ -319,7 +319,7 @@ object NCSqlModelGeneratorImpl {
             new NCMacroJson("<OF>", "{of|for|per}"),
             new NCMacroJson("<ID>", "{unique|*} {id|identifier}")
         ))
-        mdl.setAdditionalStopwords(Array())
+        mdl.setAdditionalStopWords(Array())
         mdl.setSuspiciousWords(Array())
         mdl.setEnabledBuiltInTokens(Array())
         mdl.setIntents(Array())
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
index 9b15a7d..d7fb055 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
@@ -178,8 +178,8 @@ object NCDeployManager extends NCService with 
DecorateAsScala {
                 else
                     NCNlpCoreManager.stem(word)
 
-        val addStopWords = checkAndStemmatize(mdl.getAdditionalStopWords, 
"additionalStopword")
-        val exclStopWords = checkAndStemmatize(mdl.getExcludedStopWords, 
"excludedStopword")
+        val addStopWords = checkAndStemmatize(mdl.getAdditionalStopWords, 
"additionalStopWords")
+        val exclStopWords = checkAndStemmatize(mdl.getExcludedStopWords, 
"excludedStopWords")
         val suspWords = checkAndStemmatize(mdl.getSuspiciousWords, 
"suspiciousWord")
 
         checkStopwordsDups(mdlId, addStopWords, exclStopWords)

Reply via email to