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 2022/01/26 14:53:43 UTC

[incubator-nlpcraft] 02/03: WIP.

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

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

commit f930d6019c5fb6bb69e24472c7b49c5ff3280d98
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Jan 26 15:02:32 2022 +0300

    WIP.
---
 .../internal/impl/NCAnnotationsScanner.scala       | 29 +++++-----
 .../internal/impl/intent/NCIdlContext.scala        | 41 --------------
 .../internal/impl/intent/NCIdlFunction.scala       | 25 ---------
 .../internal/impl/intent/NCIdlIntent.scala         | 57 -------------------
 .../internal/impl/intent/NCIdlIntentOptions.scala  | 18 ------
 .../nlpcraft/internal/impl/intent/NCIdlStack.scala | 45 ---------------
 .../nlpcraft/internal/impl/intent/NCIdlTerm.scala  | 64 ----------------------
 .../impl/intent/compiler/NCIdlCompiler.scala       | 34 ------------
 .../nlp/util/opennlp/NCTestConfigJava.java         |  2 +-
 9 files changed, 16 insertions(+), 299 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCAnnotationsScanner.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCAnnotationsScanner.scala
index a5ceda1..c782e3f 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCAnnotationsScanner.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/NCAnnotationsScanner.scala
@@ -19,8 +19,8 @@ package org.apache.nlpcraft.internal.impl
 
 import com.typesafe.scalalogging.LazyLogging
 import org.apache.nlpcraft.*
-import org.apache.nlpcraft.internal.impl.intent.*
-import org.apache.nlpcraft.internal.impl.intent.compiler.*
+import org.apache.nlpcraft.internal.intent.*
+import org.apache.nlpcraft.internal.intent.compiler.*
 import org.apache.nlpcraft.internal.makro.NCMacroParser
 import org.apache.nlpcraft.internal.util.NCUtils
 
@@ -40,7 +40,7 @@ private[internal] case class NCCallback(method: Method, cbFun: Function[NCIntent
     val clsName: String = method.getDeclaringClass.getName
     val funName: String = method.getName
 
-private[internal] case class NCIntentData(intent: NCIdlIntent, callback: NCCallback, samples: Seq[Seq[String]])
+private[internal] case class NCIntentData(intent: NCIDLIntent, callback: NCCallback, samples: Seq[Seq[String]])
 
 /**
   *
@@ -313,23 +313,24 @@ import org.apache.nlpcraft.internal.impl.NCAnnotationsScanner.*
   * @param mdl
   */
 class NCAnnotationsScanner(mdl: NCModel) extends LazyLogging:
-    private val id = mdl.getConfig.getId
-    private val origin = mdl.getConfig.getOrigin
+    private val cfg = mdl.getConfig
+    private val id = cfg.getId
+    private val origin = cfg.getOrigin
 
     /**
       *
       * @return
       */
-    private def scanClasses(): (mutable.Buffer[NCIdlIntent], Iterable[Class[_]]) =
+    private def scanClasses(): (mutable.Buffer[NCIDLIntent], Iterable[Class[_]]) =
         val m = mutable.HashMap.empty[Class[_], Class[_]]
-        val intentDecls = mutable.Buffer.empty[NCIdlIntent]
+        val intentDecls = mutable.Buffer.empty[NCIDLIntent]
 
         def addImports(anns: scala.Array[NCIntentImport], orig: => String): Unit =
             checkMultiple(anns, (a: NCIntentImport) => a.value, orig)
             for (
                 ann <- anns;
                 res <- ann.value;
-                intent <- NCIdlCompiler.compileIntents(NCUtils.readResource(res.strip).mkString("\n"), mdl, res)
+                intent <- NCIDLCompiler.compile(NCUtils.readResource(res.strip).mkString("\n"), cfg, res)
             )
                 if intentDecls.exists(_.id == intent.id) then
                     E(s"Duplicate intent ID [mdlId=$id, origin=$origin, resource=$res, id=${intent.id}]")
@@ -369,7 +370,7 @@ class NCAnnotationsScanner(mdl: NCModel) extends LazyLogging:
       * @return
       */
     def scan(): Seq[NCIntentData] =
-        val (intentDecls: mutable.Buffer[NCIdlIntent], classes: Iterable[Class[_]]) = scanClasses()
+        val (intentDecls: mutable.Buffer[NCIDLIntent], classes: Iterable[Class[_]]) = scanClasses()
 
         val intents = mutable.Buffer.empty[NCIntentData]
 
@@ -486,12 +487,12 @@ class NCAnnotationsScanner(mdl: NCModel) extends LazyLogging:
       *
       * @param mo
       */
-    private def processMethod(intentDecls: mutable.Buffer[NCIdlIntent], intents: mutable.Buffer[NCIntentData], mo: MethodOwner): Unit =
+    private def processMethod(intentDecls: mutable.Buffer[NCIDLIntent], intents: mutable.Buffer[NCIntentData], mo: MethodOwner): Unit =
         val m = mo.method
         val mtdStr = method2Str(m)
         lazy val samples = scanSamples(mo.method)
 
-        def bindIntent(intent: NCIdlIntent, cb: NCCallback): Unit =
+        def bindIntent(intent: NCIDLIntent, cb: NCCallback): Unit =
             if intents.exists(i => i._1.id == intent.id && i._2.id != cb.id) then
                 E(s"The intent cannot be bound to more than one callback [mdlId=$id, origin=$origin, class=${mo.className}, intentId=${intent.id}]")
             else
@@ -507,7 +508,7 @@ class NCAnnotationsScanner(mdl: NCModel) extends LazyLogging:
         val annsIntents = m.getAnnotationsByType(CLS_INTENT)
         checkSingle(annsIntents, (a:NCIntent) => a.value, mtdStr)
 
-        for (ann <- annsIntents; intent <- NCIdlCompiler.compileIntents(ann.value, mdl, mtdStr))
+        for (ann <- annsIntents; intent <- NCIDLCompiler.compile(ann.value, cfg, mtdStr))
             if intentDecls.exists(_.id == intent.id && existsForOtherMethod(intent.id)) then
                 E(s"Duplicate intent ID [mdlId=$id, origin=$origin, callback=$mtdStr, id=${intent.id}]")
             else
@@ -530,7 +531,7 @@ class NCAnnotationsScanner(mdl: NCModel) extends LazyLogging:
       * @param intent
       * @return
       */
-    private def prepareCallback(mo: MethodOwner, intent: NCIdlIntent): NCCallback =
+    private def prepareCallback(mo: MethodOwner, intent: NCIDLIntent): NCCallback =
         val mtd = mo.method
 
         // Checks method result type.
@@ -651,7 +652,7 @@ class NCAnnotationsScanner(mdl: NCModel) extends LazyLogging:
 
                 val distinct = seqSeq.map(_.distinct).distinct
 
-                for (ann <- intAnns; intent <- NCIdlCompiler.compileIntents(ann.value, mdl, mtdStr))
+                for (ann <- intAnns; intent <- NCIDLCompiler.compile(ann.value, cfg, mtdStr))
                     samples += intent.id -> distinct
 
                 for (ann <- refAnns) samples += (ann.value -> distinct)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlContext.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlContext.scala
deleted file mode 100644
index f7e5380..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlContext.scala
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nlpcraft.internal.impl.intent
-
-import org.apache.nlpcraft.*
-
-import scala.collection.mutable
-
-/**
- *
- * @param toks User input tokens.
- * @param intentMeta Intent metadata.
- * @param convMeta Conversation metadata.
- * @param fragMeta Fragment (argument) metadata passed during intent fragment reference.
- * @param req Server request holder.
- * @param vars Intent variable storage.
- */
-case class NCIdlContext(
-    toks: Seq[NCToken] = Seq.empty,
-    intentMeta: Map[String, Object] = Map.empty,
-    convMeta: Map[String, Object] = Map.empty,
-    fragMeta: Map[String, Object] = Map.empty,
-    req: NCRequest,
-    vars: mutable.Map[String, NCIdlFunction] = mutable.HashMap.empty
-)
-
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlFunction.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlFunction.scala
deleted file mode 100644
index 062209b..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlFunction.scala
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nlpcraft.internal.impl.intent
-
-import org.apache.nlpcraft.NCToken
-
-/**
- *
- */
-trait NCIdlFunction extends ((NCToken, NCIdlContext) => NCIdlStackItem)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlIntent.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlIntent.scala
deleted file mode 100644
index 30ff0cb..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlIntent.scala
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nlpcraft.internal.impl.intent
-
-import java.util.regex.Pattern
-
-/**
- * IDL intent.
- *
- * @param origin File path, file name, method name or URL.
- * @param idl Original IDL of this intent.
- * @param id
- * @param options
- * @param meta
- * @param flow
- * @param terms
- */
-case class NCIdlIntent(
-    origin: String,
-    idl: String,
-    id: String,
-    options: NCIdlIntentOptions,
-    meta:  Map[String, Object],
-    flow: Option[String],
-    flowClsName: Option[String],
-    flowMtdName: Option[String],
-    terms: List[NCIdlTerm]
-):
-    require(id != null)
-    require(terms.nonEmpty)
-    require(meta != null)
-    require(options != null)
-
-    // Flow regex as a compiled pattern.
-    // Regex validity check is already done during intent compilation.
-    lazy val flowRegex: Option[Pattern] = flow match
-        case Some(r) => Option(Pattern.compile(r))
-        case None => None
-
-    lazy val isFlowDefined: Boolean = flow.isDefined || flowMtdName.isDefined
-
-    override def toString: String = idl
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlIntentOptions.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlIntentOptions.scala
deleted file mode 100644
index 3258feb..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlIntentOptions.scala
+++ /dev/null
@@ -1,18 +0,0 @@
-package org.apache.nlpcraft.internal.impl.intent
-
-class NCIdlIntentOptions:
-    var ignoreUnusedFreeWords: Boolean = true // Whether to ignore unused free words for intent match.
-    var ignoreUnusedSystemTokens: Boolean = true // Whether to ignore unused system tokens for intent match.
-    var ignoreUnusedUserTokens: Boolean = false // Whether to ignore unused user tokens for intent match.
-    var allowStmTokenOnly: Boolean = false // Whether or not to allow intent to match if all matching tokens came from STM only.
-    var ordered: Boolean = false // Whether or not the order of term is important for intent match.
-
-object NCIdlIntentOptions:
-    /*
-    * JSON field names.
-    */
-    final val JSON_UNUSED_FREE_WORDS = "unused_free_words"
-    final val JSON_UNUSED_SYS_TOKS = "unused_sys_toks"
-    final val JSON_UNUSED_USR_TOKS = "unused_usr_toks"
-    final val JSON_ALLOW_STM_ONLY = "allow_stm_only"
-    final val JSON_ORDERED = "ordered"
\ No newline at end of file
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlStack.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlStack.scala
deleted file mode 100644
index ffc3578..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlStack.scala
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nlpcraft.internal.impl.intent
-
-import scala.collection.mutable
-
-/**
-  *
-  */
-case class NCIdlStackItem (
-    value: Object,
-    tokUse: Int
-)
-
-object NCIdlStackItem:
-    def apply(v: Boolean, f: Int): NCIdlStackItem = new NCIdlStackItem(Boolean.box(v), f)
-    def apply(v: Long, f: Int): NCIdlStackItem = new NCIdlStackItem(Long.box(v), f)
-    def apply(v: Double, f: Int): NCIdlStackItem = new NCIdlStackItem(Double.box(v), f)
-/**
-  *
-  */
-trait NCIdlStackType extends (() => NCIdlStackItem)
-/**
-  *
-  */
-class NCIdlStack extends mutable.Stack[NCIdlStackType]:
-    /**
-      * Special marker for stack frames.
-      */
-    final val PLIST_MARKER: NCIdlStackType = () => NCIdlStackItem(null, 0)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlTerm.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlTerm.scala
deleted file mode 100644
index 405f36e..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/NCIdlTerm.scala
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nlpcraft.internal.impl.intent
-
-/**
- * IDL term.
- *
- * @param idl
- * @param id Optional ID of this term.
- * @param decls Term optional declarations.
- * @param pred Term predicate.
- * @param min
- * @param max
- * @param conv Whether or not this term support conversation context.
- * @param fragMeta
- */
-case class NCIdlTerm(
-    idl: String,
-    id: Option[String],
-    decls: Map[String, NCIdlFunction],
-    pred: NCIdlFunction,
-    min: Int,
-    max: Int,
-    conv: Boolean,
-    fragMeta: Map[String, Any] = Map.empty
-):
-    require(pred != null)
-    require(min >= 0 && max >= min)
-
-    /**
-     *
-     * @param meta
-     * @return
-     */
-    def cloneWithFragMeta(meta: Map[String, Any]): NCIdlTerm =
-        NCIdlTerm(
-            idl,
-            id,
-            decls,
-            pred,
-            min,
-            max,
-            conv,
-            meta
-        )
-
-    override def toString: String = idl
-
-    def toAnsiString: String = null//g(idl) // TODO: look at 0.9.1
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/compiler/NCIdlCompiler.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/compiler/NCIdlCompiler.scala
deleted file mode 100644
index ce538bd..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/impl/intent/compiler/NCIdlCompiler.scala
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nlpcraft.internal.impl.intent.compiler
-
-import org.apache.nlpcraft.*
-import org.apache.nlpcraft.internal.impl.intent.*
-
-/**
-  *
-  */
-object NCIdlCompiler:
-    /**
-      *
-      * @param idl
-      * @param mdl
-      * @param origin
-      * @return
-      */
-    def compileIntents(idl: String, mdl: NCModel, origin: String): Set[NCIdlIntent] = Set.empty // TODO:
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/opennlp/NCTestConfigJava.java b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/opennlp/NCTestConfigJava.java
index fed2a18..a25cf51 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/opennlp/NCTestConfigJava.java
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/opennlp/NCTestConfigJava.java
@@ -28,7 +28,7 @@ public class NCTestConfigJava {
     /**
      *
      */
-    public static final NCModelConfig CFG = new NCModelConfig("testId", "test", "1.0");
+    public static final NCModelConfig CFG = new NCModelConfig("testId", "test", "1.0", "Test description", "Test origin");
 
     /**
      *