This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-520
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-520 by this push:
new a41bed6b WIP.
a41bed6b is described below
commit a41bed6b0365da88be96ce2f266bcf6277dfb06b
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Dec 16 21:42:36 2022 +0400
WIP.
---
.../apache/nlpcraft/internal/util/NCUtils.scala | 189 +--------------------
1 file changed, 4 insertions(+), 185 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
index 594fa050..818aa25d 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/util/NCUtils.scala
@@ -41,9 +41,6 @@ import scala.util.Using
*
*/
object NCUtils extends LazyLogging:
- final val NL = System getProperty "line.separator"
- private val RND = new Random()
- private final val UTC = ZoneId.of("UTC")
private val sysProps = new SystemProperties
private final lazy val GSON = new
GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()
@@ -54,116 +51,6 @@ object NCUtils extends LazyLogging:
*/
def sysEnv(s: String): Option[String] =
sysProps.get(s).orElse(sys.env.get(s))
- /**
- * Tests whether given system property of environment variable is set or
not.
- *
- * @param s @param s Name of the system property or environment variable.
- */
- def isSysEnvSet(s: String): Boolean = sysProps.get(s).nonEmpty ||
sys.env.contains(s)
-
- /**
- * Returns `true` if given system property, or environment variable is
provided and has value
- * 'true'. In all other cases returns `false`.
- *
- * @param s Name of the system property or environment variable.
- */
- def isSysEnvTrue(s: String): Boolean = sysEnv(s) match
- case None => false
- case Some(v) => java.lang.Boolean.valueOf(v) == java.lang.Boolean.TRUE
-
- /**
- * Gets random value from given sequence.
- *
- * @param seq Sequence.
- */
- def getRandom[T](seq: Seq[T]): T = seq(RND.nextInt(seq.size))
-
- /**
- * Makes random filled sequence with given length from initial.
- *
- * @param seq Initial sequence.
- * @param n Required sequence length.
- */
- def getRandomSeq[T](seq: Seq[T], n: Int): Seq[T] =
- require(seq.lengthCompare(n) >= 0)
-
- val src = mutable.ArrayBuffer.empty[T] ++ seq
- val dest = mutable.ArrayBuffer.empty[T]
-
- (0 until n).foreach(_ => dest += src.remove(RND.nextInt(src.size)))
-
- dest.toSeq
-
- /**
- * Prints ASCII-logo.
- */
- def asciiLogo(): String =
- Seq(
- raw" _ ____ ______ ______ $NL",
- raw" / | / / /___ / ____/________ _/ __/ /_ $NL",
- raw" / |/ / / __ \/ / / ___/ __ `/ /_/ __/ $NL",
- raw" / /| / / /_/ / /___/ / / /_/ / __/ /_ $NL",
- raw"/_/ |_/_/ .___/\____/_/ \__,_/_/ \__/ $NL",
- raw" /_/ $NL"
- )
- .mkString("")
-
- /**
- *
- * @param json
- */
- def prettyJson(json: String): String =
- if json == null || json.isEmpty then ""
- else
- try
-
GSON.toJson(GSON.getAdapter(classOf[JsonElement]).fromJson(json))
- // Fix the problem with escaping '<' and '>' which is only
- // a theoretical problem for browsers displaying JSON.
- .replace("\\u003c", "<")
- .replace("\\u003e", ">")
- catch case _: Exception => ""
-
- /**
- *
- * @param json
- */
- def isValidJson(json: String): Boolean =
-
scala.util.Try(GSON.getAdapter(classOf[JsonElement]).fromJson(json)).isSuccess
-
- /**
- *
- * @param json
- * @param field
- */
- @throws[Exception]
- def getJsonStringField(json: String, field: String): String =
-
GSON.getAdapter(classOf[JsonElement]).fromJson(json).getAsJsonObject.get(field).getAsString
-
- /**
- *
- * @param json
- * @param field
- */
- @throws[Exception]
- def getJsonIntField(json: String, field: String): Int =
-
GSON.getAdapter(classOf[JsonElement]).fromJson(json).getAsJsonObject.get(field).getAsInt
-
- /**
- *
- * @param json
- * @tparam T
- */
- def jsonToObject[T](json: String, typ: java.lang.reflect.Type): T =
- GSON.fromJson(json, typ)
-
- /**
- *
- * @param json
- * @tparam T
- */
- def jsonToObject[T](json: String, cls: Class[T]): T =
- GSON.fromJson(json, cls)
-
/**
* Shortcut to convert given JSON to Scala map with default mapping.
*
@@ -182,20 +69,6 @@ object NCUtils extends LazyLogging:
try GSON.fromJson(json, classOf[java.util.HashMap[String, Object]])
catch case e: Exception => E(s"Cannot deserialize JSON to map:
'$json'", e)
- /**
- *
- * @param json
- * @param field
- */
- def getJsonBooleanField(json: String, field: String): Boolean =
- try
GSON.getAdapter(classOf[JsonElement]).fromJson(json).getAsJsonObject.get(field).getAsBoolean
- catch case e: Exception => E(s"Cannot extract JSON field '$field'
from: '$json'", e)
-
- /**
- * Gets now in UTC timezone.
- */
- def nowUtc(): ZonedDateTime = ZonedDateTime.now(UTC)
-
/**
* Gets now in UTC timezone in milliseconds representation.
*/
@@ -206,21 +79,12 @@ object NCUtils extends LazyLogging:
*/
def now(): Long = System.currentTimeMillis()
- /**
- *
- * @param v
- * @param dflt
- * @tparam T
- */
- def notNull[T <: AnyRef](v: T, dflt: T): T = if v == null then dflt else v
-
/**
* Trims each sequence string and filters out empty ones.
*
* @param s String to process.
*/
- def trimFilter(s: Seq[String]): Seq[String] =
- s.map(_.strip).filter(_.nonEmpty)
+ private def trimFilter(s: Seq[String]): Seq[String] =
s.map(_.strip).filter(_.nonEmpty)
/**
* Splits, trims and filters empty strings for the given string.
@@ -250,7 +114,7 @@ object NCUtils extends LazyLogging:
* @param s
*/
@tailrec
- def trimEscapesQuotes(s: String): String =
+ private def trimEscapesQuotes(s: String): String =
val z = s.strip
if z.nonEmpty then
if z.head == '\'' && z.last == '\'' then
@@ -279,37 +143,6 @@ object NCUtils extends LazyLogging:
else
s
- /**
- * Escapes given string for JSON according to RFC 4627
http://www.ietf.org/rfc/rfc4627.txt.
- *
- * @param s String to escape.
- * @return Escaped string.
- */
- private def escapeJson(s: String): String = // TODO: remove?
- val len = s.length
- if len == 0 then
- ""
- else
- val sb = new mutable.StringBuilder
- for (ch <- s.toCharArray)
- ch match
- case '\\' | '"' => sb += '\\' += ch
- case '/' => sb += '\\' += ch
- case '\b' => sb ++= "\\b"
- case '\t' => sb ++= "\\t"
- case '\n' => sb ++= "\\n"
- case '\f' => sb ++= "\\f"
- case '\r' => sb ++= "\\r"
- case _ =>
- if ch < ' ' then
- val t = s"000${Integer.toHexString(ch)}"
- sb ++= "\\u" ++= t.substring(t.length - 4)
-
- else
- sb += ch
-
- sb.toString()
-
/**
* Makes thread.
*
@@ -414,13 +247,6 @@ object NCUtils extends LazyLogging:
try t.join()
catch case _: InterruptedException => logger.trace("Thread joining
was interrupted (ignoring).")
- /**
- * Interrupts thread.
- *
- * @param t Thread.
- */
- def interruptThread(t: Thread): Unit = if t != null then t.interrupt()
-
/**
* Checks duplicated elements in collection.
*
@@ -576,7 +402,7 @@ object NCUtils extends LazyLogging:
* @param f File.
* @param log Logger.
*/
- def readFileBytes(f: File, log: Logger = logger): Array[Byte] =
+ private def readFileBytes(f: File, log: Logger = logger): Array[Byte] =
try
val arr = new Array[Byte](f.length().toInt)
Using.resource(new FileInputStream(f))(_.read(arr))
@@ -599,7 +425,7 @@ object NCUtils extends LazyLogging:
* @param f File.
* @param log Logger.
*/
- def gzipFile(f: File, log: Logger = logger): Unit =
+ private def gzipFile(f: File, log: Logger = logger): Unit =
val gz = s"${f.getAbsolutePath}.gz"
// Do not user BOS here - it makes files corrupted.
@@ -680,13 +506,6 @@ object NCUtils extends LazyLogging:
log: Logger = logger
): Iterator[String] = readLines(new GZIPInputStream(getStream(res)), enc,
strip, convert, filterText, log)
- /**
- *
- * @param s
- * @return
- */
- def hasMeaning(s: String): Boolean = s.nonEmpty && s.head != '#'
-
/**
*
* @param bodies