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 5e947a64 WIP.
5e947a64 is described below
commit 5e947a6427476699d51d269936e0f66fe7a4f7d5
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Apr 8 15:03:30 2022 +0300
WIP.
---
.../nlpcraft/examples/pizzeria/PizzeriaModel.scala | 38 +++++++++++-----------
.../examples/pizzeria/PizzeriaModelPipeline.scala | 4 +--
.../nlpcraft/examples/pizzeria/PizzeriaOrder.scala | 13 ++------
.../examples/pizzeria/PizzeriaOrderState.scala | 24 ++++++++++++++
...tExtender.scala => PizzeriaOrderExtender.scala} | 6 ++--
...alidator.scala => PizzeriaOrderValidator.scala} | 2 +-
.../examples/pizzeria/PizzeriaModelSpec.scala | 4 +--
7 files changed, 54 insertions(+), 37 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 dc464b55..878c5e6d 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.OrderState.*
+import org.apache.nlpcraft.examples.pizzeria.PizzeriaOrderState.*
import org.apache.nlpcraft.nlp.*
import java.util.Properties
@@ -74,7 +74,7 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
private def askIsReady(o: PizzeriaOrder): NCResult =
val res = NCResult(s"Is order ready?", ASK_DIALOG)
- o.setState(DIALOG_IS_READY)
+ o.setState(WAIT_IS_READY)
res
private def askSpecify(o: PizzeriaOrder) =
@@ -84,12 +84,12 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
case None =>
require(o.isEmpty)
NCResult(s"Please order something. Ask `menu` to look what you
can order.", ASK_DIALOG)
- o.setState(DIALOG_SPECIFY)
+ o.setState(WAIT_SPECIFY)
res
private def askShouldStop(o: PizzeriaOrder) =
val res = NCResult(s"Should current order be canceled?", ASK_DIALOG)
- o.setState(DIALOG_SHOULD_CANCEL)
+ o.setState(WAIT_SHOULD_CANCEL)
res
private def doShowMenu() =
@@ -99,7 +99,7 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
ASK_RESULT
)
- private def doShowStatus(o: PizzeriaOrder, newState: OrderState) =
+ private def doShowStatus(o: PizzeriaOrder, newState: PizzeriaOrderState) =
val res = NCResult(s"Current order state: ${o.getDesc}.", ASK_RESULT)
o.setState(newState)
res
@@ -107,7 +107,7 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
private def askConfirm(o: PizzeriaOrder): NCResult =
require(o.isValid)
val res = NCResult(s"Let's specify your order: ${o.getDesc}. Is it
correct?", ASK_DIALOG)
- o.setState(DIALOG_CONFIRM)
+ o.setState(WAIT_CONFIRM)
res
private def clear(im: NCIntentMatch, o: PizzeriaOrder): Unit =
@@ -150,10 +150,10 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
def onYes(im: NCIntentMatch): NCResult = withLog(
im,
(o: PizzeriaOrder) => o.getState match
- 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
+ case WAIT_CONFIRM => doExecute(im, o)
+ case WAIT_SHOULD_CANCEL => doStop(im, o)
+ case WAIT_IS_READY => askConfirmOrAskSpecify(o)
+ case WAIT_SPECIFY | NO_DIALOG => throw UNEXPECTED_REQUEST
)
/**
@@ -165,9 +165,9 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
def onNo(im: NCIntentMatch): NCResult = withLog(
im,
(o: PizzeriaOrder) => o.getState match
- case DIALOG_CONFIRM | DIALOG_IS_READY => doContinue(o)
- case DIALOG_SHOULD_CANCEL => askConfirmOrAskSpecify(o)
- case DIALOG_SPECIFY | NO_DIALOG => throw UNEXPECTED_REQUEST
+ case WAIT_CONFIRM | WAIT_IS_READY => doContinue(o)
+ case WAIT_SHOULD_CANCEL => askConfirmOrAskSpecify(o)
+ case WAIT_SPECIFY | NO_DIALOG => throw UNEXPECTED_REQUEST
)
/**
*
@@ -187,9 +187,9 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
def onStatus(im: NCIntentMatch): NCResult = withLog(
im,
(o: PizzeriaOrder) => o.getState match
- 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.
+ case WAIT_CONFIRM => askConfirm(o) // Ignore `status`, confirm
again.
+ case WAIT_SHOULD_CANCEL => doShowStatus(o, NO_DIALOG) // Changes
state.
+ case NO_DIALOG | WAIT_IS_READY | WAIT_SPECIFY => doShowStatus(o,
o.getState) // Keeps same state.
)
/**
*
@@ -200,9 +200,9 @@ class PizzeriaModel extends NCModelAdapter(new
NCModelConfig("nlpcraft.pizzeria.
def onFinish(im: NCIntentMatch): NCResult = withLog(
im,
(o: PizzeriaOrder) => o.getState match
- case DIALOG_CONFIRM => doExecuteOrAskSpecify(im, o) // Like YES if
valid.
- case DIALOG_SPECIFY => askSpecify(o) // Ignore `finish`, specify
again.
- case NO_DIALOG | DIALOG_IS_READY | DIALOG_SHOULD_CANCEL =>
askConfirmOrAskSpecify(o)
+ case WAIT_CONFIRM => doExecuteOrAskSpecify(im, o) // Like YES if
valid.
+ case WAIT_SPECIFY => askSpecify(o) // Ignore `finish`, specify
again.
+ case NO_DIALOG | WAIT_IS_READY | WAIT_SHOULD_CANCEL =>
askConfirmOrAskSpecify(o)
)
/**
*
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 40a0736a..3fb48f62 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,7 +26,7 @@ object PizzeriaModelPipeline:
private val ps = new PorterStemmer
override def stem(txt: String): String = ps.synchronized {
ps.stem(txt) }
- import ElementExtender as Ex, EntityData as D
+ import PizzeriaOrderExtender as Ex, EntityData as D
new NCPipelineBuilder().
withTokenParser(tokParser).
@@ -39,5 +39,5 @@ object PizzeriaModelPipeline:
Ex(Seq(D("ord:pizza", "ord:pizza:qty"), D("ord:drink",
"ord:drink:qty")), D("stanford:number", "stanford:number:nne")),
).asJava
).
- withEntityValidator(new RequestValidator()).
+ withEntityValidator(new PizzeriaOrderValidator()).
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 390c4d5a..97ff64f3 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
@@ -22,7 +22,6 @@ import scala.collection.mutable
object OrderElement:
val DFLT_QTY = 1
-
/**
*
*/
@@ -50,13 +49,7 @@ 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"
-/**
- * Order states.
- */
-enum OrderState:
- case NO_DIALOG, DIALOG_IS_READY, DIALOG_SHOULD_CANCEL, DIALOG_SPECIFY,
DIALOG_CONFIRM
-
-import org.apache.nlpcraft.examples.pizzeria.OrderState.*
+import org.apache.nlpcraft.examples.pizzeria.PizzeriaOrderState.*
/**
* Order.
@@ -137,13 +130,13 @@ class PizzeriaOrder:
*
* @return
*/
- def getState: OrderState = state
+ def getState: PizzeriaOrderState = state
/**
*
* @param state
*/
- def setState(state: OrderState): Unit = this.state = state
+ def setState(state: PizzeriaOrderState): Unit = this.state = state
/**
*
diff --git
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrderState.scala
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrderState.scala
new file mode 100644
index 00000000..0ee39134
--- /dev/null
+++
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/PizzeriaOrderState.scala
@@ -0,0 +1,24 @@
+/*
+ * 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
+ *
+ * http://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.examples.pizzeria
+
+/**
+ * Order states.
+ */
+enum PizzeriaOrderState:
+ case NO_DIALOG, WAIT_IS_READY, WAIT_SHOULD_CANCEL, WAIT_SPECIFY,
WAIT_CONFIRM
diff --git
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/ElementExtender.scala
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderExtender.scala
similarity index 95%
rename from
nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/ElementExtender.scala
rename to
nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderExtender.scala
index 6bc1089f..e148053f 100644
---
a/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/ElementExtender.scala
+++
b/nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderExtender.scala
@@ -44,21 +44,21 @@ case class EntityData(id: String, property: String)
* Note that it is simple example implementation.
* It just tries to unite nearest neighbours and doesn't check intermediate
words, order correctness etc.
*/
-object ElementExtender:
+object PizzeriaOrderExtender:
case class EntityHolder(entity: NCEntity):
lazy val position: Double =
val toks = entity.getTokens.asScala
(toks.head.getIndex + toks.last.getIndex) / 2.0
private def extract(e: NCEntity): mutable.Seq[NCToken] =
e.getTokens.asScala
-import ElementExtender.*
+import PizzeriaOrderExtender.*
/**
*
* @param mainDataSeq
* @param extraData
*/
-case class ElementExtender(mainDataSeq: Seq[EntityData], extraData:
EntityData) extends NCEntityMapper with LazyLogging:
+case class PizzeriaOrderExtender(mainDataSeq: Seq[EntityData], extraData:
EntityData) extends NCEntityMapper with LazyLogging:
override def map(req: NCRequest, cfg: NCModelConfig, entities:
JList[NCEntity]): JList[NCEntity] =
def combine(mainEnt: NCEntity, mainProp: String, extraEnt: NCEntity):
NCEntity =
new NCPropertyMapAdapter with NCEntity:
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/PizzeriaOrderValidator.scala
similarity index 94%
rename from
nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/RequestValidator.scala
rename to
nlpcraft-examples/pizzeria/src/main/java/org/apache/nlpcraft/examples/pizzeria/components/PizzeriaOrderValidator.scala
index 65abf9b9..43d50e40 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/PizzeriaOrderValidator.scala
@@ -25,7 +25,7 @@ import scala.jdk.CollectionConverters.*
/**
* Rejects some invalid variant with more detailed information instead of
standard rejections.
*/
-class RequestValidator extends NCEntityValidator:
+class PizzeriaOrderValidator extends NCEntityValidator:
override def validate(req: NCRequest, cfg: NCModelConfig, ents:
util.List[NCEntity]): Unit =
val es = ents.asScala
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 8ca241dd..5109ad32 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.OrderState.*
+import org.apache.nlpcraft.examples.pizzeria.PizzeriaOrderState.*
import org.junit.jupiter.api.*
import scala.language.implicitConversions
@@ -106,7 +106,7 @@ class PizzeriaModelSpec:
testNum += 1
- private def mkOrder(state: OrderState, ps: Seq[Pizza], ds: Seq[Drink]):
PizzeriaOrder =
+ private def mkOrder(state: PizzeriaOrderState, ps: Seq[Pizza], ds:
Seq[Drink]): PizzeriaOrder =
val o = new PizzeriaOrder
o.setState(state)
o.add(ps, ds)