This is an automated email from the ASF dual-hosted git repository. aradzinski pushed a commit to branch NLPCRAFT-206 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit da3588412cfc71b43e48673d2dd0956c1c13a737 Author: Aaron Radzinski <[email protected]> AuthorDate: Mon Feb 8 18:11:27 2021 -0800 WIP. --- .../intent/impl/ver2/NCIntentDslCompiler.scala | 38 ++++++++++++++++++++++ .../model/intent/utils/ver2/NCDslIntent.scala | 18 ++++++++++ 2 files changed, 56 insertions(+) diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala index 86b3975..8e1892f 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala @@ -50,6 +50,8 @@ object NCIntentDslCompiler extends LazyLogging { private var min = 1 private var max = 1 + private var cval: Any = _ + /** * * @param min @@ -101,6 +103,42 @@ object NCIntentDslCompiler extends LazyLogging { ordered = ctx.BOOL().getText.strip == "true" } + override def exitVal(ctx: NCIntentDslParser.ValContext): Unit = { + cval = mkVal(ctx.getText) + } + + /** + * + * @param s + * @return + */ + private def mkVal(s: String): Any = { + if (s == "null") null // Try 'null'. + else if (s == "true") true // Try 'boolean'. + else if (s == "false") false // Try 'boolean'. + // Only numeric values below... + else { + // Strip '_' from numeric values. + val num = s.replaceAll("_", "") + + try + java.lang.Integer.parseInt(num) // Try 'int'. + catch { + case _: NumberFormatException ⇒ + try + java.lang.Long.parseLong(num) // Try 'long'. + catch { + case _: NumberFormatException ⇒ + try + java.lang.Double.parseDouble(num) // Try 'double'. + catch { + case _: NumberFormatException ⇒ s // String by default (incl. quotes). + } + } + } + } + } + /** * * @return diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala index f76de0d..c86b415 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslIntent.scala @@ -17,6 +17,8 @@ package org.apache.nlpcraft.model.intent.utils.ver2 +import java.util.regex.{Pattern, PatternSyntaxException} + /** * DSL intent. */ @@ -31,4 +33,20 @@ case class NCDslIntent( require(id != null) require(terms.nonEmpty) require(meta != null) + + // Flow regex as a compiled pattern. + val flowRegex = flow match { + case Some(r) ⇒ + try + Some(Pattern.compile(r)) + catch { + case e: PatternSyntaxException ⇒ + throw new IllegalArgumentException(s"${e.getDescription} in flow regex '${e.getPattern}' near index ${e.getIndex}.") + } + + case None ⇒ None + } + + override def toString: String = + s"Intent: '$id'" }
