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 2021/09/23 05:54:46 UTC
[incubator-nlpcraft] 01/02: WIP.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-443-1
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 218541af7c4431294cac5a5470c41924e0589ccb
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Sep 22 17:54:46 2021 +0300
WIP.
---
.../probe/mgrs/sentence/NCSynonymsManager.scala | 94 +++++++++-------------
1 file changed, 36 insertions(+), 58 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSynonymsManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSynonymsManager.scala
index 81e9132..cf5eb5d 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSynonymsManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/sentence/NCSynonymsManager.scala
@@ -32,10 +32,16 @@ import scala.collection.mutable
*
*/
object NCSynonymsManager extends NCService {
- type IdlCacheKey = (NCToken, String)
+ case class Key(token: NCToken) {
+ // NCToken hashCode and equals based on indexes. // TODO: check it!
+ override def hashCode(): Int = U.mkJavaHash(token.getId, token)
+ override def equals(obj: Any): Boolean = obj match {
+ case key: Key => key.token.getId == token.getId && key.token == token
+ }
+ }
+ case class Value(request: NCRequest, variants: Seq[Seq[NCToken]], predicate: NCIdlFunction)
- private val reqCache = mutable.HashMap.empty[String, NCRequest]
- private val idlCache = mutable.HashMap.empty[String, mutable.HashMap[IdlCacheKey, NCIdlFunction]]
+ private val idlCache = mutable.HashMap.empty[String, mutable.HashMap[Key, Value]]
override def start(parent: Span): NCService = {
ackStarting()
@@ -139,26 +145,12 @@ object NCSynonymsManager extends NCService {
*
* @param req
* @param tok
- * @param idlPred
+ * @param pred
+ * @param variantsToks
*/
- private def savePredicate(req: NCRequest, tok: NCToken, idlPred: NCIdlFunction): Unit = {
- val srvReqId = req.getServerRequestId
-
- reqCache += srvReqId -> req
-
- val idlCacheReq: mutable.Map[IdlCacheKey, NCIdlFunction] =
- idlCache.get(srvReqId) match {
- case Some(m) => m
- case None =>
- val m = mutable.HashMap.empty[IdlCacheKey, NCIdlFunction]
-
- idlCache += srvReqId -> m
-
- m
- }
-
- idlCacheReq += (tok, tok.getId) -> idlPred
- }
+ private def save(req: NCRequest, tok: NCToken, pred: NCIdlFunction, variantsToks: Seq[Seq[NCToken]]): Unit =
+ idlCache.getOrElseUpdate(req.getServerRequestId, mutable.HashMap.empty) +=
+ Key(tok) -> Value(req, variantsToks, pred)
/**
*
@@ -183,17 +175,15 @@ object NCSynonymsManager extends NCService {
case IDL =>
val ok =
- variantsToks.exists(variantToks =>
+ variantsToks.exists(vrntToks =>
get0(t =>
- chunk.idlPred.apply(
- t,
- NCIdlContext(req = req, toks = variantToks)
- ).value.asInstanceOf[Boolean], _ => false
+ chunk.idlPred.apply(t, NCIdlContext(toks = vrntToks, req = req)).value.asInstanceOf[Boolean],
+ _ => false
)
)
if (ok)
- savePredicate(req, tow.swap.toOption.get, chunk.idlPred)
+ save(req, tow.swap.toOption.get, chunk.idlPred, variantsToks)
ok
@@ -277,44 +267,32 @@ object NCSynonymsManager extends NCService {
* @param toks
* @return
*/
- def isStillValid(srvReqId: String, toks: Seq[NCToken]): Boolean = {
- val reqData = reqCache.get(srvReqId)
- val idlData = idlCache.get(srvReqId)
-
- require(reqData.isDefined && idlData.isDefined || reqData.isEmpty && idlData.isEmpty)
-
- if (reqData.isDefined) {
- val req: NCRequest = reqData.get
- val idl: Map[IdlCacheKey, NCIdlFunction] = idlData.get.toMap
+ def isStillValid(srvReqId: String, toks: Seq[NCToken]): Boolean =
+ toks.forall(tok =>
+ idlCache.get(srvReqId) match {
+ case Some(m) =>
+ m.get(Key(tok)) match {
+ case Some(v) =>
- toks.forall(tok =>
- idl.get((tok, tok.getId)) match {
- case Some(f) =>
- val x =
- f.apply(
- tok, NCIdlContext(req = req, toks = toks)
- ).value.asInstanceOf[Boolean]
+ val x =
+ v.predicate.apply(
+ tok, NCIdlContext(req = v.request, toks = toks)
+ ).value.asInstanceOf[Boolean]
- if (!x)
- println("x="+x + ", t=" + tok + ", toks=" + toks)
- x
+ if (!x)
+ println("x="+x + ", t=" + tok + ", toks=" + toks)
- case None => true
- }
- )
- }
- else
- true
- }
+ x
+ case None => true
+ }
+ case None => true
+ })
/**
*
* @param srvReqId
*/
- def clearRequestData(srvReqId: String): Unit = {
- reqCache -= srvReqId
- idlCache -= srvReqId
- }
+ def clearRequestData(srvReqId: String): Unit = idlCache -= srvReqId
}