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

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


The following commit(s) were added to refs/heads/NLPCRAFT-41 by this push:
     new df15c96  WIP.
df15c96 is described below

commit df15c96c07c2fd3fdfbb17b9dfa29df9c21dc8dc
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Aug 14 22:39:55 2020 +0300

    WIP.
---
 .../nlpcraft/server/rest/NCBasicRestApi.scala      |  6 +--
 .../server/suggestion/NCSuggestionsManager.scala   | 35 ++++++------
 openapi/nlpcraft_swagger.yml                       | 62 ++++++++++++++++++++++
 3 files changed, 84 insertions(+), 19 deletions(-)

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 0e67b02..41c5d58 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
@@ -630,7 +630,6 @@ class NCBasicRestApi extends NCRestApi with LazyLogging 
with NCOpenCensusTrace w
         case class Req(
             acsTok: String,
             mdlId: String,
-            limit: Option[Int],
             minScore: Option[Double]
         )
 
@@ -645,7 +644,7 @@ class NCBasicRestApi extends NCRestApi with LazyLogging 
with NCOpenCensusTrace w
             suggestions: Map[String, Seq[Suggestion]]
         )
 
-        implicit val reqFmt: RootJsonFormat[Req] = jsonFormat4(Req)
+        implicit val reqFmt: RootJsonFormat[Req] = jsonFormat3(Req)
         implicit val fbFmt: RootJsonFormat[Suggestion] = 
jsonFormat3(Suggestion)
         implicit val resFmt: RootJsonFormat[Res] = jsonFormat2(Res)
 
@@ -653,7 +652,6 @@ class NCBasicRestApi extends NCRestApi with LazyLogging 
with NCOpenCensusTrace w
             startScopedSpan(
                 "check$",
                 "mdlId" → req.mdlId,
-                "limit" → req.limit.getOrElse(() ⇒ null),
                 "minScore" → req.minScore.getOrElse(() ⇒ null),
                 "acsTok" → req.acsTok
             ) { span ⇒
@@ -666,7 +664,7 @@ class NCBasicRestApi extends NCRestApi with LazyLogging 
with NCOpenCensusTrace w
                     throw new NCE(s"Probe not found: ${req.mdlId}")
 
                 val res: Map[String, Seq[Suggestion]] =
-                    NCSuggestionsManager.suggest(req.mdlId, req.minScore, 
req.limit, span).
+                    NCSuggestionsManager.suggest(req.mdlId, req.minScore, 
span).
                         map { case (elemId, suggs) ⇒
                             elemId → suggs.map(p ⇒ Suggestion(p.suggestion, 
p.ctxWorldServerScore, p.suggestedCount))
                         }.toMap
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/suggestion/NCSuggestionsManager.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/suggestion/NCSuggestionsManager.scala
index a2256f1..0533f4e 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/suggestion/NCSuggestionsManager.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/suggestion/NCSuggestionsManager.scala
@@ -45,7 +45,9 @@ import scala.collection._
   */
 object NCSuggestionsManager extends NCService {
     private final val DFLT_LIMIT: Int = 20
+    private final val MAX_LIMIT: Int = 1000
     private final val DFLT_MIN_SCORE: Double = 0
+    private final val MIN_REQUIRED_CNT = 5
 
     private object Config extends NCConfigurable {
         val urlOpt: Option[String] = 
getStringOpt("nlpcraft.server.ctxword.url")
@@ -114,25 +116,28 @@ object NCSuggestionsManager extends NCService {
     /**
       *
       * @param mdlId Model ID.
-      * @param minScoreOpt Context word server minimal suggestion score 
(default DFLT_MIN_SCORE). Increase it for suggestions count increasing, 
decrease it to be more precise.
-      * @param limitOpt TODO: inverse parameter.
+      * @param minScore Context word server minimal suggestion score (default 
DFLT_MIN_SCORE).
+      * Increase it for suggestions count increasing, decrease it to be more 
precise. Range 0 ... 2.
       *
       * @param parent Parent.
       */
     @throws[NCE]
-    def suggest(mdlId: String, minScoreOpt: Option[Double], limitOpt: 
Option[Int], parent: Span = null): Map[String, Seq[NCSuggestion]] =
+    def suggest(mdlId: String, minScore: Option[Double], parent: Span = null): 
Map[String, Seq[NCSuggestion]] =
         startScopedSpan(
-            "suggest",
-            parent,
-            "modelId" → mdlId,
-            "minScore" → minScoreOpt.getOrElse(() ⇒ null),
-            "limit" → limitOpt.getOrElse(() ⇒ null)
+            "suggest", parent, "modelId" → mdlId, "minScore" → 
minScore.getOrElse(() ⇒ null)
         ) { _ ⇒
             val url = s"${Config.urlOpt.getOrElse(throw new NCE("Context word 
server is not configured"))}/suggestions"
 
             val mdl = NCProbeManager.getModel(mdlId)
 
-            require(mdl.intentsSamples != null && mdl.elementsSynonyms != null 
&& mdl.macros != null)
+            require(mdl.intentsSamples != null, "Samples cannot be null")
+            require(mdl.elementsSynonyms != null, "Element synonyms cannot be 
null")
+            require(mdl.macros != null, "Macros cannot be null")
+            require(mdl.intentsSamples.forall(_._2.nonEmpty), "Samples cannot 
be empty")
+
+            mdl.intentsSamples.
+                filter { case (_, samples) ⇒ samples.size < MIN_REQUIRED_CNT }.
+                foreach { case (intentId, _) ⇒ logger.warn(s"Intent has not 
enough samples: $intentId") }
 
             val parser = new NCMacroParser()
 
@@ -193,7 +198,7 @@ object NCSuggestionsManager extends NCService {
 
             val allReqsCnt = allReqs.map(_._2.size).sum
 
-            logger.debug(
+            logger.info(
                 s"Data prepared [examples=${examples.size}, 
synonyms=${elemSyns.map(_._2.size).sum}, requests=$allReqsCnt]"
             )
 
@@ -215,8 +220,8 @@ object NCSuggestionsManager extends NCService {
                                 GSON.toJson(
                                     RestRequest(
                                         sentences = batch.map(p ⇒ 
RestRequestSentence(p.sentence, Seq(p.index).asJava)).asJava,
-                                        min_score = 
minScoreOpt.getOrElse(DFLT_MIN_SCORE),
-                                        limit = limitOpt.getOrElse(DFLT_LIMIT) 
+ 1
+                                        min_score = 
minScore.getOrElse(DFLT_MIN_SCORE),
+                                        limit = (if (minScore.isDefined) 
MAX_LIMIT else DFLT_LIMIT) + 1
                                     )
                                 ),
                                 "UTF-8"
@@ -235,7 +240,7 @@ object NCSuggestionsManager extends NCService {
 
                         val i = cnt.addAndGet(batch.size)
 
-                        logger.debug(s"Executed: $i requests.")
+                        logger.info(s"Executed: $i requests...")
 
                         allSuggs.
                             computeIfAbsent(elemId, (_: String) ⇒ new 
CopyOnWriteArrayList[Suggestion]()).
@@ -293,7 +298,7 @@ object NCSuggestionsManager extends NCService {
                         }
                 }
 
-            logger.whenDebugEnabled({
+            logger.whenInfoEnabled({
                 var i = 1
 
                 debugs.groupBy(_._1.example).foreach { case (_, m) ⇒
@@ -303,7 +308,7 @@ object NCSuggestionsManager extends NCService {
                                 zipWithIndex.map { case (w, i) ⇒ if (i == 
req.index) s"<<<$w>>>" else w }.
                                 mkString(" ")
 
-                        logger.debug(
+                        logger.info(
                             s"$i. " +
                                 s"Request=$s, " +
                                 s"suggestions=[${suggs.map(_.word).mkString(", 
")}], " +
diff --git a/openapi/nlpcraft_swagger.yml b/openapi/nlpcraft_swagger.yml
index a20f1ee..7cba9f6 100644
--- a/openapi/nlpcraft_swagger.yml
+++ b/openapi/nlpcraft_swagger.yml
@@ -158,6 +158,68 @@ paths:
           description: Failed operation.
           schema:
             $ref: '#/definitions/Error'
+  /suggestion:
+    post:
+      tags:
+        - Suggestion
+      summary: Synonyms suggestions for model.
+      description: >-
+        TODO:
+      operationId: Suggestion
+      parameters:
+        - in: body
+          name: Payload body
+          description: JSON request.
+          required: true
+          schema:
+            type: object
+            required:
+              - acsTok
+              - mdlId
+            properties:
+              acsTok:
+                type: string
+                description: Access token obtain via '/signin' call
+                maxLength: 256
+              mdlId:
+                type: string
+                maxLength: 32
+                description: Data model ID for which elements synonyms will be 
suggested
+              minScore:
+                type: number
+                format: double
+                description: >-
+                  Minimal score. Range from 0 to 2. Default 0.
+      responses:
+        '200':
+          description: Successful operation.
+          schema:
+            type: object
+            required:
+              - status
+              - srvReqId
+            properties:
+              status:
+                type: string
+                description: Status code of this operation
+                enum:
+                  - API_OK
+              suggestions:
+                type: object
+                description: Synonyms suggestions for given model
+                required:
+                  - acsTok
+                  - mdlId
+                properties:
+                  acsTok:
+                    type: string
+                    description: Access token obtain via '/signin' call
+                    maxLength: 256
+
+        '400':
+          description: Failed operation.
+          schema:
+            $ref: '#/definitions/Error'
   /check:
     post:
       tags:

Reply via email to