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 2021/03/10 00:38:00 UTC

[incubator-nlpcraft] 17/17: Code review.

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

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

commit 9c70b97c31367b4ce8730ba2a4a99aa55b82f5cd
Author: Aaron Radzinski <ar...@apache.org>
AuthorDate: Tue Mar 9 09:49:17 2021 -0800

    Code review.
---
 .../{NCComboHelper.java => NCSentenceHelper.java}  | 29 +++++++++++-----------
 .../probe/mgrs/sentence/NCSentenceManager.scala    |  2 +-
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCComboHelper.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceHelper.java
similarity index 87%
rename from nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCComboHelper.java
rename to nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceHelper.java
index 1b83701..1e215ad 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCComboHelper.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceHelper.java
@@ -31,7 +31,7 @@ import static java.util.stream.Collectors.toList;
 /**
  * It is not converted to scala because scala and java long values implicit conversion performance problems.
  */
-class NCComboHelper extends RecursiveTask<List<Long>> {
+class NCSentenceHelper extends RecursiveTask<List<Long>> {
     private static final long THRESHOLD = (long)Math.pow(2, 20);
 
     private final long lo;
@@ -39,7 +39,7 @@ class NCComboHelper extends RecursiveTask<List<Long>> {
     private final long[] wordBits;
     private final int[] wordCounts;
 
-    private NCComboHelper(long lo, long hi, long[] wordBits, int[] wordCounts) {
+    private NCSentenceHelper(long lo, long hi, long[] wordBits, int[] wordCounts) {
         this.lo = lo;
         this.hi = hi;
         this.wordBits = wordBits;
@@ -65,7 +65,7 @@ class NCComboHelper extends RecursiveTask<List<Long>> {
                 }
             }
 
-            if (match && !includes(comboBits, res))
+            if (match && excludes(comboBits, res))
                 res.add(comboBits);
         }
 
@@ -75,8 +75,8 @@ class NCComboHelper extends RecursiveTask<List<Long>> {
     private List<Long> forkJoin() {
         long mid = lo + hi >>> 1L;
 
-        NCComboHelper t1 = new NCComboHelper(lo, mid, wordBits, wordCounts);
-        NCComboHelper t2 = new NCComboHelper(mid, hi, wordBits, wordCounts);
+        NCSentenceHelper t1 = new NCSentenceHelper(lo, mid, wordBits, wordCounts);
+        NCSentenceHelper t2 = new NCSentenceHelper(mid, hi, wordBits, wordCounts);
 
         t2.fork();
 
@@ -97,7 +97,7 @@ class NCComboHelper extends RecursiveTask<List<Long>> {
             List<Long> res = size1 == 1 ? l2 : l1;
             Long val = size1 == 1 ? l1.get(0) : l2.get(0);
 
-            if (!includes(val, res))
+            if (excludes(val, res))
                 res.add(val);
 
             return res;
@@ -116,23 +116,22 @@ class NCComboHelper extends RecursiveTask<List<Long>> {
                     v2 = null;
             }
 
-            if (v1 != null && !includes(v1, res))
+            if (v1 != null && excludes(v1, res))
                 res.add(v1);
 
-            if (v2 != null && !includes(v2, res))
+            if (v2 != null && excludes(v2, res))
                 res.add(v2);
         }
 
         return res;
     }
 
-    private static boolean includes(long bits, List<Long> allBits) {
-        for (int i = 0, n = allBits.size(); i < n; i++) {
-            if (containsAllBits(bits, allBits.get(i)))
-                return true;
-        }
+    private static boolean excludes(long bits, List<Long> allBits) {
+        for (Long allBit : allBits)
+            if (containsAllBits(bits, allBit))
+                return false;
 
-        return false;
+        return true;
     }
 
     private static boolean containsAllBits(long bitSet1, long bitSet2) {
@@ -194,7 +193,7 @@ class NCComboHelper extends RecursiveTask<List<Long>> {
 
         // Prepare Fork/Join task to iterate over the power set of all combinations.
         return
-            pool.invoke(new NCComboHelper(1, (long)Math.pow(2, dict.size()), wordBits, wordCounts)).
+            pool.invoke(new NCSentenceHelper(1, (long)Math.pow(2, dict.size()), wordBits, wordCounts)).
                 stream().map(bits -> bitsToWords(bits, dict)).collect(toList());
     }
 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala
index 9227859..470776c 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala
@@ -681,7 +681,7 @@ object NCSentenceManager extends NCService {
                         toSeq.sortBy(-_.size)
 
                 val sens =
-                    NCComboHelper.findCombinations(toksByIdx.map(_.asJava).asJava, pool).asScala.map(_.asScala).
+                    NCSentenceHelper.findCombinations(toksByIdx.map(_.asJava).asJava, pool).asScala.map(_.asScala).
                         flatMap(delComb ⇒ {
                             val nsClone = sen.clone()