This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-472
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-472 by this push:
new 4c64b9a WIP
4c64b9a is described below
commit 4c64b9a02035684bf920838b01f08732cbaa1102
Author: Aaron Radzinski <[email protected]>
AuthorDate: Thu Jan 13 08:40:37 2022 -0800
WIP
---
.../nlpcraft/internal/NCPipelineProcessor.scala | 12 +-
.../apache/nlpcraft/internal/NCSentenceHelper.java | 51 ++++++++
.../org/apache/nlpcraft/internal/ansi/NCAnsi.scala | 40 -------
.../nlpcraft/internal/ansi/NCAnsiProgressBar.scala | 129 ---------------------
.../nlpcraft/internal/ansi/NCAnsiSpinner.scala | 112 ------------------
.../nlpcraft/internal/antlr4/NCCompilerUtils.scala | 6 +-
.../nlpcraft/internal/ascii/NCAsciiTable.scala | 8 +-
.../nlpcraft/internal/version/NCVersion.scala | 2 +-
8 files changed, 62 insertions(+), 298 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCPipelineProcessor.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCPipelineProcessor.scala
index e29cbe1..b34a401 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCPipelineProcessor.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCPipelineProcessor.scala
@@ -54,9 +54,9 @@ class NCPipelineProcessor(mdl: NCModel) extends LazyLogging :
private val tokEnrichers = nvl(pipeline.getTokenEnrichers)
private val entEnrichers = nvl(pipeline.getEntityEnrichers)
private val entParsers = nvl(pipeline.getEntityParsers)
- private val tokenValidators = nvl(pipeline.getTokenValidators)
- private val entityValidators = nvl(pipeline.getEntityValidators)
- private val variantValidators = nvl(pipeline.getVariantValidators)
+ private val tokVals = nvl(pipeline.getTokenValidators)
+ private val entVals = nvl(pipeline.getEntityValidators)
+ private val varVals = nvl(pipeline.getVariantValidators)
/**
*
@@ -103,7 +103,7 @@ class NCPipelineProcessor(mdl: NCModel) extends LazyLogging
:
check()
e.enrich(req, cfg, toks)
- for (v <- tokenValidators)
+ for (v <- tokVals)
check()
v.validate(req, cfg, toks)
@@ -123,7 +123,7 @@ class NCPipelineProcessor(mdl: NCModel) extends LazyLogging
:
for (e <- entEnrichers)
check()
e.enrich(req, cfg, entsList)
- for (v <- entityValidators)
+ for (v <- entVals)
check()
v.validate(req, cfg, entsList)
@@ -151,7 +151,7 @@ class NCPipelineProcessor(mdl: NCModel) extends LazyLogging
:
else
Seq(mkVariant(entities)).asJava
- for (v <- variantValidators)
+ for (v <- varVals)
check()
variants = v.filter(req, cfg, variants)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCSentenceHelper.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCSentenceHelper.java
index fd02253..eb7c09c 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCSentenceHelper.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/NCSentenceHelper.java
@@ -39,6 +39,13 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
private final long[] wordBits;
private final int[] wordCounts;
+ /**
+ *
+ * @param lo
+ * @param hi
+ * @param wordBits
+ * @param wordCounts
+ */
private NCSentenceHelper(long lo, long hi, long[] wordBits, int[]
wordCounts) {
this.lo = lo;
this.hi = hi;
@@ -46,6 +53,10 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
this.wordCounts = wordCounts;
}
+ /**
+ *
+ * @return
+ */
private List<Long> computeLocal() {
List<Long> res = new ArrayList<>();
@@ -72,6 +83,10 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
return res;
}
+ /**
+ *
+ * @return
+ */
private List<Long> forkJoin() {
long mid = lo + hi >>> 1L;
@@ -83,6 +98,12 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
return merge(t1.compute(), t2.join());
}
+ /**
+ *
+ * @param l1
+ * @param l2
+ * @return
+ */
private static List<Long> merge(List<Long> l1, List<Long> l2) {
if (l1.isEmpty())
return l2;
@@ -126,6 +147,12 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
return res;
}
+ /**
+ *
+ * @param bits
+ * @param allBits
+ * @return
+ */
private static boolean excludes(long bits, List<Long> allBits) {
for (Long allBit : allBits)
if (containsAllBits(bits, allBit))
@@ -134,10 +161,23 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
return true;
}
+ /**
+ *
+ * @param bitSet1
+ * @param bitSet2
+ * @return
+ */
private static boolean containsAllBits(long bitSet1, long bitSet2) {
return (bitSet1 & bitSet2) == bitSet2;
}
+ /**
+ *
+ * @param words
+ * @param dict
+ * @param <T>
+ * @return
+ */
private static <T> long wordsToBits(Set<T> words, List<T> dict) {
long bits = 0;
@@ -148,6 +188,13 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
return bits;
}
+ /**
+ *
+ * @param bits
+ * @param dict
+ * @param <T>
+ * @return
+ */
private static <T> List<T> bitsToWords(long bits, List<T> dict) {
List<T> words = new ArrayList<>(Long.bitCount(bits));
@@ -158,6 +205,10 @@ class NCSentenceHelper extends RecursiveTask<List<Long>> {
return words;
}
+ /**
+ *
+ * @return
+ */
@Override
protected List<Long> compute() {
return hi - lo <= THRESHOLD ? computeLocal() : forkJoin();
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsi.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsi.scala
index 16f8725..53cc52d 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsi.scala
@@ -63,26 +63,6 @@ sealed trait NCAnsi extends LazyLogging:
private final val REVERSED = s"${CSI}7m"
private final val INVISIBLE = s"${CSI}8m"
- // Erase functions.
- private final val CLEAR_SCREEN = s"${CSI}J"
- private final val CLEAR_SCREEN_AFTER = s"${CSI}0J"
- private final val CLEAR_SCREEN_BEFORE = s"${CSI}1J"
- private final val CLEAR_LINE = s"${CSI}K"
- private final val CLEAR_LINE_AFTER = s"${CSI}0K"
- private final val CLEAR_LINE_BEFORE = s"${CSI}1K"
-
- // Cursor moves.
- private final val CURSOR_UP = s"${CSI}1A"
- private final val CURSOR_DOWN = s"${CSI}1B"
- private final val CURSOR_LEFT = s"${CSI}1D"
- private final val CURSOR_RIGHT = s"${CSI}1C"
- private final val CURSOR_POS_SAVE= s"${CSI}s"
- private final val CURSOR_POS_RESTORE = s"${CSI}u"
- private final val CURSOR_LINE_HOME = s"${CSI}0G"
- private final val CURSOR_SCREEN_HOME = s"${CSI}H"
- private final val CURSOR_HIDE = s"$CSI?25l"
- private final val CURSOR_SHOW = s"$CSI?25h"
-
def isEnabled: Boolean = !NCUtils.isSysEnvTrue(PROP)
@@ -184,26 +164,6 @@ sealed trait NCAnsi extends LazyLogging:
def ansiMagenta(s: Any): String = s"$ansiMagentaFg${s.toString}$ansiReset"
def ansiBold(s: Any): String = s"$ansiBold${s.toString}$ansiReset"
- // Erase functions.
- def ansiClearScreen: String = if isEnabled then CLEAR_SCREEN else ""
- def ansiClearScreenAfter: String = if isEnabled then CLEAR_SCREEN_AFTER
else ""
- def ansiClearScreenBefore: String = if isEnabled then CLEAR_SCREEN_BEFORE
else ""
- def ansiClearLine: String = if isEnabled then CLEAR_LINE else ""
- def ansiClearLineAfter: String = if isEnabled then CLEAR_LINE_AFTER else ""
- def ansiClearLineBefore: String = if isEnabled then CLEAR_LINE_BEFORE else
""
-
- // Cursor movement functions.
- def ansiCursorUp: String = if isEnabled then CURSOR_UP else ""
- def ansiCursorDown: String = if isEnabled then CURSOR_DOWN else ""
- def ansiCursorLeft: String = if isEnabled then CURSOR_LEFT else ""
- def ansiCursorRight: String = if isEnabled then CURSOR_RIGHT else ""
- def ansiCursorLineHome: String = if isEnabled then CURSOR_LINE_HOME else ""
- def ansiCursorScreenHome: String = if isEnabled then CURSOR_SCREEN_HOME
else ""
- def ansiCursorPosSave: String = if isEnabled then CURSOR_POS_SAVE else ""
- def ansiCursorPosRestore: String = if isEnabled then CURSOR_POS_RESTORE
else ""
- def ansiCursorShow: String = if isEnabled then CURSOR_SHOW else ""
- def ansiCursorHide: String = if isEnabled then CURSOR_HIDE else ""
-
/**
*
*/
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsiProgressBar.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsiProgressBar.scala
deleted file mode 100644
index 794b8ab..0000000
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsiProgressBar.scala
+++ /dev/null
@@ -1,129 +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.internal.ansi
-
-import java.io.PrintWriter
-import org.apache.nlpcraft.internal.*
-import NCAnsi.*
-import org.apache.commons.lang3.StringUtils
-import NCAnsiProgressBar.*
-
-/**
- * Forward-only, bound ANSI-based progress bar.
- *
- * @param out
- * @param totalTicks Number of ticks to complete.
- * @param dispSize Visual size of the progress bar.
- * @param clearOnComplete
- * @param useAnsi
- */
-class NCAnsiProgressBar(
- out: PrintWriter,
- totalTicks: Int,
- dispSize: Int,
- clearOnComplete: Boolean = true,
- useAnsi: Boolean = true):
- require(dispSize <= totalTicks)
-
- @volatile private var tick = 0
-
- private final val mux = new Object()
- private final val PB_LEFT = s"$B${CHAR_SET.head}$RST"
- private final val PB_RIGHT = s"$B${CHAR_SET(3)}$RST"
- private final val PB_EMPTY = s"$W${CHAR_SET(2)}$RST"
- private final val PB_FULL = s"$R$BO${CHAR_SET(1)}$RST"
- private final val PB_LEAD = s"$Y$BO${CHAR_SET(4)}$RST"
-
- /**
- *
- */
- private def clean(): Unit =
- out.print(ansiCursorLeft * (dispSize + 2/* Left & right brackets. */ +
5/* % string. */))
- out.print(ansiClearLineAfter)
- out.flush()
-
- /**
- * Starts progress bar.
- */
- def start(): Unit =
- tick = 0
- if useAnsi then
- mux.synchronized {
- // Hide cursor to avoid blinking.
- out.print(ansiCursorHide)
- out.print(PB_LEFT)
- out.print(PB_EMPTY * dispSize)
- out.print(PB_RIGHT)
- out.print(" ")
- out.print(s"${W}0% $RST")
- out.flush()
- }
-
- /**
- * Ticks progress bar one tick at a time.
- */
- def ticked(): Unit =
- mux.synchronized {
- tick += 1
-
- if useAnsi then
- clean()
- val ratio = tick.toFloat / totalTicks.toFloat
- val bar = if tick == 1 then 1 else Math.round(ratio * dispSize)
- val pct = Math.round(ratio * 100)
- out.print(PB_LEFT)
- for (i <- 0 until dispSize)
- if i < bar then out.print(PB_FULL)
- else if i == bar then out.print(PB_LEAD)
- else out.print(PB_EMPTY)
-
- out.print(PB_RIGHT)
- out.print(" ")
- out.print(W + StringUtils.rightPad(s"$pct%",4) + RST)
- out.flush()
- else if tick == 1 || tick % (totalTicks / dispSize) == 0 then
- out.print(NON_ANSI_CHAR)
- out.flush()
- }
-
- /**
- * Whether progress is complete.
- *
- * @return
- */
- def completed: Boolean = tick == totalTicks
-
- /**
- * Stops progress bar.
- */
- def stop(): Unit =
- if useAnsi && clearOnComplete then mux.synchronized {
- clean()
-
- // Show cursor.
- out.print(ansiCursorShow)
- out.flush()
- }
-
-/**
- *
- */
-object NCAnsiProgressBar:
- // Active charset to use.
- private final val NON_ANSI_CHAR = '='
- private val CHAR_SET = Seq('[', '=', '.', ']', '>')
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsiSpinner.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsiSpinner.scala
deleted file mode 100644
index 9b0878e..0000000
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ansi/NCAnsiSpinner.scala
+++ /dev/null
@@ -1,112 +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.internal.ansi
-
-import java.io.PrintWriter
-import NCAnsi.*
-import org.apache.nlpcraft.internal.*
-import NCAnsiSpinner.*
-import org.apache.nlpcraft.internal.util.NCUtils
-
-/**
- * ANSI-based hourglass spinner.
- *
- * @param out
- * @param useAnsi
- */
-class NCAnsiSpinner(out: PrintWriter, useAnsi: Boolean = true):
- @volatile private var thread: Thread = _
- @volatile private var suffix = ""
- @volatile private var prefix = ""
- @volatile private var lastLength = 0
- @volatile private var frame = 0
-
- private final val mux = new Object()
-
- /**
- *
- * @param p
- */
- def setSuffix(p: String): Unit =
- this.suffix = if p == null then "" else p
-
- /**
- *
- * @param p
- */
- def setPrefix(p: String): Unit =
- this.prefix = if p == null then "" else p
-
- /**
- *
- */
- private def clean(): Unit =
- out.print(ansiCursorLeft * lastLength)
- out.print(ansiClearLineAfter)
- out.flush()
-
- /**
- * Starts spinner.
- */
- def start(): Unit =
- if useAnsi then
- thread = NCUtils.mkThread("ansi-spinner") { t =>
- frame = 0
- lastLength = 0
- // Hide cursor to avoid blinking.
- out.print(ansiCursorHide)
- out.flush()
- while (!t.isInterrupted)
- mux.synchronized {
- if frame > 0 then clean()
- out.print(s"$prefix$ansiCyanFg${CHAR_SET(frame %
CHAR_SET.size)}$ansiReset$suffix")
- out.flush()
- }
- lastLength = NCUtils.stripAnsi(prefix).length + 1 +
NCUtils.stripAnsi(suffix).length
- frame += 1
- Thread.sleep(1000 / CHAR_SET.size) // Full rotation per
second.
- }
-
- thread.start()
- else
- mux.synchronized {
- out.print("... ")
- out.flush()
- }
-
- /**
- * Stops spinner.
- */
- def stop(): Unit =
- NCUtils.stopThread(thread)
-
- if useAnsi && frame > 0 then
- mux.synchronized {
- clean()
-
- // Show cursor.
- out.print(ansiCursorShow)
- out.flush()
- }
-
-/**
- *
- */
-object NCAnsiSpinner:
- // An active charset to use.
- private final val CHAR_SET = Seq('-', '\\', '|', '/')
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/antlr4/NCCompilerUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/antlr4/NCCompilerUtils.scala
index e89176e..909d2f5 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/antlr4/NCCompilerUtils.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/antlr4/NCCompilerUtils.scala
@@ -39,10 +39,8 @@ object NCCompilerUtils:
val dash = "-" * in0.length
var ptrStr = dash.substring(0, pos) + r("^")
- if pos < dash.length - 1 then
- ptrStr = ptrStr + y("~") + y(dash.substring(pos + 2))
- else
- ptrStr = ptrStr + y(dash.substring(pos + 1))
+ if pos < dash.length - 1 then ptrStr = ptrStr + y("~") +
y(dash.substring(pos + 2))
+ else ptrStr = ptrStr + y(dash.substring(pos + 1))
val origStr = in0.substring(0, pos) + r(in0.charAt(pos)) +
y(in0.substring(pos + 1))
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ascii/NCAsciiTable.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ascii/NCAsciiTable.scala
index c510651..9a1d961 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ascii/NCAsciiTable.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ascii/NCAsciiTable.scala
@@ -591,12 +591,8 @@ class NCAsciiTable:
def render(file: java.io.File): Unit = renderPrintStream(new
PrintStream(file), file.getAbsolutePath)
private def renderPrintStream(f: => PrintStream, file: String): Unit =
- try
- Using.resource(f) { ps =>
- ps.print(mkString())
- }
- catch
- case e: IOException => throw new NCException(s"Error outputting
table into file: $file", e)
+ try Using.resource(f)(_.print(mkString()))
+ catch case e: IOException => throw new NCException(s"Error outputting
table into file: $file", e)
/**
* Static context.
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/version/NCVersion.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/version/NCVersion.scala
index adee72a..93b93ce 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/version/NCVersion.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/version/NCVersion.scala
@@ -56,7 +56,7 @@ object NCVersion extends LazyLogging:
Version("0.9.0", LocalDate.of(2021, 7, 10)),
// Version '1.0.0+' is incompatible with previous versions.
- Version("1.0.0", LocalDate.of(2022, 3, 1)),
+ Version("1.0.0", LocalDate.of(2022, 4, 16)),
).sortBy(_.version)
// +=================================================+
// | UPDATE THIS SEQUENCE FOR EACH RELEASE MANUALLY. |