This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-510-1 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 135f614cdfba46220753d12fa084aedadbe7126e Author: Sergey Kamov <skhdlem...@gmail.com> AuthorDate: Wed Aug 10 11:09:58 2022 +0300 Validation fixes. --- .../lightswitch/NCModelValidationSpec.scala | 2 +- .../lightswitch/NCModelValidationSpec.scala | 2 +- .../lightswitch/NCModelValidationSpec.scala | 2 +- .../examples/time/NCModelValidationSpec.scala | 2 +- .../scala/org/apache/nlpcraft/NCModelClient.scala | 12 +++- .../nlpcraft/validation/NCModelValidator.scala | 12 +--- .../nlpcraft/internal/impl/NCModelClientSpec.scala | 12 ++-- .../internal/impl/NCModelClientSpec2.scala | 71 +++++++++++----------- .../internal/impl/NCModelClientSpec3.scala | 4 +- 9 files changed, 60 insertions(+), 59 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 b99b3d37..a5b0ab62 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(NCModelValidator(new LightSwitchFrModel)) { _.validateSamples() } + def test(): Unit = Using.resource(new NCModelClient(new LightSwitchFrModel)) { _.getModelValidator.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 f9ad2d28..e00a18b1 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(NCModelValidator(new LightSwitchRuModel)) { _.validateSamples() } + def test(): Unit = Using.resource(new NCModelClient(new LightSwitchRuModel)) { _.getModelValidator.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 ea852bc0..57fbb618 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(NCModelValidator(mdl)) { _.validateSamples() } + private def test(mdl: NCModel): Unit = Using.resource(NCModelClient(mdl)) { _.getModelValidator.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 4a8ae60a..0e912c08 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(NCModelValidator(new TimeModel())) { _.validateSamples() } + def test(): Unit = Using.resource(new NCModelClient(new TimeModel())) { _.getModelValidator.validateSamples() } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala index e73ba8cc..16bf86af 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala @@ -26,7 +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.validation.NCFiredIntent +import org.apache.nlpcraft.validation.{NCFiredIntent, NCModelValidator} import java.util import java.util.concurrent.* @@ -48,6 +48,10 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: private val intentsMgr = NCIntentSolverManager(dlgMgr, convMgr, intents.map(p => p.intent -> p.function).toMap) init() + + private val validator = new NCModelValidator(): + override def validateAsk(txt: String, usrId: String, saveHist: Boolean, data: Map[String, AnyRef]): NCFiredIntent = validateAsk0(txt, usrId, saveHist, data) + override def validateSamples(): Unit = validateSamples0() /** * */ @@ -159,7 +163,7 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: /** * */ - private[nlpcraft] def validateSamples(): Unit = + private def validateSamples0(): Unit = case class Result(intentId: String, text: String, error: Option[String], time: Long) val userId = UUID.randomUUID().toString @@ -221,9 +225,11 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: * @param saveHist * @return */ - private[nlpcraft] def debugAsk(txt: String, usrId: String, saveHist: Boolean, data: Map[String, AnyRef]): NCFiredIntent = + private def validateAsk0(txt: String, usrId: String, saveHist: Boolean, data: Map[String, AnyRef]): NCFiredIntent = 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 getModelValidator: NCModelValidator = validator diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/validation/NCModelValidator.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/validation/NCModelValidator.scala index 160f7345..baae4890 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/validation/NCModelValidator.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/validation/NCModelValidator.scala @@ -25,12 +25,6 @@ import org.apache.nlpcraft.internal.intent.matcher.NCIntentSolveType.* import java.util.UUID -object NCModelValidator: - def apply(client: NCModelClient): NCModelValidator = new NCModelValidator(client) - def apply(mdl: NCModel): NCModelValidator = new NCModelValidator(new NCModelClient(mdl), true) - -class NCModelValidator(client: NCModelClient, clientClose: Boolean = false) extends LazyLogging, AutoCloseable: - override def close(): Unit = if clientClose then client.close() - - def validateAsk(txt: String, usrId: String, saveHist: Boolean = false, data: Map[String, AnyRef] = Map.empty): NCFiredIntent = client.debugAsk(txt, usrId, saveHist, data) - def validateSamples(): Unit = client.validateSamples() \ No newline at end of file +trait NCModelValidator: + def validateAsk(txt: String, usrId: String, saveHist: Boolean = false, data: Map[String, AnyRef] = Map.empty): NCFiredIntent + def validateSamples(): Unit \ 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 4399a1db..a861f523 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 @@ -53,14 +53,14 @@ class NCModelClientSpec: println(s"Intent: ${res.getIntentId}") println(s"Body: ${res.getBody}") - Using.resource(NCModelValidator(mdl)) { validator => - validator.validateSamples() + val validator = client.getModelValidator - val winner = validator.validateAsk("Lights on at second floor kitchen", "userId", true) + validator.validateSamples() - println(s"Winner intent: ${winner.getIntentId}") - println("Entities: \n" + winner.getCallbackArguments.map(p => p.map(s).mkString(", ")).mkString("\n")) - } + val winner = validator.validateAsk("Lights on at second floor kitchen", "userId", true) + + println(s"Winner intent: ${winner.getIntentId}") + println("Entities: \n" + winner.getCallbackArguments.map(p => p.map(s).mkString(", ")).mkString("\n")) } /** 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 c438547c..8527ed78 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 @@ -49,54 +49,53 @@ class NCModelClientSpec2: E("Shouldn't be called.") Using.resource(new NCModelClient(mdl)) { client => - Using.resource(NCModelValidator(client)) { validator => - case class Result(txt: String): - private val wi = validator.validateAsk(txt, "userId", true) - private val allArgs: List[List[NCEntity]] = wi.getCallbackArguments + val validator = client.getModelValidator + case class Result(txt: String): + private val wi = validator.validateAsk(txt, "userId", true) + private val allArgs: List[List[NCEntity]] = wi.getCallbackArguments - val intentId: String = wi.getIntentId - val size: Int = allArgs.size + val intentId: String = wi.getIntentId + val size: Int = allArgs.size - lazy val first: Seq[NCEntity] = allArgs.head - lazy val second: Seq[NCEntity] = allArgs.last + lazy val first: Seq[NCEntity] = allArgs.head + lazy val second: Seq[NCEntity] = allArgs.last - // 1. One argument. - var res = Result("e1") + // 1. One argument. + var res = Result("e1") - require(res.intentId == "i1") - require(res.size == 2) + require(res.intentId == "i1") + require(res.size == 2) - def check(e: NCEntity, txt: String): Unit = - require(e.mkText == txt) - // All data aren't lost. - require(e.getTokens.head.keysSet.contains("lemma")) + def check(e: NCEntity, txt: String): Unit = + require(e.mkText == txt) + // All data aren't lost. + require(e.getTokens.head.keysSet.contains("lemma")) - require(res.first.size == 1) - check(res.first.head, "e1") + require(res.first.size == 1) + check(res.first.head, "e1") - require(res.second.isEmpty) + require(res.second.isEmpty) - // 2. One argument. - res = Result("e1 e2 e2") + // 2. One argument. + res = Result("e1 e2 e2") - require(res.intentId == "i1") - require(res.size == 2) + require(res.intentId == "i1") + require(res.size == 2) - require(res.first.size == 1) - check(res.first.head, "e1") + require(res.first.size == 1) + check(res.first.head, "e1") - require(res.second.size == 2) - check(res.second.head, "e2") - check(res.second.last, "e2") + require(res.second.size == 2) + check(res.second.head, "e2") + check(res.second.last, "e2") - // 3. No winners. - try - validator.validateAsk("x", "userId", false) - require(false) - catch - case e: NCRejection => println(s"Expected rejection: ${e.getMessage}") - case e: Throwable => throw e - } + // 3. No winners. + try + validator.validateAsk("x", "userId", false) + require(false) + catch + case e: NCRejection => println(s"Expected rejection: ${e.getMessage}") + case e: Throwable => throw e } 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 cc8709b9..28b16071 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 @@ -48,7 +48,9 @@ class NCModelClientSpec3: def onMatch(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("t1") t1: NCEntity): NCResult = NCResult("Data", NCResultType.ASK_RESULT) Using.resource(new NCModelClient(mdl)) { client => - def ask(): NCFiredIntent = Using.resource(NCModelValidator(client)) { _.validateAsk("e1", "userId", true) } + val validator = client.getModelValidator + + def ask(): NCFiredIntent = validator.validateAsk("e1", "userId", true) def execCallback(cb: NCFiredIntent): NCResult = cb.getCallback.apply(cb.getCallbackArguments) def execCallbackOk(cb: NCFiredIntent): Unit = println(s"Result: ${execCallback(cb).getBody}") def execCallbackFail(cb: NCFiredIntent): Unit =