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 14c34d8  WIP.
14c34d8 is described below

commit 14c34d827eeb7c71ba42c21e7e5a8c88f3f66f71
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Apr 1 16:51:43 2022 +0300

    WIP.
---
 .../nlpcraft/examples/order/OrderModel.scala       | 64 ++++++++--------------
 1 file changed, 24 insertions(+), 40 deletions(-)

diff --git 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala
 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala
index 7f0bc74..9dad41b 100644
--- 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala
+++ 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala
@@ -29,56 +29,43 @@ import org.apache.nlpcraft.NCResultType.*
 import scala.jdk.CollectionConverters.*
 
 /**
-  * This example provides very simple implementation for NLI-powered light 
switch.
-  * You can say something like this:
-  * <ul>
-  *     <li>"Turn the lights off in the entire house."</li>
-  *     <li>"Switch on the illumination in the master bedroom closet."</li>
-  * </ul>
-  * You can easily modify intent callbacks to perform the actual light 
switching using
-  * HomeKit or Arduino-based controllers.
-  * <p>
-  * See 'README.md' file in the same folder for running and testing 
instructions.
+  *
   */
-
 class OrderModel extends NCModelAdapter(
     new NCModelConfig("nlpcraft.order.ex", "Order Example Model", "1.0"),
     new NCPipelineBuilder().withSemantic("en", "order_model.yaml").build()
 ):
-    private val orders = mutable.HashMap.empty[String, Order]
+    private val ords = mutable.HashMap.empty[String, Order]
+
+    private def getOrder(im: NCIntentMatch): Order = 
ords.getOrElseUpdate(im.getContext.getRequest.getUserId, new Order)
 
-    private def getOrder(im: NCIntentMatch): Order = 
orders.getOrElseUpdate(im.getContext.getRequest.getUserId, new Order)
-    private def extractPizza(e: NCEntity): String = 
e.get[String]("ord:pizza:kind:value")
+    private def extractPizzaKind(e: NCEntity): String = 
e.get[String]("ord:pizza:kind:value")
     private def extractPizzaSize(e: NCEntity): String = 
e.get[String]("ord:pizza:size:value")
     private def extractDrink(e: NCEntity): String = 
e.get[String]("ord:drink:value")
 
     private def confirmOrSpecify(ord: Order): NCResult =
-        if ord.isValid() then
-            NCResult(ord.ask2Confirm(), ASK_DIALOG)
-        else
-            NCResult(ord.ask2Specify(), ASK_DIALOG)
+        NCResult(if ord.isValid() then ord.ask2Confirm() else 
ord.ask2Specify(), ASK_DIALOG)
 
     private def getAvgPosition(e: NCEntity): Double =
         val toks = e.getTokens.asScala
-
         (toks.head.getIndex + toks.last.getIndex) / 2.0
 
     @NCIntent("intent=confirm term(confirm)={has(ent_groups, 'confirm')}")
     def onConfirm(im: NCIntentMatch, @NCIntentTerm("confirm") confirm: 
NCEntity): NCResult =
         val ord = getOrder(im)
 
-        if !ord.inProgress() then throw new NCRejection("No orders in 
progress")
+        if !ord.inProgress() then throw new NCRejection("No orders in 
progress.")
 
         if confirm.getId == "ord:confirm:yes" then
             if ord.isValid() then
-                println("Congratulations. Order executed!")
+                println(s"Done: $ord")
                 ord.clear()
-                NCResult("Order executed.", ASK_RESULT)
+                NCResult("Congratulations. Your order executed. You can start 
make new orders.", ASK_RESULT)
             else
                 NCResult(ord.ask2Specify(), ASK_DIALOG)
         else
             ord.clear()
-            NCResult("Order cleared. We are ready for new orders.", ASK_RESULT)
+            NCResult("Order canceled. We are ready for new orders.", 
ASK_RESULT)
 
     @NCIntent(
         "intent=order " +
@@ -87,33 +74,30 @@ class OrderModel extends NCModelAdapter(
         "  term(pizzaSizesList)={# == 'ord:pizza:size'}* " +
         "  term(drinkList)={# == 'ord:drink'}*"
     )
-    @NCIntentSample(Array(
-        "I want to order margherita, marinara and tea"
-    ))
     def onCommonOrder(
         im: NCIntentMatch,
         @NCIntentTerm("common") common: List[NCEntity],
-        @NCIntentTerm("pizzaList") pizzas: List[NCEntity],
+        @NCIntentTerm("pizzaList") pizzaKinds: List[NCEntity],
         @NCIntentTerm("pizzaSizesList") pizzaSizes: List[NCEntity],
         @NCIntentTerm("drinkList") drinks: List[NCEntity]
     ): NCResult =
-        if pizzas.isEmpty && drinks.isEmpty then throw new NCRejection("Please 
order some pizza or drinks")
-        if pizzaSizes.size > pizzas.size then throw new NCRejection("Pizza and 
their sizes cannot be recognized")
+        if pizzaKinds.isEmpty && drinks.isEmpty then throw new 
NCRejection("Please order some pizza or drinks")
+        if pizzaSizes.size > pizzaKinds.size then throw new NCRejection("Pizza 
and their sizes cannot be recognized")
 
-        val ord = getOrder(im)
-
-        case class Size(entity: NCEntity, position: Double)
+        case class Holder(entity: NCEntity, position: Double)
 
-        val sizes = mutable.ArrayBuffer.empty ++ pizzaSizes.map(p => Size( 
p,getAvgPosition(p)))
+        val ord = getOrder(im)
+        val hsSizes = mutable.ArrayBuffer.empty ++ pizzaSizes.map(p => 
Holder(p ,getAvgPosition(p)))
 
-        pizzas.foreach(p => {
-            sizes.size match
-                case 0 => ord.addPizza(extractPizza(p))
+        // Pizza. Each pizza can be specified by its size. Or size will be 
asked additionally.
+        pizzaKinds.foreach(p => {
+            hsSizes.size match
+                case 0 => ord.addPizza(extractPizzaKind(p))
                 case _ =>
                     val avgPos = getAvgPosition(p)
-                    val nextNeighbour = sizes.minBy(p => Math.abs(avgPos - 
p.position))
-                    ord.addPizza(extractPizza(p), 
PizzaSize.valueOf(extractPizzaSize(nextNeighbour.entity).toUpperCase))
-                    sizes -= nextNeighbour
+                    val nextNeighbour = hsSizes.minBy(p => Math.abs(avgPos - 
p.position))
+                    ord.addPizza(extractPizzaKind(p), 
PizzaSize.valueOf(extractPizzaSize(nextNeighbour.entity).toUpperCase))
+                    hsSizes -= nextNeighbour
         })
 
         for (p <- drinks.map(extractDrink)) ord.addDrink(p)
@@ -138,7 +122,7 @@ class OrderModel extends NCModelAdapter(
         val sz = PizzaSize.valueOf(extractPizzaSize(size).toUpperCase)
 
         pizzaOpt match
-            case Some(pizza) => ord.addPizza(extractPizza(pizza), sz)
+            case Some(pizza) => ord.addPizza(extractPizzaKind(pizza), sz)
             case None => if !ord.specifyPizzaSize(sz) then throw new 
NCRejection("What specified?")
 
         confirmOrSpecify(ord)
\ No newline at end of file

Reply via email to