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

commit d12fe841b359152d00e345119fc84416e66cabc7
Author: Sergey Kamov <[email protected]>
AuthorDate: Wed Apr 6 22:28:47 2022 +0300

    WIP.
---
 .../nlpcraft/examples/order/OrderModel.scala       |  2 +-
 ...aQtyExtender.scala => DataExtenderMapper.scala} | 28 +++++-----
 .../order/components/DrinkQtyExtender.scala        | 32 ------------
 .../examples/order/components/OrderValidator.scala | 34 ------------
 .../order/components/PizzaSizeExtender.scala       | 60 ----------------------
 .../order/components/StanfordPipeline.scala        |  7 ++-
 6 files changed, 18 insertions(+), 145 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 9f4ba3bf..ae22304d 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
@@ -45,7 +45,7 @@ object OrderModel extends LazyLogging:
     private def extractPizzaSize(e: NCEntity): String = 
e.get[String]("ord:pizza:size:value")
     private def extractQty(e: NCEntity, qty: String): Option[Int] = 
Option.when(e.contains(qty))(e.get[String](qty).toDouble.toInt)
     private def extractPizza(e: NCEntity): Pizza =
-        Pizza(e.get[String]("ord:pizza:value"), 
e.getOpt[String]("ord:pizza:size").toScala, extractQty(e, "ord:pizza:qty"))
+        Pizza(e.get[String]("ord:pizza:value"), 
e.getOpt[String]("ord:pizza:size:value").toScala, extractQty(e, 
"ord:pizza:qty"))
     private def extractDrink(e: NCEntity): Drink =
         Drink(e.get[String]("ord:drink:value"), extractQty(e, "ord:drink:qty"))
 
diff --git 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/PizzaQtyExtender.scala
 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/DataExtenderMapper.scala
similarity index 63%
rename from 
nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/PizzaQtyExtender.scala
rename to 
nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/DataExtenderMapper.scala
index 5499f6d6..bc3813e7 100644
--- 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/PizzaQtyExtender.scala
+++ 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/DataExtenderMapper.scala
@@ -27,34 +27,30 @@ import scala.jdk.CollectionConverters.*
 /**
   *
   */
-class PizzaQtyExtender extends NCEntityMapper:
+case class DataExtenderMapper(key: String, prop: String, extKey: String, 
extProp: String) extends NCEntityMapper:
     private def extract(e: NCEntity): mutable.Seq[NCToken] = 
e.getTokens.asScala
     override def map(req: NCRequest, cfg: NCModelConfig, entities: 
util.List[NCEntity]): util.List[NCEntity] =
         var es = entities.asScala
-        val pizzas = es.filter(_.getId == "ord:pizza")
-        val nums = es.filter(_.getId == "stanford:number")
+        val data = es.filter(_.getId == key)
+        val extData = es.filter(_.getId == extKey)
 
-        if pizzas.nonEmpty && nums.nonEmpty then
-            if pizzas.size != nums.size then throw new NCRejection("Pizza and 
their nums should be defined together1")
+        if data.nonEmpty && data.size == extData.size then
             var ok = true
             val mapped =
-                for ((e1, e2) <- pizzas.zip(nums) if ok) yield
+                for ((e1, e2) <- data.zip(extData) if ok) yield
                     if e1.getId == e2.getId then
                         ok = false
                         null
                     else
-                        val (pizza, num) = if e1.getId == "ord:pizza" then 
(e1, e2) else (e2, e1)
+                        val (data, extData) = if e1.getId == key then (e1, e2) 
else (e2, e1)
                         new NCPropertyMapAdapter with NCEntity:
-                            // Copy from pizza.
-                            pizza.keysSet().forEach(k => put(k, pizza.get(k)))
-                            // New value from size.
-                            put[String]("ord:pizza:qty", 
num.get[String]("stanford:number:nne").toLowerCase)
-
-                            override val getTokens: JList[NCToken] = 
(extract(pizza) ++ extract(num)).sortBy(_.getIndex).asJava
+                            data.keysSet().forEach(k => put(k, data.get(k)))
+                            put[String](extProp, 
extData.get[String](extProp).toLowerCase)
+                            override val getTokens: JList[NCToken] = 
(extract(data) ++ extract(extData)).sortBy(_.getIndex).asJava
                             override val getRequestId: String = 
req.getRequestId
-                            override val getId: String = pizza.getId
+                            override val getId: String = data.getId
 
-            es = es --= pizzas
-            es = es --= nums
+            es = es --= data
+            es = es --= extData
             (es ++ mapped).sortBy(extract(_).head.getIndex).asJava
         else entities
\ No newline at end of file
diff --git 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/DrinkQtyExtender.scala
 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/DrinkQtyExtender.scala
deleted file mode 100644
index e3158153..00000000
--- 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/DrinkQtyExtender.scala
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.order.components
-
-import org.apache.nlpcraft.*
-
-import java.util
-import java.util.List as JList
-import scala.collection.mutable
-import scala.jdk.CollectionConverters.*
-
-/**
-  *
-  */
-class DrinkQtyExtender extends NCEntityMapper:
-    private def extract(e: NCEntity): mutable.Seq[NCToken] = 
e.getTokens.asScala
-    override def map(req: NCRequest, cfg: NCModelConfig, entities: 
util.List[NCEntity]): util.List[NCEntity] = entities
diff --git 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/OrderValidator.scala
 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/OrderValidator.scala
deleted file mode 100644
index dc51c804..00000000
--- 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/OrderValidator.scala
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.order.components
-
-import org.apache.nlpcraft.*
-
-import java.util
-import scala.jdk.CollectionConverters.*
-
-/**
-  *
-  */
-class OrderValidator extends NCEntityValidator:
-    override def validate(req: NCRequest, cfg: NCModelConfig, ents: 
util.List[NCEntity]): Unit =
-        val es = ents.asScala
-
-        if !es.exists(_.getId == "ord:pizza") then
-            if es.count(_.getId == "stanford:number") > 1 then throw new 
NCRejection("Error1") // TODO:
-            if es.count(_.getId == "ord:pizza:size") > 1 then throw new 
NCRejection("Error2") // TODO:
\ No newline at end of file
diff --git 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/PizzaSizeExtender.scala
 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/PizzaSizeExtender.scala
deleted file mode 100644
index 52106f25..00000000
--- 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/PizzaSizeExtender.scala
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.order.components
-
-import org.apache.nlpcraft.*
-
-import java.util
-import java.util.List as JList
-import scala.collection.mutable
-import scala.jdk.CollectionConverters.*
-
-/**
-  *
-  */
-class PizzaSizeExtender extends NCEntityMapper:
-    private def extract(e: NCEntity): mutable.Seq[NCToken] = 
e.getTokens.asScala
-    override def map(req: NCRequest, cfg: NCModelConfig, entities: 
util.List[NCEntity]): util.List[NCEntity] =
-        var es = entities.asScala
-        val pizzas = es.filter(_.getId == "ord:pizza")
-        val sizes = es.filter(_.getId == "ord:pizza:size")
-
-        if pizzas.nonEmpty && sizes.nonEmpty then
-            if pizzas.size != sizes.size then throw new NCRejection("Pizza and 
their sizes should be defined together1")
-            var ok = true
-            val mapped =
-                for ((e1, e2) <- pizzas.zip(sizes) if ok) yield
-                    if e1.getId == e2.getId then
-                        ok = false
-                        null
-                    else
-                        val (pizza, size) = if e1.getId == "ord:pizza" then 
(e1, e2) else (e2, e1)
-                        new NCPropertyMapAdapter with NCEntity:
-                            // Copy from pizza.
-                            size.keysSet().forEach(k => put(k, size.get(k)))
-                            // New value from size.
-                            put[String]("ord:pizza:size", 
size.get[String]("ord:pizza:size:value").toLowerCase)
-
-                            override val getTokens: JList[NCToken] = 
(extract(pizza) ++ extract(size)).sortBy(_.getIndex).asJava
-                            override val getRequestId: String = 
req.getRequestId
-                            override val getId: String = pizza.getId
-
-            es = es --= pizzas
-            es = es --= sizes
-            (es ++ mapped).sortBy(extract(_).head.getIndex).asJava
-        else entities
\ No newline at end of file
diff --git 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/StanfordPipeline.scala
 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/StanfordPipeline.scala
index 723db572..52f1dcea 100644
--- 
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/StanfordPipeline.scala
+++ 
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/StanfordPipeline.scala
@@ -44,6 +44,9 @@ object StanfordPipeline:
             withTokenParser(tokParser).
             withEntityParser(new NCStanfordNLPEntityParser(stanford, 
"number")).
             withEntityParser(new NCSemanticEntityParser(stemmer, tokParser, 
"order_model.yaml")).
-            withEntityMappers(Seq(new PizzaSizeExtender, new PizzaQtyExtender, 
new DrinkQtyExtender).asJava).
-            withEntityValidator(new OrderValidator).
+            withEntityMappers(Seq(
+                DataExtenderMapper(key = "ord:pizza", prop = "ord:pizza:size", 
extKey = "ord:pizza:size", extProp = "ord:pizza:size:value"),
+                DataExtenderMapper(key = "ord:pizza", prop = "ord:pizza:qty", 
extKey = "stanford:number", extProp = "stanford:number:nne"),
+                DataExtenderMapper(key = "ord:drink", prop = "ord:drink:qty", 
extKey = "stanford:number", extProp = "stanford:number:nne")
+            ).asJava).
             build()
\ No newline at end of file

Reply via email to