This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-522 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 8bf9e967403fd83ec86c48240ec65b774e8904af Author: Sergey Kamov <[email protected]> AuthorDate: Sun Feb 19 10:36:48 2023 +0400 WIP. --- .../examples/time/CalculatorModelSpec.scala | 5 ++- .../lightswitch/LightSwitchFrModelSpec.scala | 4 +- .../lightswitch/LightSwitchRuModelSpec.scala | 6 ++- .../lightswitch/LightSwitchModelSpec.scala | 4 +- .../examples/pizzeria/PizzeriaModelSpec.scala | 4 +- .../pizzeria/cli/PizzeriaModelClientCli.scala | 1 - .../pizzeria/cli/PizzeriaModelServer.scala | 50 ++++++++++++++-------- .../nlpcraft/examples/time/TimeModelSpec.scala | 4 +- .../scala/org/apache/nlpcraft/NCModelClient.scala | 9 ++-- .../internal/conversation/NCConversationSpec.scala | 7 ++- .../conversation/NCConversationSpec2.scala | 4 +- .../conversation/NCConversationTimeoutSpec.scala | 2 +- .../internal/impl/NCModelCallbacksSpec.scala | 2 +- .../nlpcraft/internal/impl/NCModelClientSpec.scala | 4 +- .../internal/impl/NCModelClientSpec2.scala | 10 ++--- .../internal/impl/NCModelClientSpec3.scala | 4 +- .../internal/impl/NCModelClientSpec4.scala | 4 +- .../internal/impl/NCModelPingPongSpec.scala | 15 ++++--- .../compiler/NCIDLFragmentsOverridingSpec.scala | 2 +- .../compiler/NCIDLFragmentsOverridingSpec2.scala | 2 +- .../apache/nlpcraft/nlp/NCEntityMapperSpec.scala | 2 +- .../apache/nlpcraft/nlp/NCEntityParserSpec.scala | 2 +- .../org/apache/nlpcraft/nlp/util/NCTestUtils.scala | 2 +- 23 files changed, 87 insertions(+), 62 deletions(-) diff --git a/nlpcraft-examples/calculator/src/test/scala/org/apache/nlpcraft/examples/time/CalculatorModelSpec.scala b/nlpcraft-examples/calculator/src/test/scala/org/apache/nlpcraft/examples/time/CalculatorModelSpec.scala index 8a95bd1e..f766dc8c 100644 --- a/nlpcraft-examples/calculator/src/test/scala/org/apache/nlpcraft/examples/time/CalculatorModelSpec.scala +++ b/nlpcraft-examples/calculator/src/test/scala/org/apache/nlpcraft/examples/time/CalculatorModelSpec.scala @@ -28,7 +28,10 @@ import scala.util.Using class CalculatorModelSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new CalculatorModel())) { client => - def check(txt: String, v: Int): Unit = require(v == client.ask(txt, "userId").getBody) + def check(txt: String, v: Int): Unit = + val res = client.ask(txt, "userId") + require(res.isSuccess) + require(v == res.get.getBody) check("2 + 2", 4) check("3 * 4", 12) diff --git a/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModelSpec.scala b/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModelSpec.scala index bbef184e..d1f217a6 100644 --- a/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModelSpec.scala +++ b/nlpcraft-examples/lightswitch-fr/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchFrModelSpec.scala @@ -29,7 +29,9 @@ class LightSwitchFrModelSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new LightSwitchFrModel)) { client => def check(txt: String): Unit = - require(client.debugAsk(txt, "userId", true).getIntentId == "ls") + val res = client.debugAsk(txt, "userId", true) + require(res.isSuccess) + require(res.get.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/LightSwitchRuModelSpec.scala b/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModelSpec.scala index e3548f10..82a0103f 100644 --- a/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModelSpec.scala +++ b/nlpcraft-examples/lightswitch-ru/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchRuModelSpec.scala @@ -28,8 +28,10 @@ import scala.util.Using class LightSwitchRuModelSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new LightSwitchRuModel)) { client => - def check(txt: String): Unit = - require(client.debugAsk(txt, "userId", true).getIntentId == "ls") + def check(txt: String): Unit = + val res = client.debugAsk(txt, "userId", true) + require(res.isSuccess) + require(res.get.getIntentId == "ls") check("Выключи свет по всем доме") check("Выруби электричество!") diff --git a/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelSpec.scala b/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelSpec.scala index bebb30bc..a03851da 100644 --- a/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelSpec.scala +++ b/nlpcraft-examples/lightswitch/src/test/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelSpec.scala @@ -29,7 +29,9 @@ class LightSwitchModelSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new LightSwitchModel())) { client => def check(txt: String): Unit = - require(client.debugAsk(txt, "userId", true).getIntentId == "ls") + val res = client.debugAsk(txt, "userId", true) + require(res.isSuccess) + require(res.get.getIntentId == "ls") check("Turn the lights off in the entire house.") check("Turn off all lights now") diff --git a/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala index 974c6ae3..c7112d3c 100644 --- a/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala +++ b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala @@ -85,7 +85,9 @@ class PizzeriaModelSpec extends AnyFunSuite with BeforeAndAfter: for (((txt, expType), idx) <- reqs.zipWithIndex) try mdl.clearLastExecutedOrder() - val resp = client.ask(txt, "userId") + val res = client.ask(txt, "userId") + require(res.isSuccess) + val resp = res.get testMsgs += s">> Request: $txt" testMsgs += s">> Response: '${resp.getType}': ${resp.getBody}" diff --git a/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala index b2beb87a..fe78c040 100644 --- a/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala +++ b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelClientCli.scala @@ -80,7 +80,6 @@ object PizzeriaModelClientCli extends LazyLogging : if in.nonEmpty then println(ask(in)) println catch - case e: NCRejection => println(s"Request rejected: ${e.getMessage}") case e: IOException => if applStarted then println(s"IO error: ${e.getMessage}") case e: Throwable => if applStarted then diff --git a/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala index c447b462..b22985d4 100644 --- a/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala +++ b/nlpcraft-examples/pizzeria/src/test/scala/org/apache/nlpcraft/examples/pizzeria/cli/PizzeriaModelServer.scala @@ -54,24 +54,38 @@ object PizzeriaModelServer: out.write(arr, 0, arr.length) } - try - val req = - e.getRequestMethod match - case "GET" => e.getRequestURI.toString.split("\\?").last - case "POST" => Using.resource(new BufferedReader(new InputStreamReader(e.getRequestBody))) { _.readLine } - case _ => throw new Exception(s"Unsupported request method: ${e.getRequestMethod}") - - if req == null || req.isEmpty then Exception(s"Empty request.") - - val resp = nlpClient.ask(req, "userId") - val prompt = if resp.getType == ASK_DIALOG then "(Your should answer on the model's question below)\n" else "" - doResponse(s"$prompt${resp.getBody}") - catch - case e: NCRejection => doResponse(s"Request rejected: ${e.getMessage}") - case e: Throwable => - System.err.println("Unexpected error.") - e.printStackTrace() - doResponse(s"Unexpected error: ${e.getMessage}") + val req = + try + val req = + e.getRequestMethod match + case "GET" => e.getRequestURI.toString.split("\\?").last + case "POST" => Using.resource(new BufferedReader(new InputStreamReader(e.getRequestBody))) { _.readLine } + case _ => throw new Exception(s"Unsupported request method: ${e.getRequestMethod}") + + if req == null || req.isEmpty then Exception(s"Empty request.") + + Some(req) + catch + case e: Throwable => + System.err.println("Unexpected error.") + e.printStackTrace() + + None + + if req.isDefined then + val res = nlpClient.ask(req.get, "userId") + + if res.isSuccess then + val resp = res.get + val prompt = if resp.getType == ASK_DIALOG then "(Your should answer on the model's question below)\n" else "" + doResponse(s"$prompt${resp.getBody}") + else + res.failed.get match + case e: NCRejection => doResponse(s"Request rejected: ${e.getMessage}") + case e: Throwable => + System.err.println("Unexpected error.") + e.printStackTrace() + doResponse(s"Unexpected error: ${e.getMessage}") }) srv.start() diff --git a/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/TimeModelSpec.scala b/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/TimeModelSpec.scala index f246ce50..5f99e8a8 100644 --- a/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/TimeModelSpec.scala +++ b/nlpcraft-examples/time/src/test/scala/org/apache/nlpcraft/examples/time/TimeModelSpec.scala @@ -29,7 +29,9 @@ class TimeModelSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new TimeModel())) { client => def check(txt: String, intentId: String): Unit = - require(client.debugAsk(txt, "userId", true).getIntentId == intentId) + val res = client.debugAsk(txt, "userId", true) + require(res.isSuccess) + require(res.get.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/src/main/scala/org/apache/nlpcraft/NCModelClient.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala index cf007ae8..5cdc6ab7 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala @@ -32,6 +32,7 @@ import java.util.concurrent.* import java.util.concurrent.atomic.* import java.util.{Objects, UUID} import scala.concurrent.ExecutionContext +import scala.util.Try /** * Client API to issue requests again given model. This the primary method of interacting with NLPCraft @@ -135,8 +136,8 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: * automatically by the processing logic as well as can be thrown by the user from the intent callback. * @throws NCException Thrown in case of any internal errors processing the user input. */ - def ask(txt: String, usrId: String, data: Map[String, AnyRef] = Map.empty): NCResult = - ask0(txt, data, usrId, NCIntentSolveType.REGULAR).swap.toOption.get + def ask(txt: String, usrId: String, data: Map[String, AnyRef] = Map.empty): Try[NCResult] = + Try(ask0(txt, data, usrId, NCIntentSolveType.REGULAR).swap.toOption.get) /** * Removes all entities from the short-term-memory (STM) associated with given user ID. @@ -210,6 +211,6 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable: * automatically by the processing logic as well as can be thrown by the user from the intent callback. * @throws NCException Thrown in case of any internal errors processing the user input. */ - def debugAsk(txt: String, usrId: String, saveHist: Boolean, data: Map[String, AnyRef] = Map.empty): NCMatchedCallback = + def debugAsk(txt: String, usrId: String, saveHist: Boolean, data: Map[String, AnyRef] = Map.empty): Try[NCMatchedCallback] = import NCIntentSolveType.* - ask0(txt, data, usrId, if saveHist then SEARCH else SEARCH_NO_HISTORY).toOption.get \ No newline at end of file + Try(ask0(txt, data, usrId, if saveHist then SEARCH else SEARCH_NO_HISTORY).toOption.get) \ No newline at end of file diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala index a272bb78..abbc1567 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec.scala @@ -45,10 +45,9 @@ class NCConversationSpec extends AnyFunSuite: Using.resource(new NCModelClient(mdl)) { cli => def execOk(txt: String): Unit = cli.ask(txt, usrId) def execReject(txt: String): Unit = - try - cli.ask(txt, usrId) - require(false) - catch + val res = cli.ask(txt, usrId) + assert(res.isFailure) + res.failed.get match case e: NCRejection => // OK. case e: Throwable => throw e diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec2.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec2.scala index 77d7f16e..bb95cb12 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec2.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationSpec2.scala @@ -69,11 +69,11 @@ class NCConversationSpec2 extends AnyFunSuite: // Checks via requests. for ((qry, h) <- questions) - require(h == cli.ask(qry, "usrId").getBody) + require(h == cli.ask(qry, "usrId").get.getBody) // Checks via dry run. for ((qry, h) <- questions) - val cb = cli.debugAsk(qry, "usrId", saveHist = true) + val cb = cli.debugAsk(qry, "usrId", saveHist = true).get require(h.intentId == cb.getIntentId) require(h.elements.size == cb.getCallbackArguments.size) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala index 2941d41a..a8ecb516 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/conversation/NCConversationTimeoutSpec.scala @@ -57,7 +57,7 @@ class NCConversationTimeoutSpec extends AnyFunSuite: Using.resource(new NCModelClient(mdl)) { cli => def check(hasValue: Boolean): Unit = - require(cli.ask("test", "userId").getBody.toString == (if hasValue then VALUE else EMPTY)) + require(cli.ask("test", "userId").get.getBody.toString == (if hasValue then VALUE else EMPTY)) check(false) check(true) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala index 96a8235d..c2132e13 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelCallbacksSpec.scala @@ -94,7 +94,7 @@ class NCModelCallbacksSpec extends AnyFunSuite: */ private def testOk(client: NCModelClient, exp: NCResult, states: State*): Unit = set(states*) - require(client.ask("x", "userId").getBody == exp.getBody) + require(client.ask("x", "userId").get.getBody == exp.getBody) /** * 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 2f422821..cfb090eb 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 @@ -44,12 +44,12 @@ class NCModelClientSpec extends AnyFunSuite: mdl.pipeline.entParsers += NCTestUtils.mkEnSemanticParser("models/lightswitch_model.yaml") Using.resource(new NCModelClient(mdl)) { client => - val res = client.ask("Lights on at second floor kitchen", "userId") + val res = client.ask("Lights on at second floor kitchen", "userId").get println(s"Intent: ${res.getIntentId}") println(s"Body: ${res.getBody}") - val winner = client.debugAsk("Lights on at second floor kitchen", "userId", true) + val winner = client.debugAsk("Lights on at second floor kitchen", "userId", true).get 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 bb24a3ef..9671474b 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 @@ -45,7 +45,7 @@ class NCModelClientSpec2 extends AnyFunSuite: Using.resource(new NCModelClient(mdl)) { client => case class Result(txt: String): - private val wi = client.debugAsk(txt, "userId", true) + private val wi = client.debugAsk(txt, "userId", true).get private val allArgs: List[List[NCEntity]] = wi.getCallbackArguments val intentId: String = wi.getIntentId @@ -84,11 +84,9 @@ class NCModelClientSpec2 extends AnyFunSuite: check(res.second.last, "e2") // 3. No winners. - try - client.debugAsk("x", "userId", false) - - require(false) - catch + val resErr = client.debugAsk("x", "userId", false) + assert(resErr.isFailure) + resErr.failed.get match 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 c108693c..07f73cf0 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 @@ -32,7 +32,7 @@ import scala.util.Using */ class NCModelClientSpec3 extends AnyFunSuite: test("test") { - import NCSemanticTestElement as TE + import org.apache.nlpcraft.nlp.parsers.NCSemanticTestElement as TE val mdl: NCTestModelAdapter = new NCTestModelAdapter : override val getPipeline: NCPipeline = mkEnPipeline(TE("e1")) @@ -41,7 +41,7 @@ class NCModelClientSpec3 extends AnyFunSuite: def onMatch(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("t1") t1: NCEntity): NCResult = TEST_RESULT Using.resource(new NCModelClient(mdl)) { client => - def ask(): NCMatchedCallback = client.debugAsk("e1", "userId", true) + def ask(): NCMatchedCallback = client.debugAsk("e1", "userId", true).get def execCallback(cb: NCMatchedCallback): NCResult = cb.getCallback.apply(cb.getCallbackArguments) def execCallbackOk(cb: NCMatchedCallback): Unit = println(s"Result: ${execCallback(cb).getBody}") def execCallbackFail(cb: NCMatchedCallback): Unit = diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala index dc89f224..b7e88c23 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala @@ -36,8 +36,8 @@ class NCModelClientSpec4 extends AnyFunSuite: val client = new NCModelClient(mdl) val allCalls = Seq( - () => client.ask("test", "userId"), - () => client.debugAsk("test", "userId", false), + () => client.ask("test", "userId").get, + () => client.debugAsk("test", "userId", false).get, () => client.clearStm("userId", _ => true), () => client.clearStm("userId"), () => client.clearDialog("userId"), 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 b60825cb..da28d7da 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 @@ -65,11 +65,8 @@ class NCModelPingPongSpec extends AnyFunSuite with BeforeAndAfter: def onOther(ctx: NCContext, im: NCIntentMatch, @NCIntentTerm("other") other: NCEntity): NCResult = mkResult(ASK_RESULT, s"Some request by: ${other.mkText}") - MDL.pipeline.entParsers += NCTestUtils.mkEnSemanticParser(List( - NCSemanticTestElement("command"), - NCSemanticTestElement("confirm"), - NCSemanticTestElement("other") - )) + import org.apache.nlpcraft.nlp.parsers.NCSemanticTestElement as TE + MDL.pipeline.entParsers += NCTestUtils.mkEnSemanticParser(List(TE("command"), TE("confirm"), TE("other"))) before { client = new NCModelClient(MDL) @@ -80,14 +77,18 @@ class NCModelPingPongSpec extends AnyFunSuite with BeforeAndAfter: } private def ask(txt: String, typ: NCResultType): Unit = - val res = client.ask(txt, "userId") + val res = client.ask(txt, "userId").get println(s"Request [text=$txt, result=$res]") require(res.getType == typ) private def askForDialog(txt: String): Unit = ask(txt, ASK_DIALOG) private def askForResult(txt: String): Unit = ask(txt, ASK_RESULT) private def askForReject(txt: String): Unit = - try ask(txt, ASK_RESULT) catch case e: NCRejection => println(s"Expected reject on: $txt") + val res = client.ask(txt, "userId") + require(res.isFailure) + res.failed.get match + case e: NCRejection => println(s"Expected reject on: $txt") + case e: Throwable => throw e test("test 1") { askForDialog("command") diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec.scala index 21e78f93..a92bcc6f 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec.scala @@ -42,7 +42,7 @@ class NCIDLFragmentsOverridingSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new M())) { client => def test(ps: (String, Int)*): Unit = - ps.foreach { case (txt, res) => require(client.ask(txt, "usr").getBody == res) } + ps.foreach { case (txt, res) => require(client.ask(txt, "usr").get.getBody == res) } test("x1" -> 1, "x2" -> 2) } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec2.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec2.scala index 59cf911f..54d59764 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec2.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsOverridingSpec2.scala @@ -45,7 +45,7 @@ class NCIDLFragmentsOverridingSpec2 extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(new M())) { client => - def test(ps: (String, Int)*): Unit = ps.foreach { case (txt, res) => require(client.ask(txt, "usr").getBody == res) } + def test(ps: (String, Int)*): Unit = ps.foreach { case (txt, res) => require(client.ask(txt, "usr").get.getBody == res) } test("x1" -> 1, "x2" -> 2, "x3" -> 3) } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala index bc71a28c..33ce7775 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala @@ -67,6 +67,6 @@ class NCEntityMapperSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(mdl)) { client => - require(client.ask("a b c d", "userId").getIntentId.orNull == "abcd") + require(client.ask("a b c d", "userId").get.getIntentId.orNull == "abcd") } } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityParserSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityParserSpec.scala index ca036729..b39844b5 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityParserSpec.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityParserSpec.scala @@ -52,7 +52,7 @@ object NCEntityParserSpec: class NCEntityParserSpec extends AnyFunSuite: test("test") { Using.resource(new NCModelClient(NCEntityParserSpec.mdl)) { client => - val intentId = client.ask( "'some quoted text'", "usrId").getIntentId.get + val intentId = client.ask( "'some quoted text'", "usrId").get.getIntentId.get require(intentId == "quoted", s"Unexpected intent: $intentId") } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala index 6bcf56ad..778d7602 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestUtils.scala @@ -108,7 +108,7 @@ object NCTestUtils: */ def askSomething(mdl: NCModel, expectedOk: Boolean): Unit = Using.resource(new NCModelClient(mdl)) { client => - def ask(): NCResult = client.ask("test", "userId") + def ask(): NCResult = client.ask("test", "userId").get if expectedOk then println(ask().getBody)
