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
commit fd2522f7c4af6991627e4062633b748b66106370 Author: Sergey Kamov <[email protected]> AuthorDate: Sat Aug 6 19:56:26 2022 +0300 Test client. --- .../lightswitch/NCModelValidationSpec.scala | 3 +- .../lightswitch/NCModelValidationSpec.scala | 3 +- .../lightswitch/NCModelValidationSpec.scala | 3 +- .../examples/time/NCModelValidationSpec.scala | 3 +- .../scala/org/apache/nlpcraft/NCModelClient.scala | 70 +--------------- .../apache/nlpcraft/test/NCModelTestClient.scala | 94 ++++++++++++++++++++++ .../nlpcraft/internal/impl/NCModelClientSpec.scala | 3 +- .../internal/impl/NCModelClientSpec2.scala | 3 +- .../internal/impl/NCModelClientSpec3.scala | 3 +- 9 files changed, 110 insertions(+), 75 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 2871ec51..a58af3bc 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 @@ -18,6 +18,7 @@ package org.apache.nlpcraft.examples.lightswitch import org.apache.nlpcraft.* +import org.apache.nlpcraft.test.NCModelTestClient import org.junit.jupiter.api.* import scala.util.Using @@ -27,4 +28,4 @@ import scala.util.Using */ class NCModelValidationSpec: @Test - def test(): Unit = Using.resource(new NCModelClient(new LightSwitchFrModel)) { _.validateSamples() } + def test(): Unit = Using.resource(new NCModelTestClient(new LightSwitchFrModel)) { _.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 6b031eb9..ce5cd48d 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 @@ -18,6 +18,7 @@ package org.apache.nlpcraft.examples.lightswitch import org.apache.nlpcraft.* +import org.apache.nlpcraft.test.NCModelTestClient import org.junit.jupiter.api.* import scala.util.Using @@ -27,4 +28,4 @@ import scala.util.Using */ class NCModelValidationSpec: @Test - def test(): Unit = Using.resource(new NCModelClient(new LightSwitchRuModel)) { _.validateSamples() } + def test(): Unit = Using.resource(new NCModelTestClient(new LightSwitchRuModel)) { _.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 b95c943e..ef9102f0 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 @@ -18,6 +18,7 @@ package org.apache.nlpcraft.examples.lightswitch import org.apache.nlpcraft.* +import org.apache.nlpcraft.test.NCModelTestClient import org.junit.jupiter.api.* import scala.util.Using @@ -26,7 +27,7 @@ import scala.util.Using * JUnit models validation. */ class NCModelValidationSpec: - private def test(mdl: NCModel): Unit = Using.resource(new NCModelClient(mdl)) { _.validateSamples() } + private def test(mdl: NCModel): Unit = Using.resource(new NCModelTestClient(mdl)) { _.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 a79a81c1..ac0f6c24 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 @@ -18,6 +18,7 @@ package org.apache.nlpcraft.examples.time import org.apache.nlpcraft.* +import org.apache.nlpcraft.test.NCModelTestClient import org.junit.jupiter.api.* import scala.util.Using @@ -27,4 +28,4 @@ import scala.util.Using */ class NCModelValidationSpec: @Test - def test(): Unit = Using.resource(new NCModelClient(new TimeModel())) { _.validateSamples() } + def test(): Unit = Using.resource(new NCModelTestClient(new TimeModel())) { _.validateSamples() } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala index 4b5553e1..67a3c421 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala @@ -40,7 +40,7 @@ import scala.concurrent.ExecutionContext class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: verify() - private val intents = NCModelScanner.scan(mdl) + private[nlpcraft] 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 +78,7 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: * @param typ * @return */ - private def ask0(txt: String, data: Map[String, Any], usrId: String, typ: NCIntentSolveType): Either[NCResult, NCCallbackData] = + private[nlpcraft] 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,55 +154,6 @@ 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)) - - /** - * - */ - 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.") - /** * */ @@ -211,20 +162,3 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: dlgMgr.close() convMgr.close() intentsMgr.close() - - /** - * - * @param txt - * @param data - * @param usrId - * @param saveHist - * @return - */ - 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) diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala new file mode 100644 index 00000000..8d76bdee --- /dev/null +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/test/NCModelTestClient.scala @@ -0,0 +1,94 @@ +/* + * 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.test + +import org.apache.nlpcraft.* +import org.apache.nlpcraft.internal.ascii.NCAsciiTable +import org.apache.nlpcraft.internal.intent.matcher.NCIntentSolveType +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 + */ + 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.") + 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 4452a1c4..f9bc8f46 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 @@ -22,6 +22,7 @@ import org.apache.nlpcraft.annotations.* import org.apache.nlpcraft.nlp.entity.parser.* import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser import org.apache.nlpcraft.nlp.util.* +import org.apache.nlpcraft.test.NCModelTestClient import org.junit.jupiter.api.Test import scala.jdk.CollectionConverters.* @@ -46,7 +47,7 @@ class NCModelClientSpec: private def test0(mdl: NCTestModelAdapter): Unit = mdl.pipeline.entParsers += NCTestUtils.mkEnSemanticParser("models/lightswitch_model.yaml") - Using.resource(new NCModelClient(mdl)) { client => + Using.resource(new NCModelTestClient(mdl)) { client => val res = client.ask("Lights on at second floor kitchen", "userId") println(s"Intent: ${res.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 4333fa2b..5f643872 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 @@ -22,6 +22,7 @@ import org.apache.nlpcraft.annotations.* import org.apache.nlpcraft.nlp.entity.parser.* import org.apache.nlpcraft.nlp.entity.parser.semantic.{NCSemanticTestElement, *} import org.apache.nlpcraft.nlp.util.* +import org.apache.nlpcraft.test.NCModelTestClient import org.junit.jupiter.api.Test import java.util @@ -47,7 +48,7 @@ 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 NCModelClient(mdl)) { client => + Using.resource(new NCModelTestClient(mdl)) { client => case class Result(txt: String): private val wi = client.debugAsk(txt, "userId", true) private val allArgs: List[List[NCEntity]] = wi.getCallbackArguments 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 332822ed..ceb80a31 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,6 +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.junit.jupiter.api.Test import java.util @@ -46,7 +47,7 @@ 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 NCModelClient(mdl)) { client => + Using.resource(new NCModelTestClient(mdl)) { client => def ask(): NCCallbackData = 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}")
