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/19 09:16:51 UTC

[incubator-nlpcraft] branch master updated (6d42535 -> 5bc79df)

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

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


    from 6d42535  Minor fixes.
     new 26cf63e  Test added.
     new 5bc79df  Test fixes.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../internal/impl/NCModelPingPongSpec.scala        | 111 +++++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100644 nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala

[incubator-nlpcraft] 02/02: Test fixes.

Posted by se...@apache.org.
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

commit 5bc79df39bd7c701664afe2ec583b22e411e61f0
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Sat Feb 19 12:16:43 2022 +0300

    Test fixes.
---
 .../internal/impl/NCModelPingPongSpec.scala        | 28 ++++++++++++----------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
index e128aab..98658bd 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
@@ -41,25 +41,27 @@ class NCModelPingPongSpec:
 
     private val MDL: NCTestModelAdapter =
         new NCTestModelAdapter():
-            // TODO: how to reset it on any request?
-            var state: String = null
-
             @NCIntent("intent=dialog term(dialog)={# == 'dialog'}")
-            def onDialog(@NCIntentTerm("dialog") dialog: NCEntity): NCResult =
-                state = "dialog"
-                R(ASK_DIALOG, s"Confirm your request 'dialog' request: ${dialog.mkText()}")
+            def onDialog(im: NCIntentMatch, @NCIntentTerm("dialog") dialog: NCEntity): NCResult =
+                R(ASK_DIALOG, s"Confirm your request 'dialog'!")
 
             @NCIntent("intent=confirm term(confirm)={# == 'confirm'}")
-            def onConfirm(@NCIntentTerm("confirm") confirm: NCEntity): NCResult =
-                if state == null || state != "dialog" then throw new NCRejection("Nothing to confirm.")
+            def onConfirm(im: NCIntentMatch, @NCIntentTerm("confirm") confirm: NCEntity): NCResult =
+                // TODO: I can compare only with last matched.
+                val lastIntentId =
+                    im.getContext.
+                        getConversation.
+                        getDialogFlow.asScala.lastOption.
+                        flatMap(p => Option(p.getIntentMatch.getIntentId)).orNull
+
+                if lastIntentId != "dialog" then
+                    throw new NCRejection("Nothing to confirm.")
 
-                state = "confirm"
-                R(ASK_RESULT, s"Confirmed by: ${confirm.mkText()}")
+                R(ASK_RESULT, s"'dialog' confirmed.")
 
             @NCIntent("intent=other term(other)={# == 'other'}")
-            def onOther(@NCIntentTerm("other") other: NCEntity): NCResult =
-                state = "other"
-                R(ASK_RESULT, s"Any by: ${other.mkText()}")
+            def onOther(im: NCIntentMatch, @NCIntentTerm("other") other: NCEntity): NCResult =
+                R(ASK_RESULT, s"Some request by: ${other.mkText()}")
 
     MDL.getPipeline.getEntityParsers.add(
         new NCSemanticEntityParser(

[incubator-nlpcraft] 01/02: Test added.

Posted by se...@apache.org.
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

commit 26cf63ec40e1ab237f05416e15765c8e0f76ee5c
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Sat Feb 19 11:54:06 2022 +0300

    Test added.
---
 .../internal/impl/NCModelPingPongSpec.scala        | 109 +++++++++++++++++++++
 1 file changed, 109 insertions(+)

diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
new file mode 100644
index 0000000..e128aab
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelPingPongSpec.scala
@@ -0,0 +1,109 @@
+/*
+ * 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.internal.impl
+
+import org.apache.nlpcraft.*
+import org.apache.nlpcraft.nlp.entity.parser.semantic.*
+import org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer
+import org.apache.nlpcraft.NCResultType.*
+import org.apache.nlpcraft.nlp.util.NCTestModelAdapter
+import org.apache.nlpcraft.nlp.util.opennlp.*
+import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+
+import scala.jdk.CollectionConverters.*
+import scala.util.Using
+
+/**
+  *
+  */
+class NCModelPingPongSpec:
+    private var client: NCModelClient = null
+
+    case class R(resType: NCResultType, txt: String) extends NCResult:
+        this.setType(resType)
+        this.setBody(txt)
+        override def toString: String = s"$resType ($txt)"
+
+    private val MDL: NCTestModelAdapter =
+        new NCTestModelAdapter():
+            // TODO: how to reset it on any request?
+            var state: String = null
+
+            @NCIntent("intent=dialog term(dialog)={# == 'dialog'}")
+            def onDialog(@NCIntentTerm("dialog") dialog: NCEntity): NCResult =
+                state = "dialog"
+                R(ASK_DIALOG, s"Confirm your request 'dialog' request: ${dialog.mkText()}")
+
+            @NCIntent("intent=confirm term(confirm)={# == 'confirm'}")
+            def onConfirm(@NCIntentTerm("confirm") confirm: NCEntity): NCResult =
+                if state == null || state != "dialog" then throw new NCRejection("Nothing to confirm.")
+
+                state = "confirm"
+                R(ASK_RESULT, s"Confirmed by: ${confirm.mkText()}")
+
+            @NCIntent("intent=other term(other)={# == 'other'}")
+            def onOther(@NCIntentTerm("other") other: NCEntity): NCResult =
+                state = "other"
+                R(ASK_RESULT, s"Any by: ${other.mkText()}")
+
+    MDL.getPipeline.getEntityParsers.add(
+        new NCSemanticEntityParser(
+            new NCEnSemanticPorterStemmer,
+            EN_PIPELINE.getTokenParser,
+            Seq(
+                NCSemanticTestElement("dialog", "my command"),
+                NCSemanticTestElement("confirm", "my confirm"),
+                NCSemanticTestElement("other", "my other")
+            ).asJava
+        )
+    )
+
+    @BeforeEach
+    def setUp(): Unit = client = new NCModelClient(MDL)
+
+    @AfterEach
+    def tearDown(): Unit = client.close()
+
+    private def ask(txt: String, expType: NCResultType): Unit =
+        val res = client.ask(txt, null, "userId")
+        println(s"Request [text=$txt, result=$res]")
+        require(res.getType == expType)
+
+    /**
+      *
+      */
+    @Test
+    def test(): Unit =
+        ask("my command", ASK_DIALOG)
+        ask("my confirm", ASK_RESULT)
+
+    /**
+      *
+      */
+    @Test
+    def test2(): Unit =
+        // 1. Nothing to confirm. No history.
+        try ask("my confirm", ASK_RESULT) catch case e: NCRejection => println("Expected reject.")
+
+        // 2. Nothing to confirm. Last question is not `dialog`.
+        ask("my other", ASK_RESULT)
+        try ask("my confirm", ASK_RESULT) catch case e: NCRejection => println("Expected reject.")
+
+        // 3. Last question is `dialog`. Can be confirmed.
+        ask("my command", ASK_DIALOG)
+        ask("my confirm", ASK_RESULT)