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

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

commit 32dd0d0cb87d75fbc87c414e9d8fbfd36ff9ac4f
Author: Aaron Radzinski <[email protected]>
AuthorDate: Wed Mar 24 19:59:42 2021 -0700

    WIP.
---
 .../model/intent/compiler/NCIdlCompilerBase.scala  | 38 ++++++++++++++++++++--
 .../functions/NCIdlFunctionsCollections.scala      |  2 ++
 .../compiler/functions/NCIdlFunctionsStrings.scala |  3 ++
 3 files changed, 40 insertions(+), 3 deletions(-)

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 66cb2c1..0a7087f 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
@@ -995,6 +995,36 @@ trait NCIdlCompilerBase {
             })
         }
 
+        def doLength(): Unit = {
+            val x = arg1()
+
+            stack.push(() ⇒ {
+                val Z(v, n) = x()
+
+                if (isList(v))
+                    Z(asList(v).size(), n)
+                else if (isStr(v))
+                    Z(asStr(v), n)
+                else
+                    throw rtParamTypeError(fun, v, "string or list")
+            })
+        }
+
+        def doIsEmpty(empty: Boolean): Unit = {
+            val x = arg1()
+
+            stack.push(() ⇒ {
+                val Z(v, n) = x()
+
+                if (isList(v))
+                    Z(asList(v).isEmpty == empty, n)
+                else if (isStr(v))
+                    Z(asStr(v).isEmpty == empty, n)
+                else
+                    throw rtParamTypeError(fun, v, "string or list")
+            })
+        }
+
         def z[Y](args: () ⇒ Y, body: Y ⇒ Z): Unit = { val x = args(); 
stack.push(() ⇒ body(x)) }
         def z0(body: () ⇒ Z): Unit = { popMarker(0); stack.push(() ⇒ body()) }
 
@@ -1139,14 +1169,16 @@ trait NCIdlCompilerBase {
             case "last" ⇒ z[ST](arg1, { x ⇒ val Z(v, n) = x(); val lst = 
toList(v); Z(if (lst.isEmpty) null else lst.get(lst.size() - 
1).asInstanceOf[Object], n)})
             case "keys" ⇒ z[ST](arg1, { x ⇒ val Z(v, n) = x(); Z(new 
util.ArrayList(toMap(v).keySet()), n) })
             case "values" ⇒ z[ST](arg1, { x ⇒ val Z(v, n) = x(); Z(new 
util.ArrayList(toMap(v).values()), n) })
-            case "size" | "count" | "length" ⇒ z[ST](arg1, { x ⇒ val Z(v, n) = 
x(); Z(toList(v).size(), n)})
             case "reverse" ⇒ doReverse()
             case "sort" ⇒ doSort()
-            case "is_empty" ⇒ z[ST](arg1, { x ⇒ val Z(v, n) = x(); 
Z(toList(v).isEmpty, n) })
-            case "non_empty" ⇒ z[ST](arg1, { x ⇒ val Z(v, n) = x(); 
Z(!toList(v).isEmpty, n) })
+            case "is_empty" ⇒ doIsEmpty(true)
+            case "non_empty" ⇒ doIsEmpty(false)
             case "distinct" ⇒ doDistinct()
             case "concat" ⇒ doConcat()
 
+            // Applies to strings as well.
+            case "size" | "count" | "length" ⇒ doLength()
+
             // Misc.
             case "to_string" ⇒ doToString()
 
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCollections.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCollections.scala
index 8f3805f..8ef84e0 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCollections.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCollections.scala
@@ -57,6 +57,8 @@ class NCIdlFunctionsCollections extends NCIdlFunctions {
             "size(list(2.0, 1, 3)) == 3",
             "length(list(2.0, 1, 3)) == 3",
             "count(list(2.0, 1, 3)) == 3",
+            "is_empty(list()) == true",
+            "non_empty(list(1)) == true",
             s"keys(json('$js')) == list('k1')",
             s"values(json('$js')) == list('v1')",
             s"distinct(list(1, 2, 3, 3, 2)) == list(1, 2, 3)",
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
index f913e91..e0c8be1 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala
@@ -54,6 +54,9 @@ class NCIdlFunctionsStrings extends NCIdlFunctions {
             "replace('abc', '0',  '0') == 'abc'",
             "split('1 A', ' ') == list('1', 'A')",
             "split_trim('1 A    ', ' ') == list('1', 'A')",
+            "is_empty('a') == false",
+            "non_empty('a') == true",
+            "is_empty('') == true",
 
             // Whitespaces.
             "replace('abc', 'ab',  '') == 'c'",

Reply via email to