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/07/24 23:01:33 UTC

[incubator-nlpcraft] branch NLPCRAFT-371 updated: WIP on code review & fixes for NLPCRAFT-371.

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

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


The following commit(s) were added to refs/heads/NLPCRAFT-371 by this push:
     new c57db77  WIP on code review & fixes for NLPCRAFT-371.
c57db77 is described below

commit c57db77a2df3cc81b7e2d0786fe03ba6b89f7689
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Sat Jul 24 16:01:19 2021 -0700

    WIP on code review & fixes for NLPCRAFT-371.
---
 .../nlpcraft/model/tools/cmdline/NCCli.scala       | 44 +++++++++++++++++--
 .../model/tools/cmdline/NCCliCommands.scala        | 50 ++++++++++++++++++++--
 .../nlpcraft/server/probe/NCProbeManager.scala     | 15 +++----
 .../nlpcraft/server/rest/NCBasicRestApi.scala      |  8 ++--
 .../nlpcraft/server/rest/NCRestModelSpec.scala     |  4 +-
 openapi/nlpcraft_swagger.yml                       | 17 +++++++-
 6 files changed, 113 insertions(+), 25 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index 401ec67..09cc21b 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -147,7 +147,8 @@ object NCCli extends NCCliBase {
     private final val REST_CMD = CMDS.find(_.name == "rest").get
     private final val CALL_CMD = CMDS.find(_.name == "call").get
     private final val ASK_CMD = CMDS.find(_.name == "ask").get
-    private final val SUGSYN_CMD = CMDS.find(_.name == "sugsyn").get
+    private final val MODEL_SUGSYN_CMD = CMDS.find(_.name == "model-sugsyn").get
+    private final val MODEL_SYNS_CMD = CMDS.find(_.name == "model-syns").get
 
     /**
      * @param cmd
@@ -549,6 +550,7 @@ object NCCli extends NCCliBase {
                     logln(r(" [Error]"))
 
                     error(s"Server start failed - check full log for errors: ${c(output.getAbsolutePath)}")
+                    error(s"If the problem persists - remove ${c("~/.nlpcraft")} folder and try again.")
 
                     tailFile(output.getAbsolutePath, 20)
                 }
@@ -1737,7 +1739,7 @@ object NCCli extends NCCliBase {
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
-    private [cmdline] def cmdSugSyn(cmd: Command, args: Seq[Argument], repl: Boolean): Unit =
+    private [cmdline] def cmdModelSugSyn(cmd: Command, args: Seq[Argument], repl: Boolean): Unit =
         state.accessToken match {
             case Some(acsTok) =>
                 val mdlId = getParamOpt(args, "mdlId") match {
@@ -1771,6 +1773,40 @@ object NCCli extends NCCliBase {
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
+    private [cmdline] def cmdModelSyns(cmd: Command, args: Seq[Argument], repl: Boolean): Unit =
+        state.accessToken match {
+            case Some(acsTok) =>
+                val mdlId = getParamOpt(args, "mdlId") match {
+                    case Some(id) => id
+                    case None =>
+                        if (state.probes.size == 1 && state.probes.head.models.length == 1)
+                            state.probes.head.models.head.id
+                        else
+                            throw MissingOptionalParameter(cmd, "mdlId")
+                }
+                val elmId = getParam(cmd, args, "elmId")
+
+                httpRest(
+                    cmd,
+                    "model/syns",
+                    s"""
+                       |{
+                       |    "acsTok": ${jsonQuote(acsTok)},
+                       |    "mdlId": ${jsonQuote(mdlId)},
+                       |    "elmId": ${jsonQuote(elmId)}
+                       |}
+                       |""".stripMargin
+                )
+
+            case None => throw NotSignedIn()
+        }
+
+    /**
+     *
+     * @param cmd Command descriptor.
+     * @param args Arguments, if any, for this command.
+     * @param repl Whether or not executing from REPL.
+     */
     private [cmdline] def cmdAsk(cmd: Command, args: Seq[Argument], repl: Boolean): Unit =
         state.accessToken match {
             case Some(acsTok) =>
@@ -2520,7 +2556,7 @@ object NCCli extends NCCliBase {
 
                                     mkCandidate(
                                         disp = name,
-                                        grp = s"REST ${cmd.group}:",
+                                        grp = MANDATORY_GRP,
                                         desc = cmd.desc,
                                         completed = true
                                     )
@@ -2530,7 +2566,7 @@ object NCCli extends NCCliBase {
                     }
 
                     // For 'ask' and 'sugysn' - add additional model IDs auto-completion/suggestion candidates.
-                    if (cmd == ASK_CMD.name || cmd == SUGSYN_CMD.name)
+                    if (cmd == ASK_CMD.name || cmd == MODEL_SUGSYN_CMD.name || cmd == MODEL_SYNS_CMD.name)
                         candidates.addAll(
                             state.probes.flatMap(_.models.toList).map(mdl => {
                                 mkCandidate(
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliCommands.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliCommands.scala
index 675fa49..d304b06 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliCommands.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliCommands.scala
@@ -542,7 +542,49 @@ private [cmdline] object NCCliCommands {
             )
         ),
         Command(
-            name = "sugsyn",
+            name = "model-syns",
+            group = "2. REST Commands",
+            synopsis = s"Wrapper for ${y("'/model/syns'")} REST call.",
+            desc = Some(
+                s"Requires user to be already signed in. This command ${bo("only makes sense in the REPL mode")} as " +
+                s"it requires user to be signed in. REPL session keeps the currently active access " +
+                s"token after user signed in. For command line mode, use ${y("'rest'")} command with " +
+                s"corresponding parameters. Note also that it requires a local probe running that hosts " +
+                s"the specified model."
+            ),
+            body = NCCli.cmdModelSyns,
+            params = Seq(
+                Parameter(
+                    id = "mdlId",
+                    names = Seq("--mdlId", "-m"),
+                    value = Some("model.id"),
+                    optional = true,
+                    desc =
+                        s"ID of the model to get synonyms for. " +
+                        s"In REPL mode, hit ${rv(" Tab ")} to see auto-suggestion for possible model IDs. Note that " +
+                        s"this is optional ONLY if there is only one connected probe and it has only one model deployed - which will be " +
+                        s"used by default. In all other cases - this parameter is mandatory."
+                ),
+                Parameter(
+                    id = "elmId",
+                    names = Seq("--elmId", "-e"),
+                    value = Some("element.id"),
+                    desc =
+                        s"ID of the model element to get synonyms for."
+                )
+            ),
+            examples = Seq(
+                Example(
+                    usage = Seq(
+                        s"""> model-syns -m=my.model.id -e=my:elem"""
+                    ),
+                    desc =
+                        s"Issues ${y("'model/syns'")} REST call with given model and element IDs."
+                )
+            )
+        ),
+        Command(
+            name = "model-sugsyn",
             group = "2. REST Commands",
             synopsis = s"Wrapper for ${y("'/model/sugsyn'")} REST call.",
             desc = Some(
@@ -553,7 +595,7 @@ private [cmdline] object NCCliCommands {
                 s"the specified model as well as running ${y("'ctxword'")} server. Find more information about " +
                 s"this tool at https://nlpcraft.apache.org/tools/syn_tool.html"
             ),
-            body = NCCli.cmdSugSyn,
+            body = NCCli.cmdModelSugSyn,
             params = Seq(
                 Parameter(
                     id = "mdlId",
@@ -577,14 +619,14 @@ private [cmdline] object NCCliCommands {
             examples = Seq(
                 Example(
                     usage = Seq(
-                        s"""> sugsyn -m=my.model.id"""
+                        s"""> model-sugsyn -m=my.model.id"""
                     ),
                     desc =
                         s"Issues ${y("'model/sugsyn'")} REST call with default min score and given model ID."
                 ),
                 Example(
                     usage = Seq(
-                        s"""> sugsyn"""
+                        s"""> model-sugsyn"""
                     ),
                     desc =
                         s"Issues ${y("'model/sugsyn'")} REST call with default min score and default model ID " +
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
index 8f402db..04792f6 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/probe/NCProbeManager.scala
@@ -1129,17 +1129,14 @@ object NCProbeManager extends NCService {
 
                     macros.asScala.foreach(t => parser.addMacro(t._1, t._2))
 
-                    val synsExpanded: java.util.List[String] =
-                        syns.asScala.flatMap(s => parser.expand(s).map(NCNlpPorterStemmer.stem)).asJava
+                    val synsExp: java.util.List[String] =
+                        syns.asScala.flatMap(s => parser.expand(s)).sorted.asJava
 
-                    val valsExpanded: java.util.Map[String, java.util.List[String]] =
-                        vals.asScala.map(v =>
-                            v._1 ->
-                            v._2.asScala.flatMap(s => parser.expand(s).map(NCNlpPorterStemmer.stem)).asJava
-                        ).toMap.asJava
+                    val valsExp: java.util.Map[String, java.util.List[String]] =
+                        vals.asScala.map(v => v._1 -> v._2.asScala.flatMap(s => parser.expand(s)).sorted.asJava).toMap.asJava
 
-                    res.put("synonymsExpanded", synsExpanded)
-                    res.put("valuesExpanded", valsExpanded)
+                    res.put("synonymsExp", synsExp)
+                    res.put("valuesExp", valsExp)
 
                     res
                 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
index e52923a..4dcb4ec 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
@@ -859,18 +859,18 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
                         case res =>
                             require(
                                 res.containsKey("synonyms") &&
-                                res.containsKey("synonymsExpanded") &&
+                                res.containsKey("synonymsExp") &&
                                 res.containsKey("values") &&
-                                res.containsKey("valuesExpanded")
+                                res.containsKey("valuesExp")
                             )
 
                             toJs(
                                 Map(
                                     "status" -> API_OK.toString,
                                     "synonyms" -> res.get("synonyms"),
-                                    "synonymsExpanded" -> res.get("synonymsExpanded"),
+                                    "synonymsExp" -> res.get("synonymsExp"),
                                     "values" -> res.get("values"),
-                                    "valuesExpanded" -> res.get("valuesExpanded")
+                                    "valuesExp" -> res.get("valuesExp")
                                 )
                             )
                     }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
index 62f7ce9..0b8c03f 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
@@ -94,10 +94,10 @@ class NCRestModelSpec2 extends NCRestSpec {
             post("model/syns", "mdlId" -> "rest.test.model", "elmId" -> elemId)(
                 ("$.status", (status: String) => assertEquals("API_OK", status)),
                 ("$.synonyms", (data: ResponseList) => assertTrue(!data.isEmpty)),
-                ("$.synonymsExpanded", (data: ResponseList) => assertTrue(!data.isEmpty)),
+                ("$.synonymsExp", (data: ResponseList) => assertTrue(!data.isEmpty)),
                 ("$.values", (data: java.util.Map[Object, Object]) =>
                     if (valsShouldBe) assertTrue(!data.isEmpty) else assertTrue(data.isEmpty)),
-                ("$.valuesExpanded", (data: java.util.Map[Object, Object]) =>
+                ("$.valuesExp", (data: java.util.Map[Object, Object]) =>
                     if (valsShouldBe) assertTrue(!data.isEmpty) else assertTrue(data.isEmpty)
                 )
             )
diff --git a/openapi/nlpcraft_swagger.yml b/openapi/nlpcraft_swagger.yml
index 2a5723b..76793f8 100644
--- a/openapi/nlpcraft_swagger.yml
+++ b/openapi/nlpcraft_swagger.yml
@@ -276,6 +276,8 @@ paths:
               - status
               - synonyms
               - values
+              - synonymsExp
+              - valuesExp
             properties:
               status:
                 type: string
@@ -284,14 +286,25 @@ paths:
                   - API_OK
               synonyms:
                 type: object
-                description: List of individual synomyms.
+                description: List of individual synomyms as they are specified in the model.
+              synonymsExp:
+                type: object
+                description: List of expanded synomyms as the model uses them (before stemmatization).
               values:
                 type: object
                 description: >-
-                  Map of element values, if any.
+                  Map of element values, if any, as they are specified in the model.
                   Map key is the value name.
                   Map value is a list of synonyms for that value.
                   Empty if element doesn't have values.
+              valuesExp:
+                type: object
+                description: >-
+                  Map of element values, if any, as the model uses them (before stemmatization).
+                  Map key is the value name.
+                  Map value is a list of expanded
+                  synonyms for that value.
+                  Empty if element doesn't have values.
         '400':
           description: Failed operation.
           schema: