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 49de5c6 WIP.
49de5c6 is described below
commit 49de5c613825cc9939d7ab51c6840131079072ac
Author: Sergey Kamov <[email protected]>
AuthorDate: Tue Aug 25 20:34:33 2020 +0300
WIP.
---
...nhanceResponse.scala => NCEnhanceElement.scala} | 4 +-
.../nlpcraft/server/model/NCEnhanceManager.scala | 59 ++++++++++++++++++----
.../server/model/NCEnhanceSynonymsSuggestion.scala | 32 ------------
.../nlpcraft/server/model/NCEnhanceType.scala | 1 +
4 files changed, 53 insertions(+), 43 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceResponse.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceElement.scala
similarity index 87%
rename from
nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceResponse.scala
rename to
nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceElement.scala
index d77b6cd..aa6c3ad 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceResponse.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceElement.scala
@@ -22,9 +22,11 @@ import
org.apache.nlpcraft.server.model.NCEnhanceType.NCEnhanceType
/**
* TODO:
*/
-case class NCEnhanceResponse(
+case class NCEnhanceElement(
enhanceType: NCEnhanceType,
errors: Option[Seq[String]] = None,
warnings: Option[Seq[String]] = None,
+ // Note that `suggestions` should be simple type or java collections.
+ // Scala collections cannot be converted into JSON.
suggestions: Option[AnyRef] = None
)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceManager.scala
index 94fe1d0..6df4fa9 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceManager.scala
@@ -17,6 +17,7 @@
package org.apache.nlpcraft.server.model
+import java.util
import java.util.concurrent.atomic.{AtomicInteger, AtomicReference}
import java.util.concurrent.{ConcurrentHashMap, CopyOnWriteArrayList,
CountDownLatch, TimeUnit}
import java.util.{List ⇒ JList}
@@ -88,6 +89,12 @@ object NCEnhanceManager extends NCService {
)
}
+ case class SuggestionResult(
+ synonym: String,
+ ctxWorldServerScore: Double,
+ suggestedCount: Int
+ )
+
case class Response(
errors: Option[Seq[String]] = None,
warnings: Option[Seq[String]] = None,
@@ -147,8 +154,8 @@ object NCEnhanceManager extends NCService {
* @param typ
* @param resp
*/
- private def convert(typ: NCEnhanceType, resp: Response): NCEnhanceResponse
=
- NCEnhanceResponse(typ, resp.errors, resp.warnings, resp.suggestions)
+ private def convert(typ: NCEnhanceType, resp: Response): NCEnhanceElement =
+ NCEnhanceElement(typ, resp.errors, resp.warnings, resp.suggestions)
/**
*
@@ -348,7 +355,7 @@ object NCEnhanceManager extends NCService {
val nonEmptySuggs = allSuggs.asScala.map(p ⇒ p._1 →
p._2.asScala).filter(_._2.nonEmpty)
- val res = mutable.HashMap.empty[String,
mutable.ArrayBuffer[NCEnhanceSynonymsSuggestion]]
+ val res = mutable.HashMap.empty[String,
mutable.ArrayBuffer[SuggestionResult]]
nonEmptySuggs.
foreach { case (elemId, elemSuggs) ⇒
@@ -367,19 +374,19 @@ object NCEnhanceManager extends NCService {
map { case (sugg, cnt) ⇒ (sugg, cnt, sugg.score * cnt
/ elemSuggs.size) }.
sortBy { case (_, _, sumFactor) ⇒ -sumFactor }.
zipWithIndex.
- foreach { case ((sugg, cnt, sumFactor), _) ⇒
+ foreach { case ((sugg, cnt, _), _) ⇒
val seq =
res.get(elemId) match {
case Some(seq) ⇒ seq
case None ⇒
- val buf =
mutable.ArrayBuffer.empty[NCEnhanceSynonymsSuggestion]
+ val buf =
mutable.ArrayBuffer.empty[SuggestionResult]
res += elemId → buf
buf
}
- seq += NCEnhanceSynonymsSuggestion(sugg.word,
sugg.score, cnt, sumFactor)
+ seq += SuggestionResult(sugg.word, sugg.score, cnt)
}
}
@@ -407,7 +414,19 @@ object NCEnhanceManager extends NCService {
Response(
warnings = norm(warns),
- suggestions = Some(res.map(p ⇒ p._1 → p._2.asJava).asJava)
+ suggestions = Some(
+ res.map { case (id, data) ⇒
+ id → data.map(d ⇒ {
+ val m = new util.HashMap[String, Any]()
+
+ m.put("synonym", d.synonym)
+ m.put("ctxWorldServerScore", d.ctxWorldServerScore)
+ m.put("suggestedCount", d.suggestedCount)
+
+ m
+ }).asJava
+ }.asJava
+ )
)
}
@@ -468,18 +487,38 @@ object NCEnhanceManager extends NCService {
/**
*
* @param mdlId
+ * @param parent
+ */
+ private def validateIntents(mdlId: String, parent: Span = null): Response =
+ startScopedSpan("validateIntents", parent, "modelId" → mdlId) { _ ⇒
+ val mdl = NCProbeManager.getModel(mdlId)
+ val syns = mdl.elementsSynonyms.values.flatten
+
+ Response(warnings =
+ norm(
+ mdl.macros.keys.
+ // TODO: is it valid check?
+ flatMap(m ⇒ if (syns.exists(_.contains(m))) None else
Some(s"Macro is not used: $m")).
+ toSeq
+ )
+ )
+ }
+
+
+ /**
+ *
+ * @param mdlId
* @param types
* @param parent
*/
@throws[NCE]
- def enhance(mdlId: String, types: Seq[NCEnhanceType], parent: Span =
null): Seq[NCEnhanceResponse] =
+ def enhance(mdlId: String, types: Seq[NCEnhanceType], parent: Span =
null): Seq[NCEnhanceElement] =
startScopedSpan("enhance", parent, "modelId" → mdlId) { _ ⇒
- // Note that NCEnhanceResponse#suggestions should be simple types
or java collections.
- // Scala collections cannot be simple converted into JSON (REST
calls)
types.map {
case t@SUGGEST_SYNONYMS ⇒ convert(t, suggestSynonyms(mdlId,
parent))
case t@VALIDATION_MACROS ⇒ convert(t, validateMacros(mdlId,
parent))
case t@VALIDATION_SYNONYMS ⇒ convert(t,
validateSynonyms(mdlId, parent))
+ case t@VALIDATION_INTENTS ⇒ convert(t, validateIntents(mdlId,
parent))
}
}
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceSynonymsSuggestion.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceSynonymsSuggestion.scala
deleted file mode 100644
index 661d423..0000000
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceSynonymsSuggestion.scala
+++ /dev/null
@@ -1,32 +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
- *
- * http://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.server.model
-
-/**
- * TODO:
- * @param synonym
- * @param ctxWorldServerScore
- * @param suggestedCount
- * @param totalScore
- */
-case class NCEnhanceSynonymsSuggestion(
- synonym: String,
- ctxWorldServerScore: Double,
- suggestedCount: Int,
- totalScore: Double
-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceType.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceType.scala
index a549e00..147a638 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceType.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/model/NCEnhanceType.scala
@@ -23,4 +23,5 @@ object NCEnhanceType extends Enumeration {
val SUGGEST_SYNONYMS = Value
val VALIDATION_MACROS = Value
val VALIDATION_SYNONYMS = Value
+ val VALIDATION_INTENTS = Value
}