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 503c579  WIP.
503c579 is described below

commit 503c57937615517d270d57b305a99b98d8da60e0
Author: Sergey Kamov <[email protected]>
AuthorDate: Sat Apr 25 16:49:57 2020 +0300

    WIP.
---
 .../model/tools/synonyms/NCSynonymsGenerator.scala | 38 ++++++++--------------
 1 file changed, 14 insertions(+), 24 deletions(-)

diff --git 
a/src/main/scala/org/apache/nlpcraft/model/tools/synonyms/NCSynonymsGenerator.scala
 
b/src/main/scala/org/apache/nlpcraft/model/tools/synonyms/NCSynonymsGenerator.scala
index 484ac7f..259b98c 100644
--- 
a/src/main/scala/org/apache/nlpcraft/model/tools/synonyms/NCSynonymsGenerator.scala
+++ 
b/src/main/scala/org/apache/nlpcraft/model/tools/synonyms/NCSynonymsGenerator.scala
@@ -111,7 +111,11 @@ case class NCSynonymsGenerator(url: String, modelPath: 
String, minFactor: Double
 
         val elemSyns = mdl.getElements.asScala.map(e ⇒ e.getId → 
e.getSynonyms.asScala.flatMap(parser.expand)).toMap
 
-        val cache = mutable.HashMap.empty[String, Seq[Suggestion]]
+        val cache = mutable.HashMap.empty[String, Seq[Suggestion]].withDefault(
+            new (String ⇒ Seq[Suggestion]) {
+                override def apply(sen: String): Seq[Suggestion] = ask(client, 
sen).filter(_.score.toDouble >= minFactor)
+            }
+        )
 
         val allSuggs =
             elemSyns.map {
@@ -135,29 +139,15 @@ case class NCSynonymsGenerator(url: String, modelPath: 
String, minFactor: Double
                                 )
 
                             if (idxs.nonEmpty)
-                                stemsSyns.map(_._2).flatMap(syn ⇒ {
-                                    val wordsTxt =
-                                        exWords.zipWithIndex.map {
-                                            case (word, idx) ⇒ if 
(idxs.contains(idx)) syn else word.word
-                                        }
-
-                                    idxs.flatMap(idx ⇒ {
-                                        val sen =
-                                            wordsTxt.zipWithIndex.map {
-                                                case (word, wordIdx) ⇒ if 
(wordIdx == idx) s"$word#" else word
-                                            }.mkString(" ")
-
-                                        cache.get(sen) match {
-                                            case Some(res) ⇒ res
-                                            case None ⇒
-                                                val res = ask(client, 
sen).filter(_.score.toDouble >= minFactor)
-
-                                                cache += sen → res
-
-                                                res
-                                        }
-                                    })
-                                })
+                                stemsSyns.map(_._2).flatMap(syn ⇒
+                                    idxs.flatMap(idx ⇒
+                                        cache(
+                                            exWords.
+                                            zipWithIndex.map { case (w, i1) ⇒ 
if (idxs.contains(i1)) syn else w.word }.
+                                            zipWithIndex.map { case (w, i2) ⇒ 
if (i2 == idx) s"$w#" else w}.
+                                            mkString(" "))
+                                    )
+                                )
                             else
                                 Seq.empty
                         })

Reply via email to