This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-490 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-490 by this push: new 6314950 WIP. 6314950 is described below commit 6314950ab6dbfa831d8a4529af1e86d5dbcb1536 Author: Sergey Kamov <skhdlem...@gmail.com> AuthorDate: Wed Mar 30 10:27:04 2022 +0300 WIP. --- .../{NCWinnerIntent.java => NCCallbackData.java} | 5 +-- .../scala/org/apache/nlpcraft/NCModelClient.java | 6 +-- .../nlpcraft/internal/impl/NCModelClientImpl.scala | 6 +-- .../intent/matcher/NCIntentSolverManager.scala | 51 ++++++++++++---------- .../nlpcraft/internal/impl/NCModelClientSpec.scala | 4 +- .../internal/impl/NCModelClientSpec2.scala | 6 +-- 6 files changed, 42 insertions(+), 36 deletions(-) diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCWinnerIntent.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCCallbackData.java similarity index 90% rename from nlpcraft/src/main/scala/org/apache/nlpcraft/NCWinnerIntent.java rename to nlpcraft/src/main/scala/org/apache/nlpcraft/NCCallbackData.java index 56baca7..154ade9 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCWinnerIntent.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCCallbackData.java @@ -18,12 +18,11 @@ package org.apache.nlpcraft; import java.util.List; -import java.util.Map; /** * TODO: */ -public interface NCWinnerIntent { +public interface NCCallbackData { String getIntentId(); - List<List<NCEntity>> getArguments(); + List<List<NCEntity>> getCallbackArguments(); } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java index e9c4083..f0ce9dc 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.java @@ -101,7 +101,7 @@ public class NCModelClient implements AutoCloseable { /** * TODO: - * Gets intent information which contains intent ID and its callback arguments entities. + * Gets callback information which contains intent ID and callback arguments entities. * Note that * - Callback is not called in this case. * - if model `onContext` method overrided - error thrown because we don't find intents in this case. @@ -113,7 +113,7 @@ public class NCModelClient implements AutoCloseable { * if false found intent ignored in history. * @return */ - public NCWinnerIntent getWinnerIntent(String txt, Map<String, Object> data, String usrId, boolean saveHistory) { - return impl.getWinnerIntent(txt, data, usrId, saveHistory); + public NCCallbackData findCallback(String txt, Map<String, Object> data, String usrId, boolean saveHistory) { + return impl.findCallback(txt, data, usrId, saveHistory); } } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelClientImpl.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelClientImpl.scala index 3ac8100..df8e0a7 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelClientImpl.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCModelClientImpl.scala @@ -75,7 +75,7 @@ class NCModelClientImpl(mdl: NCModel) extends LazyLogging: dlgMgr.start() plMgr.start() - private def ask0(txt: String, data: JMap[String, AnyRef], usrId: String, typ: NCIntentSolveType): Either[NCResult, NCWinnerIntent] = + private def ask0(txt: String, data: JMap[String, AnyRef], usrId: String, typ: NCIntentSolveType): Either[NCResult, NCCallbackData] = val plData = plMgr.prepare(txt, data, usrId) val userId = plData.request.getUserId @@ -193,6 +193,6 @@ class NCModelClientImpl(mdl: NCModel) extends LazyLogging: dlgMgr.close() convMgr.close() - def getWinnerIntent(txt: String, data: JMap[String, AnyRef], usrId: String, saveHistory: Boolean): NCWinnerIntent = + def findCallback(txt: String, data: JMap[String, AnyRef], usrId: String, saveHistory: Boolean): NCCallbackData = import NCIntentSolveType.* - ask0(txt, data, usrId, if saveHistory then TEST_HISTORY else TEST_NO_HISTORY).toOption.get \ No newline at end of file + ask0(txt, data, usrId, if saveHistory then SEARCH else SEARCH_NO_HISTORY).toOption.get \ No newline at end of file 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 3ebde23..8dbd460 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 @@ -32,8 +32,11 @@ import scala.collection.mutable.ArrayBuffer import scala.jdk.CollectionConverters.* import scala.language.postfixOps +/** + * + */ enum NCIntentSolveType: - case REGULAR, TEST_HISTORY, TEST_NO_HISTORY + case REGULAR, SEARCH, SEARCH_NO_HISTORY object NCIntentSolverManager: /** @@ -73,7 +76,13 @@ object NCIntentSolverManager: * @param entities */ private case class IntentTermEntities(termId: Option[String], entities: Seq[NCEntity]) - private case class NCWinnerIntentImpl(getIntentId: String, getArguments: JList[JList[NCEntity]]) extends NCWinnerIntent + + /** + * + * @param getIntentId + * @param getCallbackArguments + */ + private case class CallbackDataImpl(getIntentId: String, getCallbackArguments: JList[JList[NCEntity]]) extends NCCallbackData /** * @@ -167,7 +176,7 @@ object NCIntentSolverManager: */ private case class IntentEntity(var used: Boolean, var conv: Boolean, entity: NCEntity) - type ResultData = Either[NCResult, NCWinnerIntent] + type ResultData = Either[NCResult, NCCallbackData] /** * @@ -564,7 +573,7 @@ class NCIntentSolverManager( ) // Term not found at all. case None => None - catch case e: Exception => throw new NCException(s"Runtime error processing IDL term: $term", e) + catch case e: Exception => E(s"Runtime error processing IDL term: $term", e) /** * Solves term's predicate. @@ -677,11 +686,13 @@ class NCIntentSolverManager( // This can throw NCIntentSkip exception. import NCIntentSolveType.* - def saveHistory(r: NCResult): Unit = - dialog.addMatchedIntent(im, r, ctx) + def saveHistory(res: NCResult): Unit = + dialog.addMatchedIntent(im, res, ctx) conv.getConversation(req.getUserId).addEntities( req.getRequestId, im.getIntentEntities.asScala.flatMap(_.asScala).toSeq.distinct ) + def finishHistory(): Unit = + Loop.finish(Option(IterationResult(Right(CallbackDataImpl(im.getIntentId, im.getIntentEntities)), im))) typ match case REGULAR => @@ -694,14 +705,10 @@ class NCIntentSolverManager( saveHistory(cbRes) Loop.finish(Option(IterationResult(Left(cbRes), im))) - - case TEST_HISTORY => - // Added dummy result. TODO: is it ok? - saveHistory(new NCResult()) - - Loop.finish(Option(IterationResult(Right(NCWinnerIntentImpl(im.getIntentId, im.getIntentEntities)), im))) - case TEST_NO_HISTORY => - Loop.finish(Option(IterationResult(Right(NCWinnerIntentImpl(im.getIntentId, im.getIntentEntities)), im))) + case SEARCH => + saveHistory(new NCResult()) // // Added dummy result. TODO: is it ok? + finishHistory() + case SEARCH_NO_HISTORY => finishHistory() else logger.info(s"Model '${ctx.getModelConfig.getId}' triggered rematching of intents by intent '${intentRes.intentId}' on variant #${intentRes.variantIdx + 1}.") Loop.finish() @@ -722,15 +729,15 @@ class NCIntentSolverManager( * @return */ def solve(mdl: NCModel, ctx: NCContext, typ: NCIntentSolveType): ResultData = - import NCIntentSolveType.* + import NCIntentSolveType.REGULAR - val ctxRes = mdl.onContext(ctx) + val mdlCtxRes = mdl.onContext(ctx) - if ctxRes != null then + if mdlCtxRes != null then if typ != REGULAR then E("`onContext` method overriden, intents cannot be found.") // TODO: test if intents.nonEmpty then logger.warn("`onContext` method overrides existed intents. They are ignored.") // TODO: text. - Left(ctxRes) + Left(mdlCtxRes) else if intents.isEmpty then // TODO: text. @@ -748,7 +755,7 @@ class NCIntentSolverManager( case REGULAR => mdl.onResult(loopRes.intentMatch, loopRes.result.swap.toOption.get) match case null => loopRes.result - case res => Left(res) + case mdlRes => Left(mdlRes) case _ => loopRes.result catch case e: NCRejection => @@ -756,7 +763,7 @@ class NCIntentSolverManager( case REGULAR => mdl.onRejection(if loopRes != null then loopRes.intentMatch else null, e) match case null => throw e - case res => Left(res) + case mdlRejRes => Left(mdlRejRes) case _ => throw e case e: Throwable => @@ -764,7 +771,7 @@ class NCIntentSolverManager( case REGULAR => mdl.onError(ctx, e) match case null => throw e - case res => + case mdlErrRes => logger.warn("Error during execution.", e) - Left(res) + Left(mdlErrRes) case _ => throw e \ 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 d1c3daa..0dbd4f7 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 @@ -41,9 +41,9 @@ class NCModelClientSpec: client.validateSamples() - val winner = client.getWinnerIntent("Lights on at second floor kitchen", null, "userId", true) + val winner = client.findCallback("Lights on at second floor kitchen", null, "userId", true) println(s"Winner intent: ${winner.getIntentId}") - println("Entities: \n" + winner.getArguments.asScala.map(p => p.asScala.map(s).mkString(", ")).mkString("\n")) + println("Entities: \n" + winner.getCallbackArguments.asScala.map(p => p.asScala.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 4c37ddc..b2cda88 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 @@ -47,8 +47,8 @@ class NCModelClientSpec2: Using.resource(new NCModelClient(mdl)) { client => case class Result(txt: String): - private val wi = client.getWinnerIntent(txt, null, "userId", true) - private val allArgs: JList[JList[NCEntity]] = wi.getArguments + private val wi = client.findCallback(txt, null, "userId", true) + private val allArgs: JList[JList[NCEntity]] = wi.getCallbackArguments val intentId: String = wi.getIntentId val size: Int = allArgs.size() @@ -87,7 +87,7 @@ class NCModelClientSpec2: // 3. No winners. try - client.getWinnerIntent("x", null, "userId", false) + client.findCallback("x", null, "userId", false) require(false) catch