This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-510
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-510 by this push:
new feacd698 Test client.
feacd698 is described below
commit feacd698c57831b50a8d591bf28ff54bc212726c
Author: Sergey Kamov <[email protected]>
AuthorDate: Sat Aug 6 20:24:13 2022 +0300
Test client.
---
.../lightswitch/NCModelValidationSpec.scala | 2 +-
.../lightswitch/NCModelValidationSpec.scala | 2 +-
.../lightswitch/NCModelValidationSpec.scala | 2 +-
.../examples/time/NCModelValidationSpec.scala | 2 +-
.../scala/org/apache/nlpcraft/NCCallbackData.scala | 40 ------------
.../scala/org/apache/nlpcraft/NCModelClient.scala | 71 ++++++++++++++++++++-
.../intent/matcher/NCIntentSolverManager.scala | 1 +
.../org/apache/nlpcraft/test/NCCallbackData.scala | 21 +++++++
.../apache/nlpcraft/test/NCModelTestClient.scala | 72 ++--------------------
.../nlpcraft/internal/impl/NCModelClientSpec.scala | 4 +-
.../internal/impl/NCModelClientSpec2.scala | 4 +-
.../internal/impl/NCModelClientSpec3.scala | 6 +-
12 files changed, 107 insertions(+), 120 deletions(-)
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 a58af3bc..ed6aa923 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,4 +28,4 @@ import scala.util.Using
*/
class NCModelValidationSpec:
@Test
- def test(): Unit = Using.resource(new NCModelTestClient(new
LightSwitchFrModel)) { _.validateSamples() }
+ def test(): Unit = Using.resource(new NCModelClient(new
LightSwitchFrModel)) {client => new NCModelTestClient(client).validateSamples()
}
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 ce5cd48d..67985ab1 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,4 +28,4 @@ import scala.util.Using
*/
class NCModelValidationSpec:
@Test
- def test(): Unit = Using.resource(new NCModelTestClient(new
LightSwitchRuModel)) { _.validateSamples() }
+ def test(): Unit = Using.resource(new NCModelClient(new
LightSwitchRuModel)) { client => new
NCModelTestClient(client).validateSamples() }
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 ef9102f0..e0c63315 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,7 @@ import scala.util.Using
* JUnit models validation.
*/
class NCModelValidationSpec:
- private def test(mdl: NCModel): Unit = Using.resource(new
NCModelTestClient(mdl)) { _.validateSamples() }
+ private def test(mdl: NCModel): Unit = Using.resource(new
NCModelClient(mdl)) { client => new NCModelTestClient(client).validateSamples()
}
@Test
def test(): Unit = test(new LightSwitchScalaModel())
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 ac0f6c24..af27666c 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,4 +28,4 @@ import scala.util.Using
*/
class NCModelValidationSpec:
@Test
- def test(): Unit = Using.resource(new NCModelTestClient(new TimeModel()))
{ _.validateSamples() }
+ def test(): Unit = Using.resource(new NCModelClient(new TimeModel())) {
client => new NCModelTestClient(client).validateSamples() }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCCallbackData.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCCallbackData.scala
deleted file mode 100644
index cb09c81c..00000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCCallbackData.scala
+++ /dev/null
@@ -1,40 +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
- *
- * 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
-
-/**
- *
- */
-trait NCCallbackData:
- /**
- *
- * @return
- */
- def getIntentId: String
-
- /**
- *
- * @return
- */
- def getCallbackArguments: List[List[NCEntity]]
-
- /**
- *
- * @return
- */
- def getCallback: List[List[NCEntity]] => NCResult
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
index 67a3c421..98b2e916 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
@@ -26,6 +26,7 @@ import
org.apache.nlpcraft.internal.dialogflow.NCDialogFlowManager
import org.apache.nlpcraft.internal.impl.*
import org.apache.nlpcraft.internal.intent.matcher.*
import org.apache.nlpcraft.internal.util.*
+import org.apache.nlpcraft.test.NCCallbackData
import java.util
import java.util.concurrent.*
@@ -40,7 +41,7 @@ import scala.concurrent.ExecutionContext
class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable:
verify()
- private[nlpcraft] val intents = NCModelScanner.scan(mdl)
+ private val intents = NCModelScanner.scan(mdl)
private val convMgr = NCConversationManager(mdl.getConfig)
private val dlgMgr = NCDialogFlowManager(mdl.getConfig)
private val plMgr = NCModelPipelineManager(mdl.getConfig, mdl.getPipeline)
@@ -78,7 +79,7 @@ class NCModelClient(mdl: NCModel) extends LazyLogging,
AutoCloseable:
* @param typ
* @return
*/
- private[nlpcraft] def ask0(txt: String, data: Map[String, Any], usrId:
String, typ: NCIntentSolveType): Either[NCResult, NCCallbackData] =
+ private def ask0(txt: String, data: Map[String, Any], usrId: String, typ:
NCIntentSolveType): Either[NCResult, NCCallbackData] =
val plData = plMgr.prepare(txt, data, usrId)
val userId = plData.request.getUserId
@@ -154,6 +155,55 @@ class NCModelClient(mdl: NCModel) extends LazyLogging,
AutoCloseable:
require(usrId != null, "User id cannot be null.")
require(usrId != null, "Filter cannot be null.")
dlgMgr.clear(usrId, (i: NCDialogFlowItem) => filter(i))
+
+ /**
+ *
+ */
+ private[nlpcraft] def validateSamples(): Unit =
+ case class Result(intentId: String, text: String, error:
Option[String], time: Long)
+
+ val userId = UUID.randomUUID().toString
+ val res = scala.collection.mutable.ArrayBuffer.empty[Result]
+
+ def now: Long = System.currentTimeMillis()
+
+ for (i <- intents; samples <- i.samples)
+ for (sample <- samples)
+ val start = now
+
+ val err: Option[String] =
+ try
+ val r = ask(sample, Map.empty, userId)
+
+ Option.when(r.getIntentId.isEmpty || r.getIntentId.get
!= i.intent.id)(s"Unexpected intent ID: '${r.getIntentId.getOrElse("(not
set)")}'")
+ catch case e: Throwable =>
+ logger.warn("Unexpected error.", e)
+ Option(e.getLocalizedMessage)
+
+ res += Result(i.intent.id, sample, err, now - start)
+
+ clearDialog(userId)
+ clearStm(userId)
+
+ val tbl = NCAsciiTable()
+ tbl #= ("Intent ID", "+/-", "Text", "Error", "ms.")
+
+ for (res <- res)
+ tbl += (
+ res.intentId,
+ if res.error.isEmpty then "OK" else "FAIL",
+ res.text,
+ res.error.getOrElse(""),
+ res.time
+ )
+
+ val passCnt = res.count(_.error.isEmpty)
+ val failCnt = res.count(_.error.isDefined)
+
+ tbl.info(logger, Option(s"Model auto-validation results: OK $passCnt,
FAIL $failCnt:"))
+
+ if failCnt > 0 then require(false, "Some tests failed.")
+
/**
*
*/
@@ -162,3 +212,20 @@ class NCModelClient(mdl: NCModel) extends LazyLogging,
AutoCloseable:
dlgMgr.close()
convMgr.close()
intentsMgr.close()
+
+ /**
+ *
+ * @param txt
+ * @param data
+ * @param usrId
+ * @param saveHist
+ * @return
+ */
+ private[nlpcraft] def debugAsk(txt: String, data: Map[String, AnyRef],
usrId: String, saveHist: Boolean): NCCallbackData =
+ require(txt != null, "Input text cannot be null.")
+ require(data != null, "Data cannot be null.")
+ require(usrId != null, "User id cannot be null.")
+ import NCIntentSolveType.*
+ ask0(txt, data, usrId, if saveHist then SEARCH else
SEARCH_NO_HISTORY).toOption.get
+
+ private[nlpcraft] def debugAsk(txt: String, usrId: String, saveHist:
Boolean): NCCallbackData = debugAsk(txt, Map.empty, usrId, saveHist)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
index 1f04f9f1..d43d398f 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/matcher/NCIntentSolverManager.scala
@@ -24,6 +24,7 @@ import
org.apache.nlpcraft.internal.conversation.NCConversationManager
import org.apache.nlpcraft.internal.dialogflow.NCDialogFlowManager
import org.apache.nlpcraft.internal.impl.NCCallbackInput
import org.apache.nlpcraft.internal.intent.*
+import org.apache.nlpcraft.test.NCCallbackData
import java.util.function.Function
import scala.annotation.targetName
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCCallbackData.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCCallbackData.scala
new file mode 100644
index 00000000..3c2e1004
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCCallbackData.scala
@@ -0,0 +1,21 @@
+package org.apache.nlpcraft.test
+
+import org.apache.nlpcraft.*
+
+/**
+ * */
+trait NCCallbackData:
+ /**
+ *
+ * @return */
+ def getIntentId: String
+
+ /**
+ *
+ * @return */
+ def getCallbackArguments: List[List[NCEntity]]
+
+ /**
+ *
+ * @return */
+ def getCallback: List[List[NCEntity]] => NCResult
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala
index 8d76bdee..5ada8efc 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala
@@ -24,71 +24,9 @@ import
org.apache.nlpcraft.internal.intent.matcher.NCIntentSolveType.*
import java.util.UUID
-class NCModelTestClient(mdl: NCModel) extends NCModelClient(mdl):
- /**
- *
- * @param txt
- * @param data
- * @param usrId
- * @param saveHist
- * @return
- */
+class NCModelTestClient(client: NCModelClient):
def debugAsk(txt: String, data: Map[String, AnyRef], usrId: String,
saveHist: Boolean): NCCallbackData =
- require(txt != null, "Input text cannot be null.")
- require(data != null, "Data cannot be null.")
- require(usrId != null, "User id cannot be null.")
- import NCIntentSolveType.*
- ask0(txt, data, usrId, if saveHist then SEARCH else
SEARCH_NO_HISTORY).toOption.get
-
- def debugAsk(txt: String, usrId: String, saveHist: Boolean):
NCCallbackData = debugAsk(txt, Map.empty, usrId, saveHist)
-
-
-
- /**
- *
- */
- def validateSamples(): Unit =
- case class Result(intentId: String, text: String, error:
Option[String], time: Long)
-
- val userId = UUID.randomUUID().toString
- val res = scala.collection.mutable.ArrayBuffer.empty[Result]
-
- def now: Long = System.currentTimeMillis()
-
- for (i <- intents; samples <- i.samples)
- for (sample <- samples)
- val start = now
-
- val err: Option[String] =
- try
- val r = ask(sample, Map.empty, userId)
-
- Option.when(r.getIntentId.isEmpty || r.getIntentId.get
!= i.intent.id)(s"Unexpected intent ID: '${r.getIntentId.getOrElse("(not
set)")}'")
- catch case e: Throwable =>
- logger.warn("Unexpected error.", e)
- Option(e.getLocalizedMessage)
-
- res += Result(i.intent.id, sample, err, now - start)
-
- clearDialog(userId)
- clearStm(userId)
-
- val tbl = NCAsciiTable()
- tbl #= ("Intent ID", "+/-", "Text", "Error", "ms.")
-
- for (res <- res)
- tbl += (
- res.intentId,
- if res.error.isEmpty then "OK" else "FAIL",
- res.text,
- res.error.getOrElse(""),
- res.time
- )
-
- val passCnt = res.count(_.error.isEmpty)
- val failCnt = res.count(_.error.isDefined)
-
- tbl.info(logger, Option(s"Model auto-validation results: OK $passCnt,
FAIL $failCnt:"))
-
- if failCnt > 0 then require(false, "Some tests failed.")
-
+ client.debugAsk(txt, data, usrId, saveHist)
+ def debugAsk(txt: String, usrId: String, saveHist: Boolean):
NCCallbackData =
+ client.debugAsk(txt, Map.empty, usrId, saveHist)
+ def validateSamples(): Unit = client.validateSamples()
\ No newline at end of file
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
index f9bc8f46..ff804b6d 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec.scala
@@ -47,13 +47,13 @@ class NCModelClientSpec:
private def test0(mdl: NCTestModelAdapter): Unit =
mdl.pipeline.entParsers +=
NCTestUtils.mkEnSemanticParser("models/lightswitch_model.yaml")
- Using.resource(new NCModelTestClient(mdl)) { client =>
+ Using.resource(new NCModelClient(mdl)) { client =>
val res = client.ask("Lights on at second floor kitchen", "userId")
println(s"Intent: ${res.getIntentId}")
println(s"Body: ${res.getBody}")
- client.validateSamples()
+ new NCModelTestClient(client).validateSamples()
val winner = client.debugAsk("Lights on at second floor kitchen",
"userId", true)
println(s"Winner intent: ${winner.getIntentId}")
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
index 5f643872..86873401 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec2.scala
@@ -48,9 +48,9 @@ class NCModelClientSpec2:
def onMatch(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("t1")
act: NCEntity, @NCIntentTerm("t2List") locs: List[NCEntity]): NCResult =
E("Shouldn't be called.")
- Using.resource(new NCModelTestClient(mdl)) { client =>
+ Using.resource(new NCModelClient(mdl)) { client =>
case class Result(txt: String):
- private val wi = client.debugAsk(txt, "userId", true)
+ private val wi = new NCModelTestClient(client).debugAsk(txt,
"userId", true)
private val allArgs: List[List[NCEntity]] =
wi.getCallbackArguments
val intentId: String = wi.getIntentId
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
index ceb80a31..414b4c45 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec3.scala
@@ -22,7 +22,7 @@ import org.apache.nlpcraft.annotations.*
import org.apache.nlpcraft.nlp.entity.parser.*
import org.apache.nlpcraft.nlp.entity.parser.semantic.*
import org.apache.nlpcraft.nlp.util.*
-import org.apache.nlpcraft.test.NCModelTestClient
+import org.apache.nlpcraft.test.{NCCallbackData, NCModelTestClient}
import org.junit.jupiter.api.Test
import java.util
@@ -47,8 +47,8 @@ class NCModelClientSpec3:
@NCIntent("intent=i1 term(t1)={# == 'e1'}")
def onMatch(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("t1")
t1: NCEntity): NCResult = NCResult("Data", NCResultType.ASK_RESULT)
- Using.resource(new NCModelTestClient(mdl)) { client =>
- def ask(): NCCallbackData = client.debugAsk("e1", "userId", true)
+ Using.resource(new NCModelClient(mdl)) { client =>
+ def ask(): NCCallbackData = new
NCModelTestClient(client).debugAsk("e1", "userId", true)
def execCallback(cb: NCCallbackData): NCResult =
cb.getCallback.apply(cb.getCallbackArguments)
def execCallbackOk(cb: NCCallbackData): Unit = println(s"Result:
${execCallback(cb).getBody}")
def execCallbackFail(cb: NCCallbackData): Unit =