You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2021/02/26 18:48:16 UTC

[incubator-nlpcraft] branch master updated: Model enricher performance improvements.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a13ca5d  Model enricher performance improvements.
a13ca5d is described below

commit a13ca5db2c3c3216971715a4d45fb10ee1ddb134
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Fri Feb 26 21:48:03 2021 +0300

    Model enricher performance improvements.
---
 .../test/impl/NCTestAutoModelValidatorImpl.scala   | 18 ++++--
 .../apache/nlpcraft/probe/mgrs/NCProbeModel.scala  |  2 +-
 .../probe/mgrs/NCProbeSynonymsWrapper.scala        | 51 ++++++++++++++++
 .../probe/mgrs/deploy/NCDeployManager.scala        | 11 ++--
 .../nlpcraft/probe/mgrs/model/NCModelManager.scala |  2 +-
 .../mgrs/nlp/enrichers/model/NCModelEnricher.scala | 62 ++++++++++++++-----
 .../nlpcraft/model/jiggle/NCJiggleSpec.scala       |  7 +--
 .../nlpcraft/model/synonyms/NCSynonymsSpec.scala   | 71 ++++++++++++++++++++++
 8 files changed, 193 insertions(+), 31 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala
index 30dca04..36a9cca 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/test/impl/NCTestAutoModelValidatorImpl.scala
@@ -66,7 +66,8 @@ private [test] object NCTestAutoModelValidatorImpl extends LazyLogging {
             intentId: String,
             text: String,
             pass: Boolean,
-            error: Option[String]
+            error: Option[String],
+            time: Long
         )
 
         val results = samples.flatMap { case (mdlId, samples) ⇒
@@ -77,14 +78,18 @@ private [test] object NCTestAutoModelValidatorImpl extends LazyLogging {
                     cli.open(mdlId)
 
                     txts.map (txt ⇒ {
+                        var t = System.currentTimeMillis()
+
                         val res = cli.ask(txt)
 
+                        t = System.currentTimeMillis() - t
+
                         if (res.isFailed)
-                            Result(mdlId, intentId, txt, pass = false, Some(res.getResultError.get()))
+                            Result(mdlId, intentId, txt, pass = false, Some(res.getResultError.get()), t)
                         else if (intentId != res.getIntentId)
-                            Result(mdlId, intentId, txt, pass = false, Some(s"Unexpected intent ID '${res.getIntentId}'"))
+                            Result(mdlId, intentId, txt, pass = false, Some(s"Unexpected intent ID '${res.getIntentId}'"), t)
                         else
-                            Result(mdlId, intentId, txt, pass = true, None)
+                            Result(mdlId, intentId, txt, pass = true, None, t)
                     })
                 }
                 finally
@@ -96,7 +101,7 @@ private [test] object NCTestAutoModelValidatorImpl extends LazyLogging {
 
         val tbl = NCAsciiTable()
 
-        tbl #= ("Model ID", "Intent ID", "+/-", "Text", "Error")
+        tbl #= ("Model ID", "Intent ID", "+/-", "Text", "Error", "Execution time")
 
         for (res ← results)
             tbl += (
@@ -104,7 +109,8 @@ private [test] object NCTestAutoModelValidatorImpl extends LazyLogging {
                 res.intentId,
                 if (res.pass) g("OK") else r("FAIL"),
                 res.text,
-                res.error.getOrElse("")
+                res.error.getOrElse(""),
+                res.time
             )
         
         val passCnt = results.count(_.pass)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeModel.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeModel.scala
index fae4496..acc2021 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeModel.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeModel.scala
@@ -39,7 +39,7 @@ case class NCProbeModel(
     model: NCModel,
     solver: NCIntentSolver,
     intents: Seq[NCDslIntent],
-    synonyms: Map[String /*Element ID*/ , Map[Int /*Synonym length*/ , Seq[NCProbeSynonym]]], // Fast access map.
+    synonyms: Map[String /*Element ID*/ , Map[Int /*Synonym length*/ , NCProbeSynonymsWrapper]], // Fast access map.
     synonymsDsl: Map[String /*Element ID*/ , Map[Int /*Synonym length*/ , Seq[NCProbeSynonym]]], // Fast access map.
     addStopWordsStems: Set[String],
     exclStopWordsStems: Set[String],
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeSynonymsWrapper.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeSynonymsWrapper.scala
new file mode 100644
index 0000000..1fc2afe
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/NCProbeSynonymsWrapper.scala
@@ -0,0 +1,51 @@
+/*
+ * 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.probe.mgrs
+
+/**
+  * Synonyms sequence holder.
+  */
+case class NCProbeSynonymsWrapper(
+    txtDirectSynonyms: Map[String, NCProbeSynonym],
+    txtNotDirectSynonyms: Map[String, NCProbeSynonym],
+    notTxtDirectSynonyms: Seq[NCProbeSynonym],
+    notTxtNotDirectSynonyms: Seq[NCProbeSynonym],
+    count: Int
+)
+
+object NCProbeSynonymsWrapper {
+    def apply(syns: Seq[NCProbeSynonym]): NCProbeSynonymsWrapper = {
+        // When it converted to map, more important values will be last and previous
+        // (less important elements) will be overridden.
+        val txtSyns = syns.filter(_.isTextOnly).sorted
+
+        // Required order by importance.
+        val other = syns.filter(!_.isTextOnly).sorted.reverse
+
+        def filter(seq: Seq[NCProbeSynonym], direct: Boolean): Seq[NCProbeSynonym] = seq.filter(_.isDirect == direct)
+        def toMap(seq: Seq[NCProbeSynonym]): Map[String, NCProbeSynonym] = seq.map(s ⇒ s.stems → s).toMap
+
+        NCProbeSynonymsWrapper(
+            txtDirectSynonyms = toMap(filter(txtSyns, direct = true)),
+            txtNotDirectSynonyms = toMap(filter(txtSyns, direct = false)),
+            notTxtDirectSynonyms = filter(other, direct = true),
+            notTxtNotDirectSynonyms = filter(other, direct = false),
+            count = syns.size
+        )
+    }
+}
\ No newline at end of file
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
index 8d18804..a68e305 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
@@ -36,7 +36,7 @@ import org.apache.nlpcraft.model.factories.basic.NCBasicModelFactory
 import org.apache.nlpcraft.model.intent.impl.{NCIntentDslCompiler, NCIntentSolver}
 import org.apache.nlpcraft.model.intent.utils.NCDslIntent
 import org.apache.nlpcraft.probe.mgrs.NCProbeSynonymChunkKind.{DSL, REGEX, TEXT}
-import org.apache.nlpcraft.probe.mgrs.{NCProbeModel, NCProbeSynonym, NCProbeSynonymChunk}
+import org.apache.nlpcraft.probe.mgrs.{NCProbeModel, NCProbeSynonym, NCProbeSynonymChunk, NCProbeSynonymsWrapper}
 import org.apache.nlpcraft.probe.mgrs.model.NCModelSynonymDslCompiler
 import resource.managed
 
@@ -502,8 +502,8 @@ object NCDeployManager extends NCService with DecorateAsScala {
             model = mdl,
             solver = solver,
             intents = intents.keySet.toSeq,
-            synonyms = mkFastAccessMap(filter(syns, dsl = false)),
-            synonymsDsl = mkFastAccessMap(filter(syns, dsl = true)),
+            synonyms = mkFastAccessMap(filter(syns, dsl = false), NCProbeSynonymsWrapper(_)),
+            synonymsDsl = mkFastAccessMap(filter(syns, dsl = true), seq ⇒ seq),
             addStopWordsStems = addStopWords.toSet,
             exclStopWordsStems = exclStopWords.toSet,
             suspWordsStems = suspWords.toSet,
@@ -550,7 +550,8 @@ object NCDeployManager extends NCService with DecorateAsScala {
       * @param set
       * @return
       */
-    private def mkFastAccessMap(set: Set[SynonymHolder]): Map[String /*Element ID*/ , Map[Int /*Synonym length*/ , Seq[NCProbeSynonym]]] =
+    private def mkFastAccessMap[T](set: Set[SynonymHolder], f: Seq[NCProbeSynonym] ⇒ T):
+        Map[String /*Element ID*/ , Map[Int /*Synonym length*/ , T]] =
         set
             .groupBy(_.elmId)
             .map {
@@ -561,7 +562,7 @@ object NCDeployManager extends NCService with DecorateAsScala {
                         .groupBy(_.size)
                         .map {
                             // Sort synonyms from most important to least important.
-                            case (k, v) ⇒ (k, v.toSeq.sorted.reverse)
+                            case (k, v) ⇒ (k, f(v.toSeq))
                         }
                 )
             }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
index 43f108d..1cc0fc9 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
@@ -59,7 +59,7 @@ object NCModelManager extends NCService with DecorateAsScala {
             data.values.foreach(w ⇒ {
                 val mdl = w.model
 
-                val synCnt = w.synonyms.values.flatMap(_.values).flatten.size
+                val synCnt = w.synonyms.flatMap(_._2.map(_._2.count)).sum
                 val elmCnt = w.elements.keySet.size
                 val intentCnt = w.intents.size
 
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCModelEnricher.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCModelEnricher.scala
index bea4eaa..c4a7936 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCModelEnricher.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCModelEnricher.scala
@@ -25,7 +25,7 @@ import org.apache.nlpcraft.common.nlp.{NCNlpSentenceToken, NCNlpSentenceTokenBuf
 import org.apache.nlpcraft.model._
 import org.apache.nlpcraft.probe.mgrs.nlp.NCProbeEnricher
 import org.apache.nlpcraft.probe.mgrs.nlp.impl.NCRequestImpl
-import org.apache.nlpcraft.probe.mgrs.{NCProbeModel, NCProbeSynonym, NCProbeVariants}
+import org.apache.nlpcraft.probe.mgrs.{NCProbeModel, NCProbeSynonym, NCProbeSynonymsWrapper, NCProbeVariants}
 
 import scala.collection.JavaConverters._
 import scala.compat.java8.OptionConverters._
@@ -328,21 +328,21 @@ object NCModelEnricher extends NCProbeEnricher with DecorateAsScala {
             val matches = ArrayBuffer.empty[ElementMatch]
 
             /**
-              * Gets sequence of synonyms sorted in descending order by their weight, i.e. first synonym in
-              * the sequence is the most important one.
+              * Gets synonyms sorted in descending order by their weight (already prepared),
+              * i.e. first synonym in the sequence is the most important one.
               *
               * @param fastMap
               * @param elmId
               * @param len
-              * @return
               */
-            def fastAccess(
-                fastMap: Map[String /*Element ID*/ , Map[Int /*Synonym length*/ , Seq[NCProbeSynonym]]],
+            def fastAccess[T](
+                fastMap: Map[String /*Element ID*/, Map[Int /*Synonym length*/, T]],
                 elmId: String,
-                len: Int): Seq[NCProbeSynonym] =
-                fastMap.get(elmId).flatMap(_.get(len)) match {
-                    case Some(seq) ⇒ seq
-                    case None ⇒ Seq.empty[NCProbeSynonym]
+                len: Int
+            ): Option[T] =
+                fastMap.get(elmId) match {
+                    case Some(m) ⇒ m.get(len)
+                    case None ⇒ None
                 }
 
             /**
@@ -388,9 +388,39 @@ object NCModelEnricher extends NCProbeEnricher with DecorateAsScala {
 
                             // Optimization - plain synonyms can be used only on first iteration
                             if (mdl.synonyms.nonEmpty && !ns.exists(_.isUser))
-                                for (syn ← fastAccess(mdl.synonyms, elm.getId, toks.length) if !found)
-                                    if (syn.isMatch(toks))
-                                        addMatch(elm, toks, syn, Seq.empty)
+                                fastAccess(mdl.synonyms, elm.getId, toks.length) match {
+                                    case Some(h) ⇒
+                                        val stems = toks.map(_.stem).mkString(" ")
+
+                                        def tryMap(synsMap: Map[String, NCProbeSynonym], notFound: () ⇒ Unit): Unit =
+                                            synsMap.get(stems) match {
+                                                case Some(syn) ⇒
+                                                    addMatch(elm, toks, syn, Seq.empty)
+
+                                                    if (!found)
+                                                        notFound()
+                                                case None ⇒ notFound()
+                                            }
+
+                                        def tryScan(synsSeq: Seq[NCProbeSynonym]): Unit =
+                                            for (syn ← synsSeq if !found)
+                                                if (syn.isMatch(toks))
+                                                    addMatch(elm, toks, syn, Seq.empty)
+
+                                        tryMap(
+                                            h.txtDirectSynonyms,
+                                            () ⇒ {
+                                                tryScan(h.notTxtDirectSynonyms)
+
+                                                if (!found)
+                                                    tryMap(
+                                                        h.txtNotDirectSynonyms,
+                                                        () ⇒ tryScan(h.notTxtNotDirectSynonyms)
+                                                    )
+                                            }
+                                        )
+                                    case None ⇒ // No-op.
+                                }
 
                             if (mdl.synonymsDsl.nonEmpty) {
                                 found = false
@@ -403,7 +433,11 @@ object NCModelEnricher extends NCProbeEnricher with DecorateAsScala {
                                 if (seq == null)
                                     seq = convert(ns, collapsedSens, toks)
 
-                                for (comb ← seq; syn ← fastAccess(mdl.synonymsDsl, elm.getId, comb.length) if !found)
+                                for (
+                                    comb ← seq;
+                                    syn ← fastAccess(mdl.synonymsDsl, elm.getId, comb.length).getOrElse(Seq.empty)
+                                    if !found
+                                )
                                     if (syn.isMatch(comb.map(_.data)))
                                         addMatch(elm, toks, syn, comb.filter(_.isToken).map(_.token))
                             }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/jiggle/NCJiggleSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/jiggle/NCJiggleSpec.scala
index 24ca12c..6f0ed53 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/jiggle/NCJiggleSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/jiggle/NCJiggleSpec.scala
@@ -26,7 +26,7 @@ import java.util
 import scala.collection.JavaConverters._
 import scala.collection.mutable
 
-class NJiggleModel1 extends NCAbstractTokensModel {
+class NJiggleModel extends NCAbstractTokensModel {
     override def getElements: util.Set[NCElement] = Set(NCTestElement("xyz", "x y z"))
 
     // Default values.
@@ -78,11 +78,10 @@ class NJiggleModel1 extends NCAbstractTokensModel {
 
         NCResult.text("OK")
     }
-
 }
 
-@NCTestEnvironment(model = classOf[NJiggleModel1], startClient = true)
-class NCJiggleSpec1 extends NCTestContext {
+@NCTestEnvironment(model = classOf[NJiggleModel], startClient = true)
+class NCJiggleSpec extends NCTestContext {
     @Test
     def test(): Unit = {
         checkResult("x y z x y z x y z", "OK")
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/synonyms/NCSynonymsSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/synonyms/NCSynonymsSpec.scala
new file mode 100644
index 0000000..efde9da
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/synonyms/NCSynonymsSpec.scala
@@ -0,0 +1,71 @@
+/*
+ * 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.model.synonyms
+
+import org.apache.nlpcraft.model.{NCElement, NCIntent, NCIntentMatch, NCModelAdapter, NCResult}
+import org.apache.nlpcraft.{NCTestContext, NCTestElement, NCTestEnvironment}
+import org.junit.jupiter.api.Test
+
+import java.util
+
+class NCSynonymsSpecModel extends NCModelAdapter("nlpcraft.syns.test.mdl", "Synonyms Test Model", "1.0") {
+    // Default values.
+    override def isPermutateSynonyms: Boolean = true
+    override def getJiggleFactor: Int = 4
+
+    override def getElements: util.Set[NCElement] =
+        Set(
+            NCTestElement("e1", "A"),
+
+            // Text - 3 words.
+            NCTestElement("e2", "X Y Z"),
+
+            // Regex - 3 words.
+            NCTestElement("e3", "{//AA//}[3, 3]"),
+
+            // Nested - 3 words.
+            NCTestElement("e4", "{^^(id == 'e1')^^}[3, 3]")
+        )
+
+    @NCIntent("intent=onE1 term(t)={id == 'e1'}")
+    def onE1(ctx: NCIntentMatch): NCResult = NCResult.text("OK")
+
+    @NCIntent("intent=onE2 term(t)={id == 'e2'}")
+    def onE2(ctx: NCIntentMatch): NCResult = NCResult.text("OK")
+
+    @NCIntent("intent=onE3 term(t)={id == 'e3'}")
+    def onE3(ctx: NCIntentMatch): NCResult = NCResult.text("OK")
+
+    @NCIntent("intent=onE4 term(t)={id == 'e4'}")
+    def onE4(ctx: NCIntentMatch): NCResult = NCResult.text("OK")
+}
+
+@NCTestEnvironment(model = classOf[NCSynonymsSpecModel], startClient = true)
+class NCSynonymsSpec extends NCTestContext {
+    @Test
+    def test(): Unit = {
+        checkIntent("A", "onE1")
+
+        checkIntent("X Y Z", "onE2") // Text direct.
+        checkIntent("Y X Z", "onE2") // Text not direct.
+
+        checkIntent("AA AA AA", "onE3") // Regex.
+
+        checkIntent("A A A", "onE4") // Nested.
+    }
+}