This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-492
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-492 by this push:
new ce63ea1 NCEntityMapper implementation and related test added.
ce63ea1 is described below
commit ce63ea123d486d2f0229fc7d5f3b68c275c73280
Author: Sergey Kamov <[email protected]>
AuthorDate: Sun Apr 3 15:49:39 2022 +0300
NCEntityMapper implementation and related test added.
---
.../apache/nlpcraft/nlp/NCEntityMapperSpec.scala | 39 +++++++++++++---------
1 file changed, 23 insertions(+), 16 deletions(-)
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
index edd4ef3..255d53c 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
@@ -33,35 +33,42 @@ import scala.jdk.CollectionConverters.*
*
*/
class NCEntityMapperSpec:
- case class UnionMapper(id1: String, id2: String) extends NCEntityMapper:
+ private case class SummatorMapper(ids: String*) extends NCEntityMapper:
override def convert(req: NCRequest, entities: JList[NCEntity], toks:
JList[NCToken]): JList[NCEntity] =
val es = entities.asScala
+ val replaced = es.filter(p => ids.contains(p.getId))
- val id1AndId2 = es.filter(e => e.getId == id1 || e.getId == id2)
- val other = es.filter(e => !id1AndId2.contains(e))
+ if replaced.isEmpty then
+ entities
+ else
+ val newEntity: NCEntity = new NCPropertyMapAdapter with
NCEntity:
+ override val getTokens: JList[NCToken] =
replaced.flatMap(_.getTokens.asScala).sortBy(_.getIndex).asJava
+ override val getRequestId: String = req.getRequestId
+ override val getId: String = ids.mkString
- val newEntity = new NCPropertyMapAdapter with NCEntity:
- override def getTokens: JList[NCToken] =
id1AndId2.flatMap(_.getTokens.asScala).sortBy(_.getIndex).asJava
- override def getRequestId: String = req.getRequestId
- override def getId: String = s"$id1$id2"
+ (es.filter(e => !replaced.contains(e)) :+
newEntity).sortBy(_.getTokens.asScala.head.getIndex).asJava
- (other ++
Seq(newEntity)).sortBy(_.getTokens.get(0).getIndex).asJava
-
- private val mdl: NCTestModelAdapter = new NCTestModelAdapter:
+ private val mdl = new NCTestModelAdapter:
+ import NCSemanticTestElement as TE
override val getPipeline: NCPipeline =
val pl = mkEnPipeline
- import NCSemanticTestElement as TE
+ val ms = pl.getEntityMappers
+
pl.getEntityParsers.add(NCTestUtils.mkEnSemanticParser(TE("a"),
TE("b"), TE("c"), TE("d")))
+
// Replaces [a, b] -> [ab]
- pl.getEntityMappers.add(UnionMapper("a", "b"))
+ ms.add(SummatorMapper("a", "b"))
// Replaces [c, d] -> [cd]
- pl.getEntityMappers.add(UnionMapper("c", "d"))
+ ms.add(SummatorMapper("c", "d"))
// Replaces [ab, cd] -> [abcd]
- pl.getEntityMappers.add(UnionMapper("ab", "cd"))
+ ms.add(SummatorMapper("ab", "cd"))
+
pl
- @NCIntent("intent=i term(e)={# == 'abcd'}")
+ @NCIntent("intent=abcd term(e)={# == 'abcd'}")
def onMatch(@NCIntentTerm("e") e: NCEntity): NCResult = new
NCResult("OK", NCResultType.ASK_RESULT)
@Test
- def test(): Unit = Using.resource(new NCModelClient(mdl)) { client =>
require(client.ask("a b c d", null, "userId").getBody == "OK")}
+ def test(): Unit = Using.resource(new NCModelClient(mdl)) {
+ client => require(client.ask("a b c d", null, "userId").getIntentId ==
"abcd")
+ }