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 2020/06/13 18:34:34 UTC

[incubator-nlpcraft] branch master updated: WIP on NLPCRAFT-80.

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 07da3df  WIP on NLPCRAFT-80.
07da3df is described below

commit 07da3dff4871f55817dd7ed6126996d5de21f184
Author: Aaron Radzinzski <ar...@datalingvo.com>
AuthorDate: Sat Jun 13 11:34:13 2020 -0700

    WIP on NLPCRAFT-80.
---
 .../org/apache/nlpcraft/examples/sql/SqlModel.scala | 16 ++++++++++++----
 .../scala/org/apache/nlpcraft/model/NCIntent.java   |  1 +
 .../{NCIntentTerm.java => NCIntentExample.java}     | 21 ++++++++++++---------
 .../org/apache/nlpcraft/model/NCIntentRef.java      |  1 +
 .../org/apache/nlpcraft/model/NCIntentTerm.java     |  1 +
 .../model/intent/impl/NCIntentScanner.scala         | 18 +++++++++---------
 6 files changed, 36 insertions(+), 22 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 c4cf153..e4fb86c 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
@@ -252,11 +252,11 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         }
         catch {
             case e: Exception ⇒
-                System.err.println(if (query == null) "Query cannot be prepared" else "Query execution error")
+                System.err.println(if (query == null) "Query cannot be prepared." else "Query execution error.")
 
                 e.printStackTrace()
 
-                NCResult.json(toJson("Question cannot be answered, reformulate it"))
+                NCResult.json(toJson("Question cannot be answered as is."))
         }
     }
 
@@ -287,6 +287,11 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         "term(sort)={id == 'nlpcraft:sort'}? " +
         "term(limit)={id == 'nlpcraft:limit'}?"
     )
+    @NCIntentExample(Array(
+        "order date, please!",
+        "show me the order dates",
+        "list dates of orders"
+    ))
     def onCommonReport(
         ctx: NCIntentMatch,
         @NCIntentTerm("tbls") tbls: Seq[NCToken],
@@ -344,6 +349,9 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         "term(condFreeDate)={id == 'nlpcraft:date'}? " +
         "term(limit)={id == 'nlpcraft:limit'}?"
     )
+    @NCIntentExample(Array(
+        "What are the least performing categories for the last quarter?"
+    ))
     def onCustomSortReport(
         ctx: NCIntentMatch,
         @NCIntentTerm("sort") sortTok: NCToken,
@@ -358,9 +366,9 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         val ordersFreightColSort: NCSqlSort =
             new NCSqlSort {
                 override def getColumn: NCSqlColumn = SCHEMA.getTables.asScala.find(_.getTable == "orders").
-                    getOrElse(throw new RuntimeException(s"Table `orders` not found")).
+                    getOrElse(throw new RuntimeException(s"Table `orders` not found.")).
                     getColumns.asScala.find(_.getColumn == "freight").
-                    getOrElse(throw new RuntimeException(s"Column `orders.freight` not found"))
+                    getOrElse(throw new RuntimeException(s"Column `orders.freight` not found."))
                 override def isAscending: Boolean =
                     sortTok.getId match {
                         case "sort:best" ⇒ false
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntent.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntent.java
index e3f202c..c5523c7 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntent.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntent.java
@@ -31,6 +31,7 @@ import static java.lang.annotation.RetentionPolicy.*;
  *
  * @see NCIntentRef
  * @see NCIntentTerm
+ * @see NCIntentExample
  * @see NCIntentSkip
  * @see NCIntentMatch
  * @see NCModel#onMatchedIntent(NCIntentMatch) 
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentExample.java
similarity index 68%
copy from nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java
copy to nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentExample.java
index 9ded1d3..db54388 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentExample.java
@@ -23,25 +23,28 @@ import static java.lang.annotation.ElementType.*;
 import static java.lang.annotation.RetentionPolicy.*;
 
 /**
- * Annotation to mark callback parameter to receive intent term's tokens. This is a companion annotation
- * to {@link NCIntent} and {@link NCIntentRef} annotations and can only be used for
- * the parameters of the methods that are annotated with {@link NCIntent} or {@link NCIntentRef}.
- * {@code NCIntentTerm} takes a term ID as its only mandatory parameter and should be applied to callback
- * method parameters to get the tokens associated with that term (if and when the intent was matched and that
- * callback was invoked).
+ * Annotation to define one or more examples of the user input that should match a corresponding intent. This
+ * annotation can only be used together with {@link NCIntent} or {@link NCIntentRef} annotations on the callback
+ * methods.
+ * <p>
+ * Note that the examples provided by this annotation not only serve the documentation purpose but are also
+ * used internally by various parts of NLPCraft for uint testing, synonym detection, etc. It is highly
+ * advised to provide multiple examples per each intent either through this annotation of in external model
+ * declaration.
  * <p>
  * Read full documentation in <a target=_ href="https://nlpcraft.apache.org/intent-matching.html">Intent Matching</a> section and review
  * <a target=_ href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/">examples</a>.
  *
  * @see NCIntent
  * @see NCIntentRef
+ * @see NCIntentTerm
  * @see NCIntentSkip
  * @see NCIntentMatch
  * @see NCModel#onMatchedIntent(NCIntentMatch)
  */
 @Documented
 @Retention(value=RUNTIME)
-@Target(value=PARAMETER)
-public @interface NCIntentTerm {
-    String value();
+@Target(value=METHOD)
+public @interface NCIntentExample {
+    String[] value();
 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentRef.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentRef.java
index 4c030ce..6606c3e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentRef.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentRef.java
@@ -32,6 +32,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
  *
  * @see NCIntent
  * @see NCIntentTerm
+ * @see NCIntentExample
  * @see NCIntentSkip
  * @see NCIntentMatch
  * @see NCModel#onMatchedIntent(NCIntentMatch)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java
index 9ded1d3..68a5d95 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCIntentTerm.java
@@ -35,6 +35,7 @@ import static java.lang.annotation.RetentionPolicy.*;
  *
  * @see NCIntent
  * @see NCIntentRef
+ * @see NCIntentExample
  * @see NCIntentSkip
  * @see NCIntentMatch
  * @see NCModel#onMatchedIntent(NCIntentMatch)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentScanner.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentScanner.scala
index 9cbdb81..ffd8389 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentScanner.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/NCIntentScanner.scala
@@ -160,17 +160,16 @@ object NCIntentScanner {
         checkMinMax(m, tokParamTypes, termIds.map(allLimits), ctxFirstParam)
 
         // Prepares invocation method.
-        new Callback() {
-            override def apply(ctx: NCIntentMatch): NCResult = {
-                invoke(
-                    m,
-                    obj,
-                    (
-                        (if (ctxFirstParam) Seq(ctx) else Seq.empty) ++
+        (ctx: NCIntentMatch) => {
+            invoke(
+                m,
+                obj,
+                (
+                    (if (ctxFirstParam) Seq(ctx)
+                    else Seq.empty) ++
                         prepareParams(m, tokParamTypes, termIds.map(ctx.getTermTokens), ctxFirstParam)
                     ).toArray
-                )
-            }
+            )
         }
     }
 
@@ -182,6 +181,7 @@ object NCIntentScanner {
       */
     @throws[NCE]
     private def invoke(m: Method, obj: Any, args: Array[AnyRef]): NCResult = {
+        // TODO: fix in Java 9+.
         var flag = m.isAccessible
 
         try {