This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-474
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-474 by this push:
new cf82c7e WIP
cf82c7e is described below
commit cf82c7e775858037a7ac3fa691e7be6434a2059f
Author: Aaron Radzinski <[email protected]>
AuthorDate: Fri Jan 21 11:20:02 2022 -0800
WIP
---
.../main/scala/org/apache/nlpcraft/NCGlobals.scala | 112 ++++++++++++++++++++-
.../nlpcraft/internal/intent/NCIDLIntent.scala | 59 +++++++++++
.../internal/intent/NCIDLIntentOptions.scala | 44 ++++++++
.../nlpcraft/internal/intent/NCIDLTerm.scala | 66 ++++++++++++
4 files changed, 280 insertions(+), 1 deletion(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCGlobals.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCGlobals.scala
index 8e29546..50e26c0 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCGlobals.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCGlobals.scala
@@ -17,7 +17,8 @@
package org.apache.nlpcraft
-import org.apache.nlpcraft.NCException
+import org.apache.nlpcraft.*
+import internal.ansi.NCAnsi.*
/**
* Global syntax sugar for throwing [[NCException]].
@@ -26,3 +27,112 @@ import org.apache.nlpcraft.NCException
* @param cause Optional cause.
*/
def E[T](msg: String, cause: Throwable = null): T = throw new NCException(msg,
cause)
+
+// Internal deep debug flag (more verbose tracing).
+final val DEEP_DEBUG = false
+
+// Re-route to 8-bit colors.
+def G: String = ansi256Fg(34)
+def M: String = ansi256Fg(177)
+def R: String = ansi256Fg(202)
+
+def C: String = ansiCyanFg
+def Y: String = ansiYellowFg
+def W: String = ansi256Fg(231)
+def B: String = ansiBlueFg
+def K: String = ansiBlackFg
+
+def GB: String = ansi256Bg(34)
+def MB: String = ansi256Bg(177)
+def RB: String = ansi256Bg(202)
+def CB: String = ansiCyanBg
+def YB: String = ansiYellowBg
+def WB: String = ansiWhiteBg
+def BB: String = ansiBlueBg
+def KB: String = ansiBlackBg
+
+def BO: String = ansiBold
+def RST: String = ansiReset
+
+def g(s: Any): String = s"$G${s.toString}$RST"
+def m(s: Any): String = s"$M${s.toString}$RST"
+def r(s: Any): String = s"$R${s.toString}$RST"
+def c(s: Any): String = s"$C${s.toString}$RST"
+def y(s: Any): String = s"$Y${s.toString}$RST"
+def w(s: Any): String = s"$W${s.toString}$RST"
+def b(s: Any): String = s"$B${s.toString}$RST"
+def k(s: Any): String = s"$K${s.toString}$RST"
+
+def green(s: Any): String = s"$G${s.toString}$RST"
+def magenta(s: Any): String = s"$M${s.toString}$RST"
+def red(s: Any): String = s"$R${s.toString}$RST"
+def cyan(s: Any): String = s"$C${s.toString}$RST"
+def yellow(s: Any): String = s"$Y${s.toString}$RST"
+def white(s: Any): String = s"$W${s.toString}$RST"
+def blue(s: Any): String = s"$B${s.toString}$RST"
+def black(s: Any): String = s"$K${s.toString}$RST"
+def gb(s: Any): String = s"$GB${s.toString}$RST"
+def rb(s: Any): String = s"$RB${s.toString}$RST"
+def cb(s: Any): String = s"$CB${s.toString}$RST"
+def yb(s: Any): String = s"$YB${s.toString}$RST"
+def wb(s: Any): String = s"$WB${s.toString}$RST"
+def bb(s: Any): String = s"$BB${s.toString}$RST"
+def kb(s: Any): String = s"$KB${s.toString}$RST"
+def greenBg(s: Any): String = s"$GB${s.toString}$RST"
+def magentaBg(s: Any): String = s"$MB${s.toString}$RST"
+def redBg(s: Any): String = s"$RB${s.toString}$RST"
+def cyanBg(s: Any): String = s"$CB${s.toString}$RST"
+def yellowBg(s: Any): String = s"$YB${s.toString}$RST"
+def whiteBg(s: Any): String = s"$WB${s.toString}$RST"
+def blueBg(s: Any): String = s"$BB${s.toString}$RST"
+def blackBg(s: Any): String = s"$KB${s.toString}$RST"
+
+// Effect shortcuts...
+def rv(s: Any): String = s"$ansiReversed${s.toString}$RST"
+def bo(s: Any): String = s"$ansiBold${s.toString}$RST"
+def reverse(s: Any): String = s"$ansiReversed${s.toString}$RST"
+def bold(s: Any): String = s"$ansiBold${s.toString}$RST"
+
+extension(v: Int)
+ def MS: Int = v
+ def SECS: Int = v * 1000
+ def MINS: Int = v * 1000 * 60
+ def HOURS: Int = v * 1000 * 60 * 60
+ def DAYS: Int = v * 1000 * 60 * 60 * 24
+ def FPS: Int = 1000 / v
+ def ms: Int = MS
+ def secs: Int = SECS
+ def mins: Int = MINS
+ def hours: Int = HOURS
+ def days: Int = DAYS
+ def fps: Int = 1000 / v
+ def TB: Int = v * 1024 * 1024 * 1024 * 1024
+ def GB: Int = v * 1024 * 1024 * 1024
+ def MB: Int = v * 1024 * 1024
+ def KB: Int = v * 1024
+ def tb: Int = TB
+ def gb: Int = GB
+ def mb: Int = MB
+ def kb: Int = KB
+
+extension(v: Long)
+ def TB: Long = v * 1024 * 1024 * 1024 * 1024
+ def GB: Long = v * 1024 * 1024 * 1024
+ def MB: Long = v * 1024 * 1024
+ def KB: Long = v * 1024
+ def tb: Long = TB
+ def gb: Long = GB
+ def mb: Long = MB
+ def kb: Long = KB
+ def MS: Long = v
+ def SECS: Long = v * 1000
+ def MINS: Long = v * 1000 * 60
+ def HOURS: Long = v * 1000 * 60 * 60
+ def DAYS: Long = v * 1000 * 60 * 60 * 24
+ def FPS: Long = 1000 / v
+ def ms: Long = MS
+ def secs: Long = SECS
+ def mins: Long = MINS
+ def hours: Long = HOURS
+ def days: Long = DAYS
+ def fps: Long = 1000 / v
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLIntent.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLIntent.scala
new file mode 100644
index 0000000..a60bccb
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLIntent.scala
@@ -0,0 +1,59 @@
+/*
+ * 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.intent
+
+import org.apache.nlpcraft.*
+
+import java.util.regex.Pattern
+
+/**
+ * IDL intent.
+ *
+ * @param origin File path, file name, method name or URL.
+ * @param idl Original IDL of this intent.
+ * @param id
+ * @param options
+ * @param meta
+ * @param flow
+ * @param terms
+ */
+case class NCIDLIntent(
+ origin: String,
+ idl: String,
+ id: String,
+ options: NCIDLIntentOptions,
+ meta: Map[String, Object],
+ flow: Option[String],
+ flowClsName: Option[String],
+ flowMtdName: Option[String],
+ terms: List[NCIDLTerm]
+):
+ require(id != null)
+ require(terms.nonEmpty)
+ require(meta != null)
+ require(options != null)
+
+ // Flow regex as a compiled pattern.
+ // Regex validity check is already done during intent compilation.
+ lazy val flowRegex: Option[Pattern] = flow match
+ case Some(r) => Some(Pattern.compile(r))
+ case None => None
+
+ lazy val isFlowDefined: Boolean = flow.isDefined || flowMtdName.isDefined
+
+ override def toString: String = idl
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLIntentOptions.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLIntentOptions.scala
new file mode 100644
index 0000000..a2a02ab
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLIntentOptions.scala
@@ -0,0 +1,44 @@
+/*
+ * 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.intent
+
+/**
+ * Intent options container.
+ */
+class NCIDLIntentOptions:
+ /** Whether to ignore unused free words for intent match. */
+ var ignoreUnusedFreeWords: Boolean = true
+ /** Whether to ignore unused entities for intent match. */
+ var ignoreUnusedEntities: Boolean = true
+ /** Whether or not to allow intent to match if all matching entities came
from STM only. */
+ var allowStmEntityOnly: Boolean = false
+ /** Whether or not the order of term is important for intent match. */
+ var ordered: Boolean = false
+
+/**
+ *
+ */
+object NCIDLIntentOptions:
+ /*
+ * JSON field names.
+ */
+ final val JSON_UNUSED_FREE_WORDS = "unused_free_words"
+ final val JSON_UNUSED_ENTS = "unused_entities"
+ final val JSON_ALLOW_STM_ONLY = "allow_stm_only"
+ final val JSON_ORDERED = "ordered"
+
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLTerm.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLTerm.scala
new file mode 100644
index 0000000..fd52a8b
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/intent/NCIDLTerm.scala
@@ -0,0 +1,66 @@
+/*
+ * 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.intent
+
+import org.apache.nlpcraft.*
+
+/**
+ * IDL term.
+ *
+ * @param idl
+ * @param id Optional ID of this term.
+ * @param decls Term optional declarations.
+ * @param pred Term predicate.
+ * @param min
+ * @param max
+ * @param conv Whether or not this term support conversation context.
+ * @param fragMeta
+ */
+case class NCIDLTerm(
+ idl: String,
+ id: Option[String],
+ decls: Map[String, NCIDLFunction],
+ pred: NCIDLFunction,
+ min: Int,
+ max: Int,
+ conv: Boolean,
+ fragMeta: Map[String, Any] = Map.empty
+):
+ require(pred != null)
+ require(min >= 0 && max >= min)
+
+ /**
+ *
+ * @param meta
+ * @return
+ */
+ def cloneWithFragMeta(meta: Map[String, Any]): NCIDLTerm =
+ NCIDLTerm(
+ idl,
+ id,
+ decls,
+ pred,
+ min,
+ max,
+ conv,
+ meta
+ )
+
+ override def toString: String = idl
+
+ def toAnsiString: String = g(idl)
\ No newline at end of file