You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2020/04/16 11:30:41 UTC

[incubator-nlpcraft] 01/03: WIP.

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

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

commit 0b4c47780a091c165fdddefa6bc2d0a47f8a52b2
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Wed Apr 15 12:17:59 2020 +0300

    WIP.
---
 .../apache/nlpcraft/examples/sql/SqlModel.scala    | 54 ++++++++++++++++++----
 .../nlpcraft/examples/sql/db/SqlServer.scala       |  4 +-
 2 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
index 6d78653..a0ce9bd 100644
--- a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
+++ b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
@@ -53,7 +53,8 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
     }
     
     private def findColumnToken(tok: NCToken): NCToken = {
-        val cols = (Seq(tok) ++ tok.findPartTokens().asScala).flatMap(p ⇒ if (p.getGroups.contains("column")) Some(p) else None)
+        val cols = (Seq(tok) ++ tok.findPartTokens().asScala).
+            flatMap(p ⇒ if (p.getGroups.contains("column")) Some(p) else None)
         
         cols.size match {
             case 1 ⇒ cols.head
@@ -97,7 +98,7 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
     private def findAnyColumnToken(tok: NCToken): NCToken =
         findAnyColumnTokenOpt(tok).getOrElse(throw new IllegalArgumentException(s"No columns found for token: $tok"))
 
-    def extractNumConditions(ext: NCSqlExtractor, colTok: NCToken, numTok: NCToken): Seq[SqlSimpleCondition] = {
+    private def extractNumConditions(ext: NCSqlExtractor, colTok: NCToken, numTok: NCToken): Seq[SqlSimpleCondition] = {
         val col = ext.extractColumn(colTok)
 
         val from: java.lang.Double = numTok.meta("nlpcraft:num:from")
@@ -219,11 +220,46 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         }
     }
 
-    // TODO: it is not started.
-//    override def onMatchedIntent(m: NCIntentMatch): Boolean = {
-//        val convToks = m.getContext.getConversation.getTokens.asScala
-//        val varToks = m.getVariant.getMatchedTokens.asScala
-//
-//        super.onMatchedIntent(m)
-//    }
+    override def onMatchedIntent(m: NCIntentMatch): Boolean = {
+        val toks = m.getVariant.getMatchedTokens.asScala
+        val newToks = toks -- m.getContext.getConversation.getTokens.asScala
+
+        println("toks=" + toks.map(_.origText))
+        println("conv=" + m.getContext.getConversation.getTokens.asScala.map(_.origText))
+        println("newToks=" + newToks.map(_.origText))
+
+        // Variant doesn't use conversation tokens.
+        if (newToks.length == toks.length)
+            true
+        else {
+            def isValue(t: NCToken): Boolean = findAnyColumnTokenOpt(t) match {
+                case Some(col) ⇒ col.getValue != null
+                case None ⇒ false
+            }
+            def isColumn(t: NCToken): Boolean = findAnyColumnTokenOpt(t).isDefined
+            def isDate(t: NCToken): Boolean = t.getId == "nlpcraft:date"
+
+            // Conversation supported if
+            // - all new tokens are values,
+            // - all new tokens are columns,
+            // - new single token is date.
+            // So, this example supports conversation for simple qualifying questions.
+            val suitable =
+                newToks.forall(isValue) ||
+                newToks.forall(isColumn) ||
+                newToks.size == 1 && isDate(toks.head)
+
+            if (!suitable) {
+                // TODO: drop it.
+                if (m.getContext.getVariants.size() == 1)
+                    throw new NCRejection("Question cannot be answered")
+
+                logger.info("Conversation reset")
+
+                m.getContext.getConversation.clearAllStm()
+            }
+
+            suitable
+        }
+    }
 }
\ No newline at end of file
diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlServer.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlServer.scala
index 5903330..8620002 100644
--- a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlServer.scala
+++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlServer.scala
@@ -52,7 +52,7 @@ object SqlServer extends App with LazyLogging {
 
     lazy final val H2_URL: String = s"jdbc:h2:tcp://localhost:$H2_PORT/nlp2sql"
 
-    private def go(): Unit = {
+    private def process(): Unit = {
         val srv = Server.createTcpServer(SRV_PARAMS:_*).start
 
         logger.info(s"H2 server start parameters: ${SRV_PARAMS.mkString(" ")}")
@@ -83,5 +83,5 @@ object SqlServer extends App with LazyLogging {
         Thread.currentThread().join()
     }
 
-    go()
+    process()
 }