You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2022/02/25 20:49:05 UTC

[incubator-nlpcraft] branch NLPCRAFT-483 updated: Adapters usage refactoring.

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

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


The following commit(s) were added to refs/heads/NLPCRAFT-483 by this push:
     new 7a02391  Adapters usage refactoring.
7a02391 is described below

commit 7a02391109903cdd0e14336cb02bcc92cda8ca7a
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Fri Feb 25 23:48:39 2022 +0300

    Adapters usage refactoring.
---
 .../examples/lightswitch/LightSwitchRuModel.scala  |  8 +--
 .../lightswitch/LightSwitchGroovyModel.groovy      | 35 ++--------
 .../examples/lightswitch/LightSwitchJavaModel.java | 36 ++--------
 .../examples/lightswitch/LightSwitchKotlinModel.kt | 32 ++-------
 .../lightswitch/LightSwitchScalaModel.scala        | 25 ++-----
 .../apache/nlpcraft/examples/time/TimeModel.java   | 76 ++++++++++------------
 .../apache/nlpcraft/nlp/NCENDefaultPipeline.java   |  4 +-
 7 files changed, 67 insertions(+), 149 deletions(-)

diff --git a/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala b/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
index 5a65ec9..802934c 100644
--- a/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
+++ b/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModel.scala
@@ -38,10 +38,10 @@ import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser
   * See 'README.md' file in the same folder for running and testing instructions.
   */
 
-class LightSwitchRuModel extends NCModel:
-    override val getConfig: NCModelConfig = new NCModelConfig("nlpcraft.lightswitch.ru.ex", "LightSwitch Example Model RU", "1.0")
-    override val getPipeline: NCModelPipeline = new NCRuPipeline(new NCRuSemanticEntityParser("lightswitch_model_ru.yaml"))
-
+class LightSwitchRuModel extends NCModelAdapter(
+    new NCModelConfig("nlpcraft.lightswitch.ru.ex", "LightSwitch Example Model RU", "1.0"),
+    new NCRuPipeline(new NCRuSemanticEntityParser("lightswitch_model_ru.yaml"))
+):
     /**
       * Intent and its on-match callback.
       *
diff --git a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchGroovyModel.groovy b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchGroovyModel.groovy
index aedc0d9..9ce91cb 100644
--- a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchGroovyModel.groovy
+++ b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchGroovyModel.groovy
@@ -18,11 +18,8 @@
 package org.apache.nlpcraft.examples.lightswitch
 
 import org.apache.nlpcraft.*
-import org.apache.nlpcraft.internal.util.NCResourceReader
-import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser
-import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer
-import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser
-import org.apache.nlpcraft.nlp.token.enricher.en.NCStopWordsTokenEnricher
+import org.apache.nlpcraft.nlp.NCENDefaultPipeline
+import org.apache.nlpcraft.nlp.NCENSemanticEntityParser
 
 /**
  * This example provides very simple implementation for NLI-powered light switch.
@@ -36,22 +33,12 @@ import org.apache.nlpcraft.nlp.token.enricher.en.NCStopWordsTokenEnricher
  * <p>
  * See 'README.md' file in the same folder for running and testing instructions.
  */
-class LightSwitchGroovyModel implements NCModel {
-    private final NCModelConfig cfg
-    private final NCModelPipeline pipeline
-
+class LightSwitchGroovyModel extends NCModelAdapter {
     LightSwitchGroovyModel() {
-        NCResourceReader reader = new NCResourceReader()
-        NCOpenNLPTokenParser tp = new NCOpenNLPTokenParser(
-            reader.getPath("opennlp/en-token.bin"),
-            reader.getPath("opennlp/en-pos-maxent.bin"),
-            reader.getPath("opennlp/en-lemmatizer.dict")
+        super(
+            new NCModelConfig("nlpcraft.lightswitch.java.ex", "LightSwitch Example Model", "1.0"),
+            new NCENDefaultPipeline(new NCENSemanticEntityParser("lightswitch_model.yaml"))
         )
-
-        this.cfg = new NCModelConfig("nlpcraft.lightswitch.groovy.ex", "LightSwitch Example Model", "1.0")
-        this.pipeline = new NCModelPipelineBuilder(tp, new NCSemanticEntityParser(new NCEnSemanticPorterStemmer(), tp, "lightswitch_model.yaml")).
-            withTokenEnricher(new NCStopWordsTokenEnricher()).
-            build()
     }
 
     /**
@@ -102,14 +89,4 @@ class LightSwitchGroovyModel implements NCModel {
 
         res
     }
-
-    @Override
-    NCModelConfig getConfig() {
-        return cfg
-    }
-
-    @Override
-    NCModelPipeline getPipeline() {
-        return pipeline
-    }
 }
diff --git a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java
index fbe5d5c..b557111 100644
--- a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java
+++ b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java
@@ -19,6 +19,8 @@ package org.apache.nlpcraft.examples.lightswitch;
 
 import org.apache.nlpcraft.*;
 import org.apache.nlpcraft.internal.util.NCResourceReader;
+import org.apache.nlpcraft.nlp.NCENDefaultPipeline;
+import org.apache.nlpcraft.nlp.NCENSemanticEntityParser;
 import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser;
 import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer;
 import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser;
@@ -39,28 +41,12 @@ import java.util.stream.Collectors;
  * <p>
  * See 'README.md' file in the same folder for running and testing instructions.
  */
-public class LightSwitchJavaModel implements NCModel {
-    private final NCModelConfig cfg;
-    private final NCModelPipeline pipeline;
-    /**
-     *
-     * @param tokMdlSrc
-     * @param posMdlSrc
-     * @param lemmaDicSrc
-     */
+public class LightSwitchJavaModel extends NCModelAdapter {
     public LightSwitchJavaModel() {
-        NCResourceReader reader = new NCResourceReader();
-
-        NCOpenNLPTokenParser tp = new NCOpenNLPTokenParser(
-            reader.getPath("opennlp/en-token.bin"),
-            reader.getPath("opennlp/en-pos-maxent.bin"),
-            reader.getPath("opennlp/en-lemmatizer.dict")
+        super(
+            new NCModelConfig("nlpcraft.lightswitch.java.ex", "LightSwitch Example Model", "1.0"),
+            new NCENDefaultPipeline(new NCENSemanticEntityParser("lightswitch_model.yaml"))
         );
-
-        this.cfg = new NCModelConfig("nlpcraft.lightswitch.java.ex", "LightSwitch Example Model", "1.0");
-        this.pipeline = new NCModelPipelineBuilder(tp, new NCSemanticEntityParser(new NCEnSemanticPorterStemmer(), tp, "lightswitch_model.yaml")).
-            withTokenEnricher(new NCStopWordsTokenEnricher()).
-            build();
     }
 
     /**
@@ -113,14 +99,4 @@ public class LightSwitchJavaModel implements NCModel {
 
         return res;
     }
-
-    @Override
-    public NCModelConfig getConfig() {
-        return cfg;
-    }
-
-    @Override
-    public NCModelPipeline getPipeline() {
-        return pipeline;
-    }
 }
\ No newline at end of file
diff --git a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt
index a05388c..63ed597 100644
--- a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt
+++ b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt
@@ -19,11 +19,8 @@ package org.apache.nlpcraft.examples.lightswitch
 
 
 import org.apache.nlpcraft.*
-import org.apache.nlpcraft.internal.util.NCResourceReader
-import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser
-import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer
-import org.apache.nlpcraft.nlp.token.enricher.en.NCStopWordsTokenEnricher
-import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser
+import org.apache.nlpcraft.nlp.NCENDefaultPipeline
+import org.apache.nlpcraft.nlp.NCENSemanticEntityParser
 import java.util.*
 import java.util.stream.Collectors
 
@@ -39,19 +36,10 @@ import java.util.stream.Collectors
  *
  * See 'README.md' file in the same folder for running and testing instructions.
  */
-class LightSwitchKotlinModel : NCModel {
-    private val reader = NCResourceReader()
-    private val tp = NCOpenNLPTokenParser(
-        reader.getPath("opennlp/en-token.bin"),
-        reader.getPath("opennlp/en-pos-maxent.bin"),
-        reader.getPath("opennlp/en-lemmatizer.dict")
-    )
-
-    private val cfg = NCModelConfig("nlpcraft.lightswitch.kotlin.ex", "LightSwitch Example Model", "1.0")
-    private val pipeline =
-        NCModelPipelineBuilder(tp, NCSemanticEntityParser(NCEnSemanticPorterStemmer(), tp, "lightswitch_model.yaml")).
-        withTokenEnricher(NCStopWordsTokenEnricher()).
-        build()
+class LightSwitchKotlinModel : NCModelAdapter(
+    NCModelConfig("nlpcraft.lightswitch.kotlin.ex", "LightSwitch Example Model", "1.0"),
+    NCENDefaultPipeline(NCENSemanticEntityParser("lightswitch_model.yaml"))
+) {
     /**
      * Intent and its on-match callback.
      *
@@ -103,12 +91,4 @@ class LightSwitchKotlinModel : NCModel {
 
         return res
     }
-
-    override fun getConfig(): NCModelConfig {
-        return cfg
-    }
-
-    override fun getPipeline(): NCModelPipeline {
-        return pipeline
-    }
 }
\ No newline at end of file
diff --git a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
index 6ccc995..6167d34 100644
--- a/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
+++ b/nlpcraft-examples/lightswitch/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchScalaModel.scala
@@ -18,12 +18,13 @@
 package org.apache.nlpcraft.examples.lightswitch
 
 import org.apache.nlpcraft.*
+import org.apache.nlpcraft.internal.util.NCResourceReader
 import org.apache.nlpcraft.nlp.entity.parser.nlp.NCNLPEntityParser
 import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser
 import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer
-import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser
 import org.apache.nlpcraft.nlp.token.enricher.en.NCStopWordsTokenEnricher
-import org.apache.nlpcraft.internal.util.NCResourceReader
+import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser
+import org.apache.nlpcraft.nlp.*
 
 /**
   * This example provides very simple implementation for NLI-powered light switch.
@@ -38,22 +39,10 @@ import org.apache.nlpcraft.internal.util.NCResourceReader
   * See 'README.md' file in the same folder for running and testing instructions.
   */
 
-class LightSwitchScalaModel extends NCModel:
-    override val getConfig: NCModelConfig = new NCModelConfig("nlpcraft.lightswitch.scala.ex", "LightSwitch Example Model", "1.0")
-    override val getPipeline: NCModelPipeline =
-        val reader = new NCResourceReader()
-        val tp = new NCOpenNLPTokenParser(
-            reader.getPath("opennlp/en-token.bin"),
-            reader.getPath("opennlp/en-pos-maxent.bin"),
-            reader.getPath("opennlp/en-lemmatizer.dict")
-        )
-        new NCModelPipelineBuilder(
-            tp,
-            new NCSemanticEntityParser(new NCEnSemanticPorterStemmer(), tp, "lightswitch_model.yaml")
-        ).
-            withTokenEnricher(new NCStopWordsTokenEnricher()).
-            build()
-
+class LightSwitchScalaModel extends NCModelAdapter(
+    new NCModelConfig("nlpcraft.lightswitch.java.ex", "LightSwitch Example Model", "1.0"),
+    new NCENDefaultPipeline(new NCENSemanticEntityParser("lightswitch_model.yaml"))
+):
     /**
       * Intent and its on-match callback.
       *
diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
index c39a781..8f6af61 100644
--- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
+++ b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
@@ -17,23 +17,39 @@
 
 package org.apache.nlpcraft.examples.time;
 
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.dataformat.yaml.*;
-import org.apache.nlpcraft.*;
-import org.apache.nlpcraft.examples.time.utils.cities.*;
-import org.apache.nlpcraft.examples.time.utils.keycdn.*;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import org.apache.nlpcraft.NCEntity;
+import org.apache.nlpcraft.NCIntent;
+import org.apache.nlpcraft.NCIntentMatch;
+import org.apache.nlpcraft.NCIntentRef;
+import org.apache.nlpcraft.NCIntentSample;
+import org.apache.nlpcraft.NCIntentTerm;
+import org.apache.nlpcraft.NCModelAdapter;
+import org.apache.nlpcraft.NCModelConfig;
+import org.apache.nlpcraft.NCModelPipeline;
+import org.apache.nlpcraft.NCRejection;
+import org.apache.nlpcraft.NCResult;
+import org.apache.nlpcraft.NCResultType;
+import org.apache.nlpcraft.examples.time.utils.cities.CitiesDataProvider;
+import org.apache.nlpcraft.examples.time.utils.cities.City;
+import org.apache.nlpcraft.examples.time.utils.cities.CityData;
+import org.apache.nlpcraft.examples.time.utils.keycdn.GeoData;
+import org.apache.nlpcraft.examples.time.utils.keycdn.GeoManager;
 import org.apache.nlpcraft.internal.util.NCResourceReader;
+import org.apache.nlpcraft.nlp.NCENDefaultPipeline;
+import org.apache.nlpcraft.nlp.NCENSemanticEntityParser;
 import org.apache.nlpcraft.nlp.entity.parser.opennlp.NCOpenNLPEntityParser;
-import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser;
-import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer;
-import org.apache.nlpcraft.nlp.token.parser.opennlp.NCOpenNLPTokenParser;
 
-import java.time.*;
-import java.time.format.*;
-import java.util.*;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
 
-import static java.time.format.FormatStyle.*;
+import static java.time.format.FormatStyle.MEDIUM;
 
 /**
  * Time example data model.
@@ -47,7 +63,7 @@ import static java.time.format.FormatStyle.*;
 @NCIntent("fragment=city term(city)~{# == 'opennlp:location'}")
 @NCIntent("intent=intent2 term~{# == 'x:time'} fragment(city)")
 @NCIntent("intent=intent1 term={# == 'x:time'}")
-public class TimeModel implements NCModel {
+public class TimeModel extends NCModelAdapter {
     // Medium data formatter.
     static private final DateTimeFormatter FMT = DateTimeFormatter.ofLocalizedDateTime(MEDIUM);
 
@@ -57,27 +73,17 @@ public class TimeModel implements NCModel {
     // Geo manager.
     static private final GeoManager geoMrg = new GeoManager();
 
-    private final NCModelConfig cfg;
-    private final NCModelPipeline pipeline;
-
     /**
      * Initializes model.
      */
     public TimeModel() {
-        NCResourceReader reader = new NCResourceReader();
-
-        NCOpenNLPTokenParser tp = new NCOpenNLPTokenParser(
-            reader.getPath("opennlp/en-token.bin"),
-            reader.getPath("opennlp/en-pos-maxent.bin"),
-            reader.getPath("opennlp/en-lemmatizer.dict")
+        super(
+            new NCModelConfig("nlpcraft.time.ex", "Time Example Model", "1.0"),
+            new NCENDefaultPipeline(
+                new NCENSemanticEntityParser("time_model.yaml"),
+                new NCOpenNLPEntityParser(new NCResourceReader().getPath("opennlp/en-ner-location.bin"))
+            )
         );
-
-        this.cfg = new NCModelConfig("nlpcraft.time.ex", "Time Example Model", "1.0");
-        this.pipeline = new NCModelPipelineBuilder(
-            tp,
-            new NCOpenNLPEntityParser(reader.getPath("opennlp/en-ner-location.bin")),
-            new NCSemanticEntityParser(new NCEnSemanticPorterStemmer(), tp, "time_model.yaml")
-        ).build();
     }
 
     /**
@@ -185,14 +191,4 @@ public class TimeModel implements NCModel {
             geo.getCityName(), geo.getCountryName(), geo.getTimezoneName(), geo.getLatitude(), geo.getLongitude()
         );
     }
-
-    @Override
-    public NCModelConfig getConfig() {
-        return cfg;
-    }
-
-    @Override
-    public NCModelPipeline getPipeline() {
-        return pipeline;
-    }
 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/NCENDefaultPipeline.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/NCENDefaultPipeline.java
index c504e78..244cac5 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/NCENDefaultPipeline.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/NCENDefaultPipeline.java
@@ -68,8 +68,8 @@ public class NCENDefaultPipeline implements NCModelPipeline {
      *
      * @param parser
      */
-    public NCENDefaultPipeline(NCEntityParser parser) {
-        this.entParsers = Collections.singletonList(parser);
+    public NCENDefaultPipeline(NCEntityParser... parsers) {
+        this.entParsers = Arrays.asList(parsers);
     }
 
     @Override