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 e200d34a WIP.
e200d34a is described below
commit e200d34a07a410fa4b83ab2515ccfe2f5c04a470
Author: Sergey Kamov <[email protected]>
AuthorDate: Mon Apr 4 21:44:04 2022 +0300
WIP.
---
.../examples/order/components/SimpleCombiner.scala | 61 ++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git
a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/SimpleCombiner.scala
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/SimpleCombiner.scala
new file mode 100644
index 00000000..025da0c1
--- /dev/null
+++
b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/components/SimpleCombiner.scala
@@ -0,0 +1,61 @@
+/*
+ * 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.jdk.CollectionConverters.*
+
+/**
+ *
+ * @param id1
+ * @param id2
+ * @param newId
+ */
+case class SimpleCombiner(id1: String, id2: String, newId: String) extends
NCEntityMapper:
+ override def map(req: NCRequest, cfg: NCModelConfig, entities:
util.List[NCEntity]): util.List[NCEntity] =
+ var es = entities.asScala
+ val es1 = es.filter(_.getId == id1)
+ val es2 = es.filter(_.getId == id2)
+
+ if es1.nonEmpty && es2.size == es1.size then
+ var ok = true
+
+ val newEs =
+ for ((e1, e2) <- es1.zip(es2) if ok) yield
+ if e1.getId == e2.getId then
+ ok = false
+ null
+ else
+ new NCPropertyMapAdapter with NCEntity:
+ override val getTokens: JList[NCToken] =
(e1.getTokens.asScala ++ e2.getTokens.asScala).sortBy(_.getIndex).asJava
+ override val getRequestId: String =
req.getRequestId
+ override val getId: String = newId
+
+ if ok then
+ es = es --= es1
+ es = es --= es2
+ (es ++ newEs).sortBy(_.getTokens.asScala.head.getIndex).asJava
+ else
+ entities
+ else
+ entities
+
+