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
The following commit(s) were added to refs/heads/NLPCRAFT-206 by this push:
new 9fc897f WIP.
9fc897f is described below
commit 9fc897fc5cd57a005f0a16f3a82acbbccdebfab1
Author: Aaron Radzinski <[email protected]>
AuthorDate: Mon Mar 1 18:53:38 2021 -0800
WIP.
---
.../intent/impl/ver2/NCIntentDslCompiler.scala | 33 ++++++++++++++++++----
.../model/intent/utils/ver2/NCDslTerm.scala | 18 +++++++++++-
.../org/apache/nlpcraft/model/intent/dsl/test.nc | 2 +-
3 files changed, 45 insertions(+), 8 deletions(-)
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 a941b5a..6943f1c 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
@@ -47,18 +47,17 @@ object NCIntentDslCompiler extends LazyLogging {
// Fragment components.
private var fragId: String = _
+ private var fragMeta: Map[String, Any] = _
// Intent components.
private var intentId: String = _
private var ordered: Boolean = false
private var flowRegex: Option[String] = None
+ private var intentMeta: Map[String, Any] = _
// Accumulator for parsed terms.
private val terms = ArrayBuffer.empty[NCDslTerm]
- // Current JSON-based meta.
- private var meta: Map[String, Any] = _
-
// Currently term.
private var termId: String = _
private var termConv: Boolean = _
@@ -116,9 +115,31 @@ object NCIntentDslCompiler extends LazyLogging {
override def exitTermEq(ctx: IDP.TermEqContext): Unit = termConv =
ctx.TILDA() != null
override def exitIntentId(ctx: IDP.IntentIdContext): Unit = intentId =
ctx.id().getText
override def exitFragId(ctx: IDP.FragIdContext): Unit = fragId =
ctx.id().getText
- override def exitMetaDecl(ctx: IDP.MetaDeclContext): Unit = meta =
U.jsonToScalaMap(ctx.jsonObj().getText)
+ override def exitFragMeta(ctx: IDP.FragMetaContext): Unit = fragMeta =
U.jsonToScalaMap(ctx.jsonObj().getText)
+ override def exitMetaDecl(ctx: IDP.MetaDeclContext): Unit = intentMeta
= U.jsonToScalaMap(ctx.jsonObj().getText)
override def exitOrderedDecl(ctx: IDP.OrderedDeclContext): Unit =
ordered = ctx.BOOL().getText == "true"
+ override def exitFragRef(ctx: IDP.FragRefContext): Unit = {
+ implicit val evidence: ParserRuleContext = ctx
+
+ val id = ctx.id().getText
+
+ FragCache.get(mdlId, id) match {
+ case Some(frag) ⇒
+ val meta = if (fragMeta == null) Map.empty[String, Any]
else fragMeta
+
+ for (fragTerm ← frag.terms)
+ if (terms.exists(t ⇒ t.id != null && t.id ==
fragTerm.id))
+ throw newSyntaxError(s"Duplicate fragment term ID:
${fragTerm.id}")
+ else
+ terms += fragTerm.cloneWithMeta(meta)
+
+ case None ⇒ throw newSyntaxError(s"Unknown intent fragment ID:
$id")
+ }
+
+ fragMeta = null
+ }
+
override def exitFlowDecl(ctx: IDP.FlowDeclContext): Unit = {
implicit val evidence: ParserRuleContext = ctx
@@ -238,7 +259,7 @@ object NCIntentDslCompiler extends LazyLogging {
dsl,
intentId,
ordered,
- if (meta == null) Map.empty else meta,
+ if (intentMeta == null) Map.empty else intentMeta,
flowRegex,
refClsName,
refMtdName,
@@ -247,7 +268,7 @@ object NCIntentDslCompiler extends LazyLogging {
refClsName = None
refMtdName = None
-
+ intentMeta = null
terms.clear()
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
index 81df585..79a5ed2 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTerm.scala
@@ -33,8 +33,24 @@ case class NCDslTerm(
pred: (NCToken, NCDslTermContext) ⇒ (Boolean/*Predicate.*/,
Boolean/*Whether or not token was used.*/),
min: Int,
max: Int,
- conv: Boolean
+ conv: Boolean,
+ fragMeta: Map[String, Any] = Map.empty
) {
require(pred != null)
require(min >= 0 && max >= min)
+
+ /**
+ *
+ * @param meta
+ * @return
+ */
+ def cloneWithMeta(meta: Map[String, Any]): NCDslTerm =
+ NCDslTerm(
+ id,
+ pred,
+ min,
+ max,
+ conv,
+ meta
+ )
}
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/test.nc
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/test.nc
index 70f80ab..5f8ad98 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/test.nc
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/test.nc
@@ -26,7 +26,7 @@ predicate=p1
// Intent #1.
intent=i1
flow=/org.package#method1/ // User-code flow predicate.
- predicate(p1) /* Macro-expansion. */
+ predicate(p1, {'a': true, 'b': {'Москва': [1, 2, 3]}}) /*
Macro-expansion. */
term~{length("some text") > 0} // Normal term.
// Intent #2.