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 2021/03/20 05:47:43 UTC

[incubator-nlpcraft] branch master updated: Improved meta_part() function.

This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new b0adec9  Improved meta_part() function.
b0adec9 is described below

commit b0adec973075ab1b5770a6bb86219c2febc4866c
Author: Aaron Radzinzski <ar...@datalingvo.com>
AuthorDate: Fri Mar 19 22:47:28 2021 -0700

    Improved meta_part() function.
---
 .../apache/nlpcraft/examples/sql/SqlModel.scala    |  2 +-
 .../apache/nlpcraft/examples/sql/sql_model.yaml    |  6 ++--
 .../model/intent/compiler/NCIdlCompilerBase.scala  | 32 +++++++++++++++++++---
 .../model/NCEnricherNestedModelSpec5.scala         |  2 +-
 4 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
index 4089507..7cefe2e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
@@ -342,7 +342,7 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         "intent=customSortReport " +
             "term(sort)~{id() == 'sort:best' || id() == 'sort:worst'} " +
             "term(tbls)~{has(groups(), 'table')}[0,7] " +
-            "term(cols)~{id() == 'col:date' || id() == 'col:num' || id() == 'col:varchar'}[0,7] " +
+            "term(cols)~{has(list('col:date', 'col:num', 'col:varchar'), id())}[0,7] " +
             "term(condNums)~{id() == 'condition:num'}[0,7] " +
             "term(condVals)~{id() == 'condition:value'}[0,7] " +
             "term(condDates)~{id() == 'condition:date'}[0,7] " +
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml
index 1a7eb0a..fb4d403 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/sql_model.yaml
@@ -1455,17 +1455,17 @@ elements:
   groups:
     - "element"
   synonyms:
-    - "^^{id() == 'col' && has(list(4, 8), meta_part(find_part(token(), 'colName'), 'sql:datatype'))}^^"
+    - "^^{id() == 'col' && has(list(4, 8), meta_part('colName', 'sql:datatype'))}^^"
 - id: "col:date"
   groups:
     - "element"
   synonyms:
-    - "^^{id() == 'col' && meta_part(find_part(token(), 'colName'), 'sql:datatype') == 91}^^"
+    - "^^{id() == 'col' && meta_part('colName', 'sql:datatype') == 91}^^"
 - id: "col:varchar"
   groups:
     - "element"
   synonyms:
-    - "^^{id() == 'col' && meta_part(find_part(token(), 'colName'), 'sql:datatype') == 12}^^"
+    - "^^{id() == 'col' && meta_part('colName', 'sql:datatype') == 12}^^"
 - id: "condition:num"
   groups:
     - "condition"
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala
index 3f4c961..941ddf4 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala
@@ -672,10 +672,10 @@ trait NCIdlCompilerBase {
             val (x1, x2) = arg2()
 
             stack.push(() ⇒ {
-                val NCIdlStackItem(v1, n1) = x1()
-                val NCIdlStackItem(v2, n2) = x2()
+                val NCIdlStackItem(lst, n1) = x1()
+                val NCIdlStackItem(obj, n2) = x2()
 
-                Z(toJList(v1).contains(box(v2)), n1 + n2)
+                Z(toJList(lst).contains(box(obj)), n1 + n2)
             })
         }
 
@@ -770,6 +770,30 @@ trait NCIdlCompilerBase {
                 Z(parts.get(0), n1 + n2)
             })
         }
+        
+        def doPartMeta(): Unit = {
+            val (x1, x2) = arg2()
+    
+            stack.push(() ⇒ {
+                val NCIdlStackItem(alias, n1) = x1() // Token alias or token ID.
+                val NCIdlStackItem(key, n2) = x2()
+    
+                val parts = tok.findPartTokens(toStr(alias))
+    
+                if (parts.isEmpty)
+                    throw newRuntimeError(s"Cannot find part for token [" +
+                        s"tokenId=${tok.getId}, " +
+                        s"partId=$alias" +
+                        s"]")
+                else if (parts.size() > 1)
+                    throw newRuntimeError(s"Too many parts found for token (use 'parts' function instead) [" +
+                        s"tokenId=${tok.getId}, " +
+                        s"partId=$alias" +
+                        s"]")
+                
+                Z(parts.get(0).meta[Object](toStr(key)), n1 + n2)
+            })
+        }
 
         //noinspection DuplicatedCode
         def doParts(): Unit = {
@@ -791,7 +815,7 @@ trait NCIdlCompilerBase {
 
         fun match {
             // Metadata access.
-            case "meta_part" ⇒ z[(T, T)](arg2, { x ⇒ val NCIdlStackItem(v1, n1) = x._1(); val NCIdlStackItem(v2, n2) = x._2(); Z(toToken(v1).meta[Object](toStr(v2)), n1 + n2) })
+            case "meta_part" ⇒ doPartMeta()
             case "meta_token" ⇒ z[T](arg1, { x ⇒ val NCIdlStackItem(v, _) = x(); Z(tok.meta[Object](toStr(v)), 1) })
             case "meta_model" ⇒ z[T](arg1, { x ⇒ val NCIdlStackItem(v, _) = x(); Z(tok.getModel.meta[Object](toStr(v)), 0) })
             case "meta_intent" ⇒ z[T](arg1, { x ⇒ val NCIdlStackItem(v, _) = x(); Z(termCtx.intentMeta.get(toStr(v)).orNull, 0) })
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCEnricherNestedModelSpec5.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCEnricherNestedModelSpec5.scala
index 175aebc..264d078 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCEnricherNestedModelSpec5.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/model/NCEnricherNestedModelSpec5.scala
@@ -37,7 +37,7 @@ class NCNestedTestModel5 extends NCModelAdapter(
         "intent=bigCity " +
         "term(city)={" +
         "    id() == 'cityWrapper' && " +
-        "    get(meta_part(find_part(token(), 'cityAlias'), 'nlpcraft:city:citymeta'), 'population') >= 10381222" +
+        "    get(meta_part('cityAlias', 'nlpcraft:city:citymeta'), 'population') >= 10381222" +
         "}"
     )
     private def onBigCity(ctx: NCIntentMatch): NCResult = NCResult.text("OK")