You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2020/12/22 03:28:55 UTC

[incubator-nlpcraft] branch NLPCRAFT-203 updated: Update NCIntentSolverEngine.scala

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

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


The following commit(s) were added to refs/heads/NLPCRAFT-203 by this push:
     new dc2b5ed  Update NCIntentSolverEngine.scala
dc2b5ed is described below

commit dc2b5edc86c7b9f802855901b498a3b4c908b08d
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Mon Dec 21 19:28:44 2020 -0800

    Update NCIntentSolverEngine.scala
---
 .../model/intent/impl/NCIntentSolverEngine.scala   | 95 ++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentSolverEngine.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentSolverEngine.scala
index d7c6af8..558fd6d 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentSolverEngine.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentSolverEngine.scala
@@ -36,6 +36,101 @@ import scala.collection.mutable
   * Intent solver that finds the best matching intent given user sentence.
   */
 object NCIntentSolverEngine extends LazyLogging with NCOpenCensusTrace {
+    /**
+     * NOTE: not thread-safe.
+     */
+    private class Weight2 extends Ordered[Weight2] {
+        private var buf = mutable.ArrayBuffer[Int]()
+
+        /**
+         *
+         * @param ws
+         */
+        def this(ws: Int*) = {
+            this()
+
+            buf.appendAll(ws)
+        }
+
+        /**
+         * Adds given weight to this weight.
+         *
+         * @param that Weight to add.
+         * @return
+         */
+        def ++=(that: Weight2): Weight2 = {
+            val buf2 = mutable.ArrayBuffer[Int]()
+
+            for (i ← 0 until Math.max(buf.size, that.buf.size)) {
+                val (w, w2) = get(i, that)
+
+                buf.append(w + w2)
+            }
+
+            buf = buf2
+
+            this
+        }
+
+        /**
+         * Appends new weight.
+         *
+         * @param w New weight to append.
+         * @return
+         */
+        def += (w: Int): Weight2 = {
+            buf.append(w)
+
+            this
+        }
+
+        /**
+         * Sets specific weight at a given index.
+         *
+         * @param idx
+         * @param w
+         */
+        def setWeight(idx: Int, w: Int): Unit =
+            buf(idx) = w
+
+        /**
+         *
+         * @param i
+         * @param that
+         * @return
+         */
+        private def get(i: Int, that: Weight2): (Int, Int) =
+        (
+            if (i < buf.size) buf(i) else 0,
+            if (i < that.buf.size) that.buf(i) else 0
+        )
+
+        /**
+         *
+         * @param that
+         * @return
+         */
+        override def compare(that: Weight2): Int = {
+            var res = 0
+
+            for (i ← 0 until Math.max(buf.size, that.buf.size) if res == 0) {
+                val (w, w2) = get(i, that)
+
+                res = Integer.compare(w, w2)
+            }
+
+            res
+        }
+
+        /**
+         *
+         * @return
+         */
+        def get: Seq[Int] = buf.toSeq
+
+        override def toString: String = s"Weight (${buf.mkString(", ")})"
+    }
+
     private class Weight extends Ordered[Weight] {
         private val weights: Array[Int] = new Array(3)