WIP.
diff --git a/nlpcraft-examples/sql/src/main/java/org/apache/nlpcraft/examples/sql/SqlModel.scala b/nlpcraft-examples/sql/src/main/java/org/apache/nlpcraft/examples/sql/SqlModel.scala
index 6e5753f..181ae8b 100644
--- a/nlpcraft-examples/sql/src/main/java/org/apache/nlpcraft/examples/sql/SqlModel.scala
+++ b/nlpcraft-examples/sql/src/main/java/org/apache/nlpcraft/examples/sql/SqlModel.scala
@@ -421,7 +421,7 @@
def isColumn(t: NCToken): Boolean = findAnyColumnTokenOpt(t).isDefined
def isDate(t: NCToken): Boolean = t.getId == "nlpcraft:date"
- val ok = toks.forall(isValue) || toks.size == 1 && isDate(toks.head)
+ val ok = toks.forall(isValue) || toks.forall(isColumn) || toks.size == 1 && isDate(toks.head)
if (!ok) {
m.getContext.getConversation.clearStm(_ => true)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
index 1bae8f7..4031ca9 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolver.scala
@@ -27,10 +27,10 @@
import org.apache.nlpcraft.model.intent.NCIdlIntent
import org.apache.nlpcraft.model.{NCContext, NCIntentMatch, NCIntentSkip, NCModel, NCRejection, NCResult, NCToken, NCVariant}
import org.apache.nlpcraft.probe.mgrs.dialogflow.NCDialogFlowManager
+import org.apache.nlpcraft.probe.mgrs.sentence.NCSentenceManager
import java.util.{List => JList}
-
-import scala.jdk.CollectionConverters.SeqHasAsJava
+import scala.jdk.CollectionConverters.{ListHasAsScala, SeqHasAsJava}
/**
* Front-end for intent solver.
@@ -95,10 +95,24 @@
for (res <- results if res != null) {
try {
i += 1
-
+
+ val allConvToks = ctx.getConversation.getTokens.asScala
+ val nonConvToks = res.groups.flatMap(_.tokens).filterNot(allConvToks.contains)
+
+ val intentToks =
+ res.groups.map(_.tokens).map(toks => {
+ toks.filter(allConvToks.contains).foreach(convTok =>
+ NCSentenceManager.fixMeta(convTok, nonConvToks, allConvToks)
+ )
+
+ toks
+ })
+
+ ctx.getConversation.getTokens
+
val intentMatch: NCIntentMatch = new NCMetadataAdapter with NCIntentMatch {
override val getContext: NCContext = ctx
- override val getIntentTokens: JList[JList[NCToken]] = res.groups.map(_.tokens.asJava).asJava
+ override val getIntentTokens: JList[JList[NCToken]] = intentToks.map(_.asJava).asJava
override val getVariant: NCVariant = new NCVariantImpl(res.variant.tokens)
override val isAmbiguous: Boolean = !res.isExactMatch
override val getIntentId: String = res.intentId
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
index 54d6922..c6510b5 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/solver/NCIntentSolverEngine.scala
@@ -357,12 +357,12 @@
sorted.map(m =>
NCIntentSolverResult(
- m.intentMatch.intent.id,
- m.callback,
- m.intentMatch.tokenGroups.map(grp => NCIntentTokensGroup(grp.term.id, grp.usedTokens.map(_.token))),
- m.intentMatch.exactMatch,
- m.variant,
- m.variantIdx
+ intentId = m.intentMatch.intent.id,
+ fn = m.callback,
+ groups = m.intentMatch.tokenGroups.map(grp => NCIntentTokensGroup(grp.term.id, grp.usedTokens.map(_.token))),
+ isExactMatch = m.intentMatch.exactMatch,
+ variant = m.variant,
+ variantIdx = m.variantIdx
)
).toList
}
@@ -501,10 +501,10 @@
)
solveTerm(
- term,
- termCtx,
- senToks,
- if (term.conv) convToks else Seq.empty
+ term = term,
+ ctx = termCtx,
+ senToks = senToks,
+ convToks = if (term.conv) convToks else Seq.empty
) match {
case Some(termMatch) =>
if (ordered && lastTermMatch != null && lastTermMatch.maxIndex > termMatch.maxIndex)
@@ -702,16 +702,13 @@
// Sum of conversation depths for each token from the conversation.
// Negated to make sure that bigger (smaller negative number) is better.
- val convDepthsSum = -usedToks.filter(t => convSrvReqIds.contains(t.token.getServerRequestId)).zipWithIndex.map(_._2 + 1).sum
+ val convDepthsSum =
+ -usedToks.filter(t => convSrvReqIds.contains(t.token.getServerRequestId)).zipWithIndex.map(_._2 + 1).sum
// Mark found tokens as used.
usedToks.foreach(_.used = true)
- Some(usedToks -> new Weight(
- senTokNum,
- convDepthsSum,
- tokUses
- ))
+ Some(usedToks -> new Weight(senTokNum, convDepthsSum, tokUses))
}
}
}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala
index 74ead87..115d7f9 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSentenceManager.scala
@@ -22,7 +22,7 @@
import org.apache.nlpcraft.common.nlp.pos.NCPennTreebank
import org.apache.nlpcraft.common.nlp.{NCNlpSentence, NCNlpSentenceNote, NCNlpSentenceToken}
import org.apache.nlpcraft.common.{NCE, NCService, U, _}
-import org.apache.nlpcraft.model.NCModel
+import org.apache.nlpcraft.model.{NCModel, NCToken}
import org.apache.nlpcraft.probe.mgrs.NCTokenPartKey
import java.io.{Serializable => JSerializable}
@@ -43,7 +43,6 @@
type CacheValue = Seq[Seq[NCNlpSentenceNote]]
private val combCache = mutable.HashMap.empty[String, mutable.HashMap[CacheKey, CacheValue]]
-
/**
*
* @param notes
@@ -791,4 +790,57 @@
* @param srvReqId
*/
def clearCache(srvReqId: String): Unit = combCache -= srvReqId
+
+
+
+ /**
+ *
+ * @param convTok
+ * @param nonConvToks
+ * @param allConvToks
+ */
+ def fixMeta(convTok: NCToken, nonConvToks: Seq[NCToken], allConvToks: Seq[NCToken]): Unit =
+ convTok.getId match {
+ case "nlpcraft:sort" =>
+ def fix(notesName: String, idxsName: String): Unit = {
+ val notes = convTok.meta[JList[String]](s"nlpcraft:sort:$notesName")
+ val idxs = convTok.meta[JList[Int]](s"nlpcraft:sort:$idxsName")
+
+ require(notes == null && idxs == null || notes.size() == idxs.size())
+
+ if (notes != null && !notes.isEmpty) {
+ val data: Seq[(String, Int)] =
+ notes.asScala.zip(idxs.asScala).map { case (note, idx) =>
+ nonConvToks.find(t => t.getId == note && t.getIndex == idx) match {
+ case Some(_) => (note, idx)
+ case None =>
+ val ref =
+ allConvToks.
+ find(t => t.getId == note && t.getIndex == idx).
+ getOrElse(
+ throw new NCE(s"Reference is not found [note=$note, index=$idx]")
+ )
+
+ val newRef =
+ nonConvToks.
+ find(t =>
+ t.getGroups.asScala.toSet.intersect(ref.getGroups.asScala.toSet).nonEmpty
+ ).
+ getOrElse(
+ throw new NCE(s"New reference is not found [note=$note, index=$idx]")
+ )
+
+ (newRef.getId, newRef.getIndex)
+ }
+ }
+
+ convTok.getMetadata.put(s"nlpcraft:sort:$notesName", data.map(_._1).asJava)
+ convTok.getMetadata.put(s"nlpcraft:sort:$idxsName", data.map(_._2).asJava)
+ }
+ }
+
+ fix("bynotes", "byindexes")
+ fix("subjnotes", "subjindexes")
+ case _ => // TODO: implement all other.
+ }
}
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala
index be94a42..e28d4e5 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmIndexesTestModelSpec.scala
@@ -75,14 +75,11 @@
println(s"variant userDefinedTokens: ${ctx.getVariant.getUserDefinedTokens.asScala.map(toStr).mkString("|")}")
println(s"variant conversation: ${ctx.getContext.getConversation.getTokens.asScala.map(toStr).mkString("|")}")
- val bynotes = sort.meta[JList[String]]("nlpcraft:sort:bynotes")
- val byindexes = sort.meta[JList[Int]]("nlpcraft:sort:byindexes")
-
NCResult.json(
mapper.writeValueAsString(
NCStmIndexesTestModelData(
- bynotes = bynotes.asScala.toSeq,
- byindexes = byindexes.asScala.toSeq
+ bynotes = sort.meta[JList[String]]("nlpcraft:sort:bynotes").asScala.toSeq,
+ byindexes = sort.meta[JList[Int]]("nlpcraft:sort:byindexes").asScala.toSeq
)
)
)
@@ -103,7 +100,7 @@
checkResult(
"b b",
extract,
- NCStmIndexesTestModelData(bynotes = Seq("A"), byindexes = Seq(3))
+ NCStmIndexesTestModelData(bynotes = Seq("B"), byindexes = Seq(0))
)
}
}
\ No newline at end of file