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/09/14 07:24:09 UTC
[incubator-nlpcraft] branch NLPCRAFT-122 updated: WIP.
This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-122
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-122 by this push:
new 27f7778 WIP.
27f7778 is described below
commit 27f7778a748fabf73871b9e1d20178b9ba82fbf7
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Mon Sep 14 00:23:56 2020 -0700
WIP.
---
.../org/apache/nlpcraft/common/NCService.scala | 7 +-
.../apache/nlpcraft/common/ansi/NCAnsiColor.scala | 78 ++++++++++++++++++++++
.../nlpcraft/common/ascii/NCAsciiTable.scala | 65 +++++++++++++-----
.../apache/nlpcraft/common/util/NCIdGenerator.java | 19 +-----
.../org/apache/nlpcraft/common/util/NCUtils.scala | 3 +-
.../nlpcraft/examples/weather/weather_model.json | 2 +
.../org/apache/nlpcraft/probe/NCProbeBoot.scala | 5 +-
.../probe/mgrs/deploy/NCDeployManager.scala | 2 +-
8 files changed, 139 insertions(+), 42 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCService.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCService.scala
index 8d97b1f..f6869b1 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCService.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCService.scala
@@ -20,6 +20,7 @@ package org.apache.nlpcraft.common
import com.typesafe.scalalogging.LazyLogging
import io.opencensus.trace.Span
import org.apache.nlpcraft.common.opencensus.NCOpenCensusTrace
+import org.apache.nlpcraft.common.ansi.NCAnsiColor._
import scala.compat.Platform._
@@ -50,9 +51,9 @@ abstract class NCService extends LazyLogging with NCOpenCensusTrace {
started = true
- val dur = s"[${currentTime - startMs}ms]"
+ val dur = s"$ansiGreenFg[${currentTime - startMs}ms]$ansiReset"
- logger.info(s"+$clsName started $dur")
+ logger.info(s"$clsName started $dur")
addTags(currentSpan(),
"startDurationMs" → (currentTime - startMs), "state" → started
@@ -74,6 +75,6 @@ abstract class NCService extends LazyLogging with NCOpenCensusTrace {
"state" → started
)
- logger.info(s"-$clsName stopped.")
+ logger.info(s"$clsName stopped.")
}
}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
new file mode 100644
index 0000000..7f9ff9e
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiColor.scala
@@ -0,0 +1,78 @@
+/*
+ * 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.common.ansi
+
+import org.apache.nlpcraft.common._
+
+/**
+ * Scala 2.13 shim for `scala.io.AnsiColor`.
+ */
+trait NCAnsiColor {
+ private final val BLACK = "\u001b[30m"
+ private final val RED = "\u001b[31m"
+ private final val GREEN = "\u001b[32m"
+ private final val YELLOW = "\u001b[33m"
+ private final val BLUE = "\u001b[34m"
+ private final val MAGENTA = "\u001b[35m"
+ private final val CYAN = "\u001b[36m"
+ private final val WHITE = "\u001b[37m"
+ private final val BLACK_B = "\u001b[40m"
+ private final val RED_B = "\u001b[41m"
+ private final val GREEN_B = "\u001b[42m"
+ private final val YELLOW_B = "\u001b[43m"
+ private final val BLUE_B = "\u001b[44m"
+ private final val MAGENTA_B = "\u001b[45m"
+ private final val CYAN_B = "\u001b[46m"
+ private final val WHITE_B = "\u001b[47m"
+ private final val RESET = "\u001b[0m"
+ private final val BOLD = "\u001b[1m"
+ private final val UNDERLINED = "\u001b[4m"
+ private final val BLINK = "\u001b[5m"
+ private final val REVERSED = "\u001b[7m"
+ private final val INVISIBLE = "\u001b[8m"
+
+ // Enabled by default.
+ private final val PROP = "NLPCRAFT_ANSI_COLOR_DISABLED"
+
+ private def isEnabled: Boolean = !U.isSysEnvTrue(PROP)
+
+ def ansiBlackFg: String = if (isEnabled) BLACK else ""
+ def ansiBlackBg: String = if (isEnabled) BLACK_B else ""
+ def ansiRedFg: String = if (isEnabled) RED else ""
+ def ansiRedBg: String = if (isEnabled) RED_B else ""
+ def ansiGreenFg: String = if (isEnabled) GREEN else ""
+ def ansiGreenBg: String = if (isEnabled) GREEN_B else ""
+ def ansiYellowFg: String = if (isEnabled) YELLOW else ""
+ def ansiYellowBg: String = if (isEnabled) YELLOW_B else ""
+ def ansiBlueFg: String = if (isEnabled) BLUE else ""
+ def ansiBlueBg: String = if (isEnabled) BLUE_B else ""
+ def ansiMagentaFg: String = if (isEnabled) MAGENTA else ""
+ def ansiMagentaBg: String = if (isEnabled) MAGENTA_B else ""
+ def ansiCyanFg: String = if (isEnabled) CYAN else ""
+ def ansiCyanBg: String = if (isEnabled) CYAN_B else ""
+ def ansiWhiteFg: String = if (isEnabled) WHITE else ""
+ def ansiWhiteBg: String = if (isEnabled) WHITE_B else ""
+ def ansiBold: String = if (isEnabled) BOLD else ""
+ def ansiUnderlined: String = if (isEnabled) UNDERLINED else ""
+ def ansiReset: String = if (isEnabled) RESET else ""
+ def ansiReversed: String = if (isEnabled) REVERSED else ""
+ def ansiBlink: String = if (isEnabled) BLINK else ""
+ def ansiInvisible: String = if (isEnabled) INVISIBLE else ""
+}
+
+object NCAnsiColor extends NCAnsiColor { }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
index 1251b1a..fc62dc9 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
@@ -23,6 +23,7 @@ import com.typesafe.scalalogging.Logger
import org.apache.nlpcraft.common._
import org.apache.nlpcraft.common.ascii.NCAsciiTable._
import resource._
+import org.apache.nlpcraft.common.ansi.NCAnsiColor._
import scala.collection.JavaConverters._
import scala.collection.mutable
@@ -93,7 +94,7 @@ class NCAsciiTable {
// Cell's calculated width including padding.
lazy val width: Int =
if (height > 0)
- style.padding + lines.max(Ordering.by[String, Int](_.length)).length
+ style.padding + lines.max(Ordering.by[String, Int](stripAnsi(_).length)).length
else
style.padding
@@ -112,12 +113,12 @@ class NCAsciiTable {
)
// Table drawing symbols.
- private val HDR_HOR = '='
- private val HDR_VER = '|'
- private val HDR_CRS = '+'
- private val ROW_HOR = '-'
- private val ROW_VER = '|'
- private val ROW_CRS = '+'
+ private val HDR_HOR = s"$ansiCyanFg=$ansiReset"
+ private val HDR_VER = s"$ansiCyanFg|$ansiReset"
+ private val HDR_CRS = s"$ansiCyanFg+$ansiReset"
+ private val ROW_HOR = s"$ansiCyanFg-$ansiReset"
+ private val ROW_VER = s"$ansiCyanFg|$ansiReset"
+ private val ROW_CRS = s"$ansiCyanFg+$ansiReset"
// Headers & rows.
private var hdr = IndexedSeq.empty[Cell]
@@ -154,8 +155,8 @@ class NCAsciiTable {
var defaultHeaderStyle: String = DFLT_HEADER_STYLE
// Dash drawing.
- private def dash(ch: Char, len: Int): String = (for (_ ← 1 to len) yield ch).mkString("")
- private def space(len: Int): String = dash(' ', len)
+ private def dash(ch: String, len: Int): String = ch * len
+ private def space(len: Int): String = " " * len
/**
* Sets table's margin.
@@ -206,6 +207,14 @@ class NCAsciiTable {
}
/**
+ *
+ * @param s
+ * @return
+ */
+ private def stripAnsi(s: String): String =
+ s.replaceAll("\u001B\\[[;\\d]*m", "")
+
+ /**
* Adds row (one or more row cells) with a given style.
*
* @param cells Row cells tuples (style, text). For multi-line cells - use `Seq(...)`.
@@ -315,7 +324,7 @@ class NCAsciiTable {
var start = 0
var lastSpace = -1
var curr = 0
- val len = line.length
+ val len = stripAnsi(line).length
def addLine(line: String) =
if (buf.isEmpty)
@@ -351,13 +360,17 @@ class NCAsciiTable {
/**
*
+ * @param hdr
* @param style
* @param lines
* @return
*/
- private def mkStyledCell(style: String, lines: Any*): Cell = {
+ private def mkStyledCell(hdr: Boolean, style: String, lines: Any*): Cell = {
val st = Style(style)
- val strLines = lines.map(x)
+ var strLines = lines.map(x)
+
+ if (hdr)
+ strLines = strLines.map(s ⇒ s"$ansiGreenFg$s$ansiReset")
Cell(
st,
@@ -374,7 +387,11 @@ class NCAsciiTable {
* @param lines One or more cell lines.
*/
def addHeaderCell(lines: Any*): NCAsciiTable = {
- hdr :+= mkStyledCell(defaultHeaderStyle, lines: _*)
+ hdr :+= mkStyledCell(
+ true,
+ defaultHeaderStyle,
+ lines: _*
+ )
this
}
@@ -385,7 +402,11 @@ class NCAsciiTable {
* @param lines One or more row cells. Multiple lines will be printed on separate lines.
*/
def addRowCell(lines: Any*): NCAsciiTable = {
- curRow :+= mkStyledCell(defaultRowStyle, lines: _*)
+ curRow :+= mkStyledCell(
+ false,
+ defaultRowStyle,
+ lines: _*
+ )
this
}
@@ -397,7 +418,11 @@ class NCAsciiTable {
* @param lines One or more cell lines.
*/
def addStyledHeaderCell(style: String, lines: Any*): NCAsciiTable = {
- hdr :+= mkStyledCell(if (style.trim.isEmpty) defaultHeaderStyle else style, lines: _*)
+ hdr :+= mkStyledCell(
+ hdr = true,
+ if (style.trim.isEmpty) defaultHeaderStyle else style,
+ lines: _*
+ )
this
}
@@ -409,7 +434,11 @@ class NCAsciiTable {
* @param lines One or more row cells. Multiple lines will be printed on separate lines.
*/
def addStyledRowCell(style: String, lines: Any*): NCAsciiTable = {
- curRow :+= mkStyledCell(if (style.trim.isEmpty) defaultRowStyle else style, lines: _*)
+ curRow :+= mkStyledCell(
+ false,
+ if (style.trim.isEmpty) defaultRowStyle else style,
+ lines: _*
+ )
this
}
@@ -421,7 +450,7 @@ class NCAsciiTable {
* @param sty Style.
*/
private def aligned(txt: String, width: Int, sty: Style): String = {
- val d = width - txt.length
+ val d = width - stripAnsi(txt).length
sty.align match {
case "center" ⇒ space(d / 2) + txt + space(d / 2 + d % 2)
@@ -498,7 +527,7 @@ class NCAsciiTable {
* @param cor
* @return
*/
- def mkAsciiLine(crs: Char, cor: Char): String =
+ def mkAsciiLine(crs: String, cor: String): String =
s"${space(margin.left)}$crs${dash(cor, tableW)}$crs${space(margin.right)}\n"
// Print header, if any.
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCIdGenerator.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCIdGenerator.java
index d8578e4..a11ecf1 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCIdGenerator.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCIdGenerator.java
@@ -33,23 +33,8 @@ public class NCIdGenerator {
private String salt = "";
- private ArrayList<Character> seps = new ArrayList<>();
- private ArrayList<Character> guards = new ArrayList<>();
-
- /**
- *
- */
- NCIdGenerator() {
- this("");
- }
-
- /**
- *
- * @param salt Salt seed.
- */
- NCIdGenerator(String salt) {
- this(salt, 0);
- }
+ private final ArrayList<Character> seps = new ArrayList<>();
+ private final ArrayList<Character> guards = new ArrayList<>();
/**
*
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index 226d48e..4a3d21a 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -43,6 +43,7 @@ import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.IOUtils
import org.apache.nlpcraft.common._
+import org.apache.nlpcraft.common.ansi.NCAnsiColor._
import org.apache.nlpcraft.common.blowfish.NCBlowfishHasher
import resource._
@@ -1344,7 +1345,7 @@ object NCUtils extends LazyLogging {
if (msg != null) {
x.getLocalizedMessage.split("\n").foreach(line ⇒ {
- logger.error(s"${" " * indent}${if (first) "+-" else " "}${line.trim}")
+ logger.error(s"${" " * indent}${if (first) s"$ansiRedFg+-$ansiReset" else " "}${line.trim}")
first = false
})
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
index b5d6577..d6fcec1 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/weather/weather_model.json
@@ -38,6 +38,7 @@
"indicator"
],
"synonyms": [
+ "A",
"{history|past|previous}"
]
},
@@ -48,6 +49,7 @@
"indicator"
],
"synonyms": [
+ "A",
"{future|forecast|prognosis|prediction}"
]
}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
index 02eadbe..4231712 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/NCProbeBoot.scala
@@ -47,6 +47,7 @@ import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.sort.NCSortEnricher
import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.stopword.NCStopWordEnricher
import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.suspicious.NCSuspiciousNounsEnricher
import org.apache.nlpcraft.probe.mgrs.nlp.validate.NCValidateManager
+import org.apache.nlpcraft.common.ansi.NCAnsiColor._
import scala.collection.JavaConverters._
import scala.collection.mutable
@@ -354,7 +355,7 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
println(s)
}
-
+
/**
*
*/
@@ -363,7 +364,7 @@ private [probe] object NCProbeBoot extends LazyLogging with NCOpenCensusTrace {
val ver = NCVersion.getCurrent
- tbl += ("Probe ID", cfg.id)
+ tbl += ("Probe ID", s"$ansiBlackFg$ansiBlueBg${cfg.id}$ansiReset")
tbl += ("Probe Token", cfg.token)
tbl += ("API Version", ver.version + ", " + ver.date.toString)
tbl += ("Down-Link", cfg.downLinkString)
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 3501691..fe91b8f 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
@@ -438,7 +438,7 @@ object NCDeployManager extends NCService with DecorateAsScala {
row._3
))
- logger.warn(s"\nDup synonyms in '$mdlId' model:\n${tbl.toString}")
+ logger.warn(s"Dup synonyms in '$mdlId' model:\n${tbl.toString}")
if (mdl.isDupSynonymsAllowed)
logger.warn(s"NOTE: '$mdlId' model allows dup synonyms but the large number may degrade the performance.")