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 984da908 Calculator test. Minor bugfixes.
984da908 is described below

commit 984da9082c0308ffca7703812541933dbf8f81ba
Author: Sergey Kamov <[email protected]>
AuthorDate: Sat Oct 15 18:15:21 2022 +0400

    Calculator test. Minor bugfixes.
---
 build.sbt                                          |   3 +-
 .../lightswitch/NCModelValidationSpec.scala        |   3 +-
 .../lightswitch/NCModelValidationSpec.scala        |   3 +-
 .../lightswitch/NCModelValidationSpec.scala        |   3 +-
 .../examples/time/NCModelValidationSpec.scala      |   3 +-
 .../nlpcraft/models/NCIntCalcModelSpec.scala       | 106 +++++++++++++++++++++
 .../nlp/enrichers/NCEnStopWordsTokenEnricher.scala |   2 +-
 .../nlpcraft/nlp/parsers/NCNLPEntityParser.scala   |   2 +-
 nlpcraft/src/test/resources/scan/samples.txt       |  19 ----
 9 files changed, 117 insertions(+), 27 deletions(-)

diff --git a/build.sbt b/build.sbt
index e717c7ed..73818b82 100644
--- a/build.sbt
+++ b/build.sbt
@@ -71,8 +71,7 @@ lazy val libs = Seq(
     "org.apache.opennlp" % "opennlp-tools" % apacheOpennlpVer,
     "org.apache.logging.log4j" % "log4j-slf4j-impl" % log4jVer,
     "org.apache.logging.log4j" % "log4j-api" % log4jVer,
-    "org.jline" % "jline-terminal" % jlineVer,
-    "org.junit.jupiter" % "junit-jupiter-engine" % junitVer % Test
+    "org.jline" % "jline-terminal" % jlineVer
 )
 
 val commonScalaDoc = Seq(
diff --git 
a/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
 
b/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
index 1d15dda4..43ae3aba 100644
--- 
a/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
+++ 
b/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
@@ -28,7 +28,8 @@ import scala.util.Using
 class NCModelValidationSpec extends AnyFunSuite:
     test("test") {
         Using.resource(new NCModelClient(new LightSwitchFrModel)) { client =>
-            def check(txt: String): Unit = client.debugAsk(txt, "userId", 
true).getIntentId == "ls"
+            def check(txt: String): Unit =
+                require(client.debugAsk(txt, "userId", true).getIntentId == 
"ls")
 
             check("Éteignez les lumières dans toute la maison.")
             check("Éteignez toutes les lumières maintenant.")
diff --git 
a/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
 
b/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
index 05662f99..b4518982 100644
--- 
a/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
+++ 
b/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
@@ -28,7 +28,8 @@ import scala.util.Using
 class NCModelValidationSpec extends AnyFunSuite:
     test("test") {
         Using.resource(new NCModelClient(new LightSwitchRuModel)) { client =>
-            def check(txt: String): Unit = client.debugAsk(txt, "userId", 
true).getIntentId == "ls"
+            def check(txt: String): Unit = 
+                require(client.debugAsk(txt, "userId", true).getIntentId == 
"ls")
 
             check("Выключи свет по всем доме")
             check("Выруби электричество!")
diff --git 
a/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
 
b/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
index 94f53c6c..06f93382 100644
--- 
a/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
+++ 
b/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/NCModelValidationSpec.scala
@@ -27,7 +27,8 @@ import scala.util.Using
   */
 class NCModelValidationSpec extends AnyFunSuite:
     private def test(mdl: NCModel): Unit = Using.resource(new 
NCModelClient(mdl)) { client =>
-        def check(txt: String): Unit = client.debugAsk(txt, "userId", 
true).getIntentId == "ls"
+        def check(txt: String): Unit =
+            require(client.debugAsk(txt, "userId", true).getIntentId == "ls")
         
         check("Turn the lights off in the entire house.")
         check("Turn off all lights now")
diff --git 
a/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
 
b/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
index 686d4b0b..694e226d 100644
--- 
a/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
+++ 
b/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/NCModelValidationSpec.scala
@@ -28,7 +28,8 @@ import scala.util.Using
 class NCModelValidationSpec extends AnyFunSuite:
     test("test") {
         Using.resource(new NCModelClient(new TimeModel())) { client =>
-            def check(txt: String, intentId: String): Unit = 
client.debugAsk(txt, "userId", true).getIntentId == intentId
+            def check(txt: String, intentId: String): Unit =
+                require(client.debugAsk(txt, "userId", true).getIntentId == 
intentId)
 
             check("What time is it now in New York City?", "intent2")
             check("What's the current time in Moscow?", "intent2")
diff --git 
a/nlpcraft-stanford/src/test/scala/org/apache/nlpcraft/models/NCIntCalcModelSpec.scala
 
b/nlpcraft-stanford/src/test/scala/org/apache/nlpcraft/models/NCIntCalcModelSpec.scala
new file mode 100644
index 00000000..b92e6304
--- /dev/null
+++ 
b/nlpcraft-stanford/src/test/scala/org/apache/nlpcraft/models/NCIntCalcModelSpec.scala
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nlpcraft.models
+
+import edu.stanford.nlp.pipeline.StanfordCoreNLP
+import opennlp.tools.stemmer.PorterStemmer
+import org.apache.nlpcraft.*
+import org.apache.nlpcraft.annotations.*
+import org.apache.nlpcraft.nlp.entity.parser.stanford.*
+import org.apache.nlpcraft.nlp.parsers.*
+import org.apache.nlpcraft.nlp.token.parser.stanford.NCStanfordNLPTokenParser
+import org.apache.nlpcraft.nlp.util.*
+import org.scalatest.funsuite.AnyFunSuite
+
+import java.util.Properties
+import scala.util.Using
+
+object NCIntCalcModelSpec:
+    val MDL: NCModel = new NCTestModelAdapter:
+        private var mem: Option[Int] = None
+
+        override val getPipeline: NCPipeline =
+            val stanford =
+                val props = new Properties()
+                props.setProperty("annotators", "tokenize, ssplit, pos, lemma, 
ner")
+                new StanfordCoreNLP(props)
+
+            new NCPipelineBuilder().
+                withTokenParser(new NCStanfordNLPTokenParser(stanford)).
+                // For operations.
+                withEntityParser(new NCNLPEntityParser).
+                // For numerics.
+                withEntityParser(new NCStanfordNLPEntityParser(stanford, 
Set("number"))).
+                build
+
+        private def nne(e: NCEntity): Int = 
java.lang.Double.parseDouble(e[String]("stanford:number:nne")).intValue
+
+        private def calc(x: Int, op: String, y: Int): NCResult =
+            mem =
+                Some(op match
+                    case "+" => x + y
+                    case "-" => x - y
+                    case "*" => x * y
+                    case "/" => x / y
+                    case _ => throw new IllegalStateException()
+                )
+
+            NCResult(mem.get)
+
+        @NCIntent(
+            "intent=calc options={ 'ordered': false }" +
+            "  term(x)={# == 'stanford:number'} " +
+            "  term(op)={# == 'nlp:token' && has(list('+', '-', '*', '/'), 
meta_ent('nlp:token:text')) == true} " +
+            "  term(y)={# == 'stanford:number'}"
+        )
+        def onMatch(
+            ctx: NCContext,
+            im: NCIntentMatch,
+            @NCIntentTerm("x") x: NCEntity,
+            @NCIntentTerm("op") op: NCEntity,
+            @NCIntentTerm("y") y: NCEntity
+        ): NCResult = calc(nne(x), op.mkText, nne(y))
+
+        @NCIntent(
+            "intent=calcMem options={ 'ordered': false }" +
+            "  term(op)={# == 'nlp:token' && has(list('+', '-', '*', '/'), 
meta_ent('nlp:token:text')) == true} " +
+            "  term(y)={# == 'stanford:number'}"
+        )
+        def onMatchMem(
+            ctx: NCContext,
+            im: NCIntentMatch,
+            @NCIntentTerm("op") op: NCEntity,
+            @NCIntentTerm("y") y: NCEntity
+        ): NCResult =
+            mem match
+                case Some(x) => calc(x, op.mkText, nne(y))
+                case None => throw new NCRejection("Memory is empty.")
+
+class NCIntCalcModelSpec extends AnyFunSuite:
+    test("test") {
+        Using.resource(new NCModelClient(NCIntCalcModelSpec.MDL)) { client =>
+            def check(txt: String, v: Int): Unit =
+                require(v == client.ask(txt, "userId").getBody)
+
+            check("2 + 2", 4)
+            check("3 * 4", 12)
+            check("/ two", 6)
+            check("+ twenty two", 28)
+            check("7 + 2", 9)
+        }
+    }
+
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
index 7a791630..ed41a5b1 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
@@ -281,7 +281,7 @@ class NCEnStopWordsTokenEnricher(addStopsSet: Set[String] = 
Set.empty, exclStops
         exclStems = if exclStopsSet == null then Set.empty else 
exclStopsSet.map(stem)
 
         def check(name: String, set: Set[String]): Unit =
-            if set.exists(_.exists(_.isWhitespace)) then E(s"$name contain a 
string with whitespaces.")
+            if set.exists(_.exists(_.isWhitespace)) then throw E(s"$name 
contain a string with whitespaces.")
 
         check("Additional synonyms", addStems)
         check("Excluded synonyms", exclStems)
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/parsers/NCNLPEntityParser.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/parsers/NCNLPEntityParser.scala
index 53ad9086..9f812749 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/parsers/NCNLPEntityParser.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/parsers/NCNLPEntityParser.scala
@@ -26,7 +26,7 @@ import java.util.stream.Collectors
   *
   */
 object NCNLPEntityParser:
-    private def id = "nlp:token"
+    private val id: String = "nlp:token"
 
 import org.apache.nlpcraft.nlp.parsers.NCNLPEntityParser.*
 
diff --git a/nlpcraft/src/test/resources/scan/samples.txt 
b/nlpcraft/src/test/resources/scan/samples.txt
deleted file mode 100644
index 01819dc8..00000000
--- a/nlpcraft/src/test/resources/scan/samples.txt
+++ /dev/null
@@ -1,19 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License.  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-Hi truck - drive without stopping at the last checkpoint
-

Reply via email to