This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-491 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-491 by this push: new a94af435 WIP. a94af435 is described below commit a94af43555b2bc43466ed0f161949fe2b41b117a Author: Sergey Kamov <skhdlem...@gmail.com> AuthorDate: Fri Apr 8 10:32:00 2022 +0300 WIP. --- .../nlpcraft/examples/pizzeria/PizzeriaModel.scala | 88 ++++++++++------------ .../examples/pizzeria/PizzeriaModelPipeline.scala | 5 +- .../nlpcraft/examples/pizzeria/PizzeriaOrder.scala | 8 +- .../pizzeria/components/RequestValidator.scala | 4 +- .../examples/pizzeria/PizzeriaModelSpec.scala | 4 +- 5 files changed, 49 insertions(+), 60 deletions(-) diff --git a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala index f08ad8b2..e9498fd7 100644 --- a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala +++ b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModel.scala @@ -20,7 +20,7 @@ package org.apache.nlpcraft.examples.pizzeria import com.typesafe.scalalogging.LazyLogging import org.apache.nlpcraft.* import org.apache.nlpcraft.NCResultType.* -import org.apache.nlpcraft.examples.pizzeria.State.* +import org.apache.nlpcraft.examples.pizzeria.OrderState.* import org.apache.nlpcraft.nlp.* import java.util.Properties @@ -41,7 +41,6 @@ object PizzeriaModel extends LazyLogging: private def extractDrink(e: NCEntity): Drink = Drink(e.get[String]("ord:drink:value"), extractQty(e, "ord:drink:qty")) - import org.apache.nlpcraft.examples.pizzeria.PizzeriaModel.* /** @@ -93,7 +92,7 @@ class PizzeriaModel extends NCModelAdapter (new NCModelConfig("nlpcraft.pizzeria ASK_RESULT ) - private def doShowStatus(o: PizzeriaOrder, newState: State) = + private def doShowStatus(o: PizzeriaOrder, newState: OrderState) = val res = NCResult(s"Current order state: ${o.getDesc}.", ASK_RESULT) o.setState(newState) res @@ -111,6 +110,7 @@ class PizzeriaModel extends NCModelAdapter (new NCModelConfig("nlpcraft.pizzeria conv.clearStm(_ => true) conv.clearDialog(_ => true) + // Access level set for tests reasons. private[pizzeria] def doExecute(im: NCIntentMatch, o: PizzeriaOrder): NCResult = require(o.isValid) val res = NCResult(s"Executed: ${o.getDesc}.", ASK_RESULT) @@ -143,9 +143,7 @@ class PizzeriaModel extends NCModelAdapter (new NCModelConfig("nlpcraft.pizzeria def onYes(im: NCIntentMatch): NCResult = withLog( im, (o: PizzeriaOrder) => o.getState match - case DIALOG_CONFIRM => - require(o.isValid); - doExecute(im, o) + case DIALOG_CONFIRM => doExecute(im, o) case DIALOG_SHOULD_CANCEL => doStop(im, o) case DIALOG_IS_READY => askConfirmOrAskSpecify(o) case DIALOG_SPECIFY | NO_DIALOG => throw UNEXPECTED_REQUEST @@ -170,44 +168,9 @@ class PizzeriaModel extends NCModelAdapter (new NCModelConfig("nlpcraft.pizzeria * @return */ @NCIntent("intent=stop term(stop)={# == 'ord:stop'}") - def onStop(im: NCIntentMatch): NCResult = withLog( - im, - // It doesn't depend on order validity and dialog state. - (o: PizzeriaOrder) => askStopOrDoStop(im, o) - ) + // It doesn't depend on order validity and dialog state. + def onStop(im: NCIntentMatch): NCResult = withLog(im, (o: PizzeriaOrder) => askStopOrDoStop(im, o)) - /** - * - * @param im - * @param ps - * @param ds - * @return - */ - @NCIntent("intent=order term(ps)={# == 'ord:pizza'}* term(ds)={# == 'ord:drink'}*") - def onOrder(im: NCIntentMatch, @NCIntentTerm("ps") ps: List[NCEntity], @NCIntentTerm("ds") ds: List[NCEntity]): NCResult = withLog( - im, - (o: PizzeriaOrder) => - require(ps.nonEmpty || ds.nonEmpty); - // It doesn't depend on order validity and dialog state. - o.add(ps.map(extractPizza), ds.map(extractDrink)); - askIsReadyOrAskSpecify(o) - ) - /** - * - * @param im - * @param size - * @return - */ - @NCIntent("intent=orderPizzaSize term(size)={# == 'ord:pizza:size'}") - def onOrderPizzaSize(im: NCIntentMatch, @NCIntentTerm("size") size: NCEntity): NCResult = withLog( - im, - (o: PizzeriaOrder) => - // If order in progress and has pizza with unknown size, it doesn't depend on dialog state. - if !o.isEmpty then - if o.setPizzaNoSize(extractPizzaSize(size)) then askIsReadyOrAskSpecify(o) else throw UNEXPECTED_REQUEST - else - throw UNEXPECTED_REQUEST - ) /** * * @param im @@ -217,9 +180,7 @@ class PizzeriaModel extends NCModelAdapter (new NCModelConfig("nlpcraft.pizzeria def onStatus(im: NCIntentMatch): NCResult = withLog( im, (o: PizzeriaOrder) => o.getState match - case DIALOG_CONFIRM => - require(o.isValid); - askConfirm(o) // Ignore `status`, confirm again. + case DIALOG_CONFIRM => askConfirm(o) // Ignore `status`, confirm again. case DIALOG_SHOULD_CANCEL => doShowStatus(o, NO_DIALOG) // Changes state. case NO_DIALOG | DIALOG_IS_READY | DIALOG_SPECIFY => doShowStatus(o, o.getState) // Keeps same state. ) @@ -242,8 +203,37 @@ class PizzeriaModel extends NCModelAdapter (new NCModelConfig("nlpcraft.pizzeria * @return */ @NCIntent("intent=menu term(menu)={# == 'ord:menu'}") - def onMenu(im: NCIntentMatch): NCResult = withLog( + // It doesn't depend and doesn't influence on order validity and dialog state. + def onMenu(im: NCIntentMatch): NCResult = withLog(im, _ => doShowMenu()) + + /** + * + * @param im + * @param ps + * @param ds + * @return + */ + @NCIntent("intent=order term(ps)={# == 'ord:pizza'}* term(ds)={# == 'ord:drink'}*") + def onOrder(im: NCIntentMatch, @NCIntentTerm("ps") ps: List[NCEntity], @NCIntentTerm("ds") ds: List[NCEntity]): NCResult = withLog( im, - // It doesn't depend and doesn't influence on order validity and dialog state. - _ => doShowMenu() + (o: PizzeriaOrder) => + require(ps.nonEmpty || ds.nonEmpty); + // It doesn't depend on order validity and dialog state. + o.add(ps.map(extractPizza), ds.map(extractDrink)); + askIsReadyOrAskSpecify(o) + ) + + /** + * + * @param im + * @param size + * @return + */ + @NCIntent("intent=orderSpecify term(size)={# == 'ord:pizza:size'}") + def onOrderSpecify(im: NCIntentMatch, @NCIntentTerm("size") size: NCEntity): NCResult = withLog( + im, + (o: PizzeriaOrder) => + // If order in progress and has pizza with unknown size, it doesn't depend on dialog state. + if !o.isEmpty && o.setPizzaNoSize(extractPizzaSize(size)) then askIsReadyOrAskSpecify(o) + else throw UNEXPECTED_REQUEST ) \ No newline at end of file diff --git a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelPipeline.scala b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelPipeline.scala index ce342519..118d6f98 100644 --- a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelPipeline.scala +++ b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelPipeline.scala @@ -26,8 +26,7 @@ object PizzeriaModelPipeline: private val ps = new PorterStemmer override def stem(txt: String): String = ps.synchronized { ps.stem(txt) } - import ElementExtender as Ex - import EntityData as D + import ElementExtender as Ex, EntityData as D new NCPipelineBuilder(). withTokenParser(tokParser). @@ -41,4 +40,4 @@ object PizzeriaModelPipeline: ).asJava ). withEntityValidator(new RequestValidator()). - build() + build() \ No newline at end of file diff --git a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala index c76788cf..04a8647f 100644 --- a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala +++ b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrder.scala @@ -48,10 +48,10 @@ case class Pizza(name: String, var size: Option[String], var qty: Option[Int]) e case class Drink(name: String, var qty: Option[Int]) extends OrderElement: override def toString = s"$name ${qty.getOrElse(OrderElement.DFLT_QTY)} pcs" -enum State: +enum OrderState: case NO_DIALOG, DIALOG_IS_READY, DIALOG_SHOULD_CANCEL, DIALOG_SPECIFY, DIALOG_CONFIRM -import org.apache.nlpcraft.examples.pizzeria.State.* +import org.apache.nlpcraft.examples.pizzeria.OrderState.* class PizzeriaOrder: private var state = NO_DIALOG @@ -128,13 +128,13 @@ class PizzeriaOrder: * * @return */ - def getState: State = state + def getState: OrderState = state /** * * @param state */ - def setState(state: State): Unit = this.state = state + def setState(state: OrderState): Unit = this.state = state /** * diff --git a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/RequestValidator.scala b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/RequestValidator.scala index 34f5d698..341552f4 100644 --- a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/RequestValidator.scala +++ b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/RequestValidator.scala @@ -38,7 +38,7 @@ class RequestValidator extends NCEntityValidator: // Single size - it is order specification request. if cntSize != 1 && cntSize > cntPizza then - throw new NCRejection("There are unrecognized pizza sizes in the request.") + throw new NCRejection("There are unrecognized pizza sizes in the request, maybe because some misprints.") if cntNums > cntPizza + cntDrink then - throw new NCRejection("There are many unrecognized numerics in the request.") + throw new NCRejection("There are many unrecognized numerics in the request, maybe because some misprints.") \ No newline at end of file diff --git a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala b/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala index f175a090..1478a693 100644 --- a/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala +++ b/nlpcraft-examples/pizzeria/src/test/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaModelSpec.scala @@ -19,7 +19,7 @@ package org.apache.nlpcraft.examples.pizzeria import org.apache.nlpcraft.* import org.apache.nlpcraft.NCResultType.* -import org.apache.nlpcraft.examples.pizzeria.State.* +import org.apache.nlpcraft.examples.pizzeria.OrderState.* import org.junit.jupiter.api.* import scala.language.implicitConversions @@ -104,7 +104,7 @@ class PizzeriaModelSpec: testNum += 1 - private def mkOrder(state: State, ps: Seq[Pizza], ds: Seq[Drink]): PizzeriaOrder = + private def mkOrder(state: OrderState, ps: Seq[Pizza], ds: Seq[Drink]): PizzeriaOrder = val o = new PizzeriaOrder o.setState(state) o.add(ps, ds)