This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-376
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-376 by this push:
     new 0c912a3  WIP.
0c912a3 is described below

commit 0c912a3eb70ce25d42c9b2e1aa78c8063f6dc424
Author: Sergey Kamov <[email protected]>
AuthorDate: Wed Jul 28 13:36:43 2021 +0300

    WIP.
---
 .../model/intent/solver/NCIntentSolver.scala       |   2 +-
 .../models/stm/NCStmIndexesTestModelSpec.scala     | 106 ---------------------
 .../models/stm/indexes/NCStmLimitSpec.scala        |  61 ++++++++++++
 .../models/stm/indexes/NCStmRelationSpec.scala     |  61 ++++++++++++
 .../models/stm/indexes/NCStmSortSpec.scala         |  66 +++++++++++++
 .../models/stm/indexes/NCStmSpecModelAdapter.scala |  53 +++++++++++
 6 files changed, 242 insertions(+), 107 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
index fa77941..ba9daf5 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
@@ -290,7 +290,7 @@ class NCIntentSolver(intents: List[(NCIdlIntent/*Intent*/, 
NCIntentMatch => NCRe
                     val ref = getForRecalc(refId, Seq(refIdx), _.size == 
1).head
 
                     convTok.getMetadata.put(s"nlpcraft:limit:note", ref.getId)
-                    convTok.getMetadata.put(s"nlpcraft:limit:indexes", 
Collections.singleton(ref.getIndex))
+                    convTok.getMetadata.put(s"nlpcraft:limit:indexes", 
Collections.singletonList(ref.getIndex))
                 }
 
             case "nlpcraft:relation" =>
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala
deleted file mode 100644
index 173f139..0000000
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala
+++ /dev/null
@@ -1,106 +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
- *
- *      https://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.models.stm
-
-import com.fasterxml.jackson.databind.ObjectMapper
-import com.fasterxml.jackson.module.scala.DefaultScalaModule
-import org.apache.nlpcraft.model.{NCElement, NCIntent, NCIntentMatch, 
NCModelAdapter, NCResult, _}
-import org.apache.nlpcraft.models.stm.NCStmIndexesTestModelData.mapper
-import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
-import org.junit.jupiter.api.Test
-
-import java.util
-import java.util.Collections
-import scala.jdk.CollectionConverters.{ListHasAsScala, MapHasAsScala, 
SeqHasAsJava, SetHasAsJava}
-import scala.language.implicitConversions
-import java.util.{List => JList}
-
-object NCStmIndexesTestModelData {
-    val mapper = new ObjectMapper()
-
-    mapper.registerModule(new DefaultScalaModule())
-}
-
-case class NCStmIndexesTestModelData(
-    subjnotes: Seq[String] = Seq.empty,
-    subjindexes: Seq[Int] = Seq.empty,
-    bynotes: Seq[String] = Seq.empty,
-    byindexes: Seq[Int] = Seq.empty
-)
-
-class NCStmIndexesTestModel extends NCModelAdapter("nlpcraft.stm.idxs.test", 
"STM Indexes Test Model", "1.0") {
-    override def getElements: util.Set[NCElement] =
-        Set(
-            mkElement("A", "G", "a a"),
-            mkElement("B", "G", "b b")
-        ).asJava
-
-    private def mkElement(id: String, group: String, syns: String*): NCElement 
=
-        new NCElement {
-            override def getId: String = id
-            override def getSynonyms: util.List[String] = {
-                val seq: Seq[String] = syns
-
-                seq.asJava
-            }
-            override def getGroups: util.List[String] = 
Collections.singletonList(group)
-        }
-
-    @NCIntent("intent=onBySort term(sort)~{tok_id() == 'nlpcraft:sort'} 
term(elem)={has(tok_groups(), 'G')}")
-    private def onBySort(ctx: NCIntentMatch, @NCIntentTerm("sort") sort: 
NCToken): NCResult = {
-        def toStr(t: NCToken): String = s"${t.origText}(${t.index})"
-
-        println(s"sort: $sort")
-        println(s"sort-nlp-meta: 
${sort.getMetadata.asScala.filter(_._1.startsWith("nlpcraft:nlp:")).mkString(", 
")}")
-        println(s"sort-not-nlp-meta: 
${sort.getMetadata.asScala.filter(!_._1.startsWith("nlpcraft:nlp:")).mkString(",
 ")}")
-
-        println(s"variant: ${ctx.getVariant.asScala.map(toStr).mkString("|")}")
-        println(s"variant freeTokens: 
${ctx.getVariant.getFreeTokens.asScala.map(toStr).mkString("|")}")
-        println(s"variant matchedTokens: 
${ctx.getVariant.getMatchedTokens.asScala.map(toStr).mkString("|")}")
-        println(s"variant userDefinedTokens: 
${ctx.getVariant.getUserDefinedTokens.asScala.map(toStr).mkString("|")}")
-        println(s"variant conversation: 
${ctx.getContext.getConversation.getTokens.asScala.map(toStr).mkString("|")}")
-
-        NCResult.json(
-            mapper.writeValueAsString(
-                NCStmIndexesTestModelData(
-                    bynotes = 
sort.meta[JList[String]]("nlpcraft:sort:bynotes").asScala.toSeq,
-                    byindexes = 
sort.meta[JList[Int]]("nlpcraft:sort:byindexes").asScala.toSeq
-                )
-            )
-        )
-    }
-}
-
-@NCTestEnvironment(model = classOf[NCStmIndexesTestModel], startClient = true)
-class NCStmIndexesTestModelSpec extends NCTestContext {
-    private def extract(s: String): NCStmIndexesTestModelData = 
mapper.readValue(s, classOf[NCStmIndexesTestModelData])
-
-    @Test
-    private[stm] def test(): Unit = {
-        checkResult(
-            "test test sort by a a",
-            extract,
-            NCStmIndexesTestModelData(bynotes = Seq("A"), byindexes = Seq(3))
-        )
-        checkResult(
-            "test b b",
-            extract,
-            NCStmIndexesTestModelData(bynotes = Seq("B"), byindexes = Seq(1))
-        )
-    }
-}
\ No newline at end of file
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmLimitSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmLimitSpec.scala
new file mode 100644
index 0000000..5feee96
--- /dev/null
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmLimitSpec.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
+ *
+ *      https://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.models.stm.indexes
+
+import org.apache.nlpcraft.model.{NCIntent, NCIntentMatch, NCResult, _}
+import org.apache.nlpcraft.models.stm.indexes.NCStmSpecModelAdapter.mapper
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
+import org.junit.jupiter.api.Test
+
+import java.util.{List => JList}
+import scala.jdk.CollectionConverters.ListHasAsScala
+import scala.language.implicitConversions
+
+case class NCStmLimitSpecModelData(note: String, indexes: Seq[Int])
+
+class NCStmLimitSpecModel extends NCStmSpecModelAdapter {
+    @NCIntent("intent=limit term(limit)~{tok_id() == 'nlpcraft:limit'} 
term(elem)~{has(tok_groups(), 'G')}")
+    private def onLimit(ctx: NCIntentMatch, @NCIntentTerm("limit") limit: 
NCToken): NCResult =
+        NCResult.json(
+            mapper.writeValueAsString(
+                NCStmLimitSpecModelData(
+                    note = limit.meta[String]("nlpcraft:limit:note"),
+                    indexes = 
limit.meta[JList[Int]]("nlpcraft:limit:indexes").asScala.toSeq
+                )
+            )
+        )
+}
+
+@NCTestEnvironment(model = classOf[NCStmLimitSpecModel], startClient = true)
+class NCStmLimitSpec extends NCTestContext {
+    private def extract(s: String): NCStmLimitSpecModelData = 
mapper.readValue(s, classOf[NCStmLimitSpecModelData])
+
+    @Test
+    private[stm] def test(): Unit = {
+        checkResult(
+            "top 23 a a",
+            extract,
+            NCStmLimitSpecModelData(note = "A", indexes = Seq(1))
+        )
+        checkResult(
+            "test test b b",
+            extract,
+            NCStmLimitSpecModelData(note = "B", indexes = Seq(2))
+        )
+    }
+}
\ No newline at end of file
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmRelationSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmRelationSpec.scala
new file mode 100644
index 0000000..745a78a
--- /dev/null
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmRelationSpec.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
+ *
+ *      https://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.models.stm.indexes
+
+import org.apache.nlpcraft.model.{NCIntent, NCIntentMatch, NCResult, _}
+import org.apache.nlpcraft.models.stm.indexes.NCStmSpecModelAdapter.mapper
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
+import org.junit.jupiter.api.Test
+
+import java.util.{List => JList}
+import scala.jdk.CollectionConverters.ListHasAsScala
+import scala.language.implicitConversions
+
+case class NCStmRelationSpecModelData(note: String, indexes: Seq[Int])
+
+class NCStmRelationSpecModel extends NCStmSpecModelAdapter {
+    @NCIntent("intent=rel term(rel)~{tok_id() == 'nlpcraft:relation'} 
term(elem)~{has(tok_groups(), 'G')}*")
+    private def onRelation(ctx: NCIntentMatch, @NCIntentTerm("rel") rel: 
NCToken): NCResult =
+        NCResult.json(
+            mapper.writeValueAsString(
+                NCStmLimitSpecModelData(
+                    note = rel.meta[String]("nlpcraft:relation:note"),
+                    indexes = 
rel.meta[JList[Int]]("nlpcraft:relation:indexes").asScala.toSeq
+                )
+            )
+        )
+}
+
+@NCTestEnvironment(model = classOf[NCStmRelationSpecModel], startClient = true)
+class NCStmRelationSpec extends NCTestContext {
+    private def extract(s: String): NCStmRelationSpecModelData = 
mapper.readValue(s, classOf[NCStmRelationSpecModelData])
+
+    @Test
+    private[stm] def test(): Unit = {
+        checkResult(
+            "compare a a and a a",
+            extract,
+            NCStmRelationSpecModelData(note = "A", indexes = Seq(1, 3))
+        )
+        checkResult(
+            "b b and b b",
+            extract,
+            NCStmRelationSpecModelData(note = "B", indexes = Seq(0, 2))
+        )
+    }
+}
\ No newline at end of file
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmSortSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmSortSpec.scala
new file mode 100644
index 0000000..cb932cb
--- /dev/null
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmSortSpec.scala
@@ -0,0 +1,66 @@
+/*
+ * 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
+ *
+ *      https://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.models.stm.indexes
+
+import org.apache.nlpcraft.model.{NCIntent, NCIntentMatch, NCResult, _}
+import org.apache.nlpcraft.models.stm.indexes.NCStmSpecModelAdapter.mapper
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
+import org.junit.jupiter.api.Test
+
+import java.util.{List => JList}
+import scala.jdk.CollectionConverters.ListHasAsScala
+import scala.language.implicitConversions
+
+case class NCStmSortSpecModelData(
+    subjnotes: Seq[String] = Seq.empty,
+    subjindexes: Seq[Int] = Seq.empty,
+    bynotes: Seq[String] = Seq.empty,
+    byindexes: Seq[Int] = Seq.empty
+)
+
+class NCStmSortSpecModel extends NCStmSpecModelAdapter {
+    @NCIntent("intent=onBySort term(sort)~{tok_id() == 'nlpcraft:sort'} 
term(elem)~{has(tok_groups(), 'G')}")
+    private def onBySort(ctx: NCIntentMatch, @NCIntentTerm("sort") sort: 
NCToken): NCResult =
+        NCResult.json(
+            mapper.writeValueAsString(
+                NCStmSortSpecModelData(
+                    bynotes = 
sort.meta[JList[String]]("nlpcraft:sort:bynotes").asScala.toSeq,
+                    byindexes = 
sort.meta[JList[Int]]("nlpcraft:sort:byindexes").asScala.toSeq
+                )
+            )
+        )
+}
+
+@NCTestEnvironment(model = classOf[NCStmSortSpecModel], startClient = true)
+class NCStmSortSpec extends NCTestContext {
+    private def extract(s: String): NCStmSortSpecModelData = 
mapper.readValue(s, classOf[NCStmSortSpecModelData])
+
+    @Test
+    private[stm] def test(): Unit = {
+        checkResult(
+            "test test sort by a a",
+            extract,
+            NCStmSortSpecModelData(bynotes = Seq("A"), byindexes = Seq(3))
+        )
+        checkResult(
+            "test b b",
+            extract,
+            NCStmSortSpecModelData(bynotes = Seq("B"), byindexes = Seq(1))
+        )
+    }
+}
\ No newline at end of file
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmSpecModelAdapter.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmSpecModelAdapter.scala
new file mode 100644
index 0000000..133ab23
--- /dev/null
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCStmSpecModelAdapter.scala
@@ -0,0 +1,53 @@
+/*
+ * 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
+ *
+ *      https://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.models.stm.indexes
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+import org.apache.nlpcraft.model.{NCElement, NCModelAdapter}
+
+import java.util
+import java.util.Collections
+import scala.jdk.CollectionConverters.{SeqHasAsJava, SetHasAsJava}
+
+object NCStmSpecModelAdapter {
+    val mapper = new ObjectMapper()
+
+    mapper.registerModule(new DefaultScalaModule())
+}
+
+class NCStmSpecModelAdapter extends NCModelAdapter("nlpcraft.stm.idxs.test", 
"STM Indexes Test Model", "1.0") {
+    override def getElements: util.Set[NCElement] =
+        Set(
+            mkElement("A", "G", "a a"),
+            mkElement("B", "G", "b b")
+        ).asJava
+
+    private def mkElement(id: String, group: String, syns: String*): NCElement 
=
+        new NCElement {
+            override def getId: String = id
+            override def getSynonyms: util.List[String] = {
+                val seq: Seq[String] = syns
+
+                seq.asJava
+            }
+            override def getGroups: util.List[String] = 
Collections.singletonList(group)
+        }
+
+
+}

Reply via email to