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 2022/09/20 13:34:25 UTC

[incubator-nlpcraft] branch NLPCRAFT-515 updated: Tests minor fixes.

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

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


The following commit(s) were added to refs/heads/NLPCRAFT-515 by this push:
     new 54922403 Tests minor fixes.
54922403 is described below

commit 549224030043206289b3470e05c24025a2ec95ca
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Tue Sep 20 16:34:18 2022 +0300

    Tests minor fixes.
---
 .../src/main/scala/org/apache/nlpcraft/NCModelClient.scala |  3 +--
 .../apache/nlpcraft/internal/impl/NCModelClientSpec4.scala | 11 +++--------
 .../nlpcraft/internal/impl/NCPipelineManagerSpec.scala     | 10 +---------
 .../internal/intent/compiler/NCIDLFragmentsSpec.scala      | 14 +++++++-------
 .../scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala |  4 +---
 .../scala/org/apache/nlpcraft/nlp/util/NCTestConfig.scala  | 10 +++++++---
 .../org/apache/nlpcraft/nlp/util/NCTestModelAdapter.scala  |  2 +-
 7 files changed, 21 insertions(+), 33 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
index fa0d996f..cf007ae8 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCModelClient.scala
@@ -65,8 +65,7 @@ class NCModelClient(mdl: NCModel) extends LazyLogging, AutoCloseable:
         require(cfg.getName != null, "Model name cannot be null.")
         require(cfg.getVersion != null, "Model version cannot be null.")
         require(pipeline.getTokenParser != null, "Token parser cannot be null.")
-        require(pipeline.getEntityParsers != null, "List of entity parsers in the pipeline cannot be null.")
-
+        require(pipeline.getEntityParsers != null && pipeline.getEntityParsers.nonEmpty, "List of entity parsers in the pipeline cannot be null or empty.")
     /**
       *
       */
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala
index 43b2430c..80141f29 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCModelClientSpec4.scala
@@ -28,12 +28,8 @@ import org.scalatest.funsuite.AnyFunSuite
   */
 class NCModelClientSpec4 extends AnyFunSuite:
     test("test") {
-        val pl = mkEnPipeline
-
         //  For intents matching, we have to add at least one entity parser.
-        pl.entParsers += new NCNLPEntityParser
-
-        val mdl: NCModel = new NCModelAdapter(CFG, pl) :
+        val mdl: NCModelAdapter = new NCModelAdapter(CFG, mkEnPipeline(new NCNLPEntityParser)):
             @NCIntent("intent=i term(any)={true}")
             def onMatch(ctx: NCContext, im: NCIntentMatch): NCResult = TEST_RESULT
 
@@ -48,9 +44,8 @@ class NCModelClientSpec4 extends AnyFunSuite:
             () => client.clearDialog("userId", _ => true)
         )
 
-        for (call <- allCalls) call.apply()
-
-        client.close()
+        try for (call <- allCalls) call.apply()
+        finally client.close()
 
         for (call <- allCalls ++ Seq(() => client.close()))
             try
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCPipelineManagerSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCPipelineManagerSpec.scala
index a9f44005..ca29fe52 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCPipelineManagerSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/impl/NCPipelineManagerSpec.scala
@@ -23,10 +23,6 @@ import org.apache.nlpcraft.nlp.util.*
 import org.scalatest.funsuite.AnyFunSuite
 import org.scalatest.*
 
-
-import java.util
-import java.util.concurrent.*
-
 /**
   *
   */
@@ -36,11 +32,7 @@ class NCPipelineManagerSpec extends AnyFunSuite:
       */
     test("test") {
         def test(txt: String, variantCnt: Int, elements: NCSemanticElement*): Unit =
-            val pipeline = mkEnPipeline
-
-            pipeline.entParsers += NCTestUtils.mkEnSemanticParser(elements*)
-
-            val res = new NCModelPipelineManager(CFG, pipeline).prepare(txt, null, "userId")
+            val res = new NCModelPipelineManager(CFG, mkEnPipeline(NCTestUtils.mkEnSemanticParser(elements*))).prepare(txt, null, "userId")
 
             println(s"Variants count: ${res.variants.size}")
             for ((v, idx) <- res.variants.zipWithIndex)
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsSpec.scala
index f34fbc74..6c526667 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/internal/intent/compiler/NCIDLFragmentsSpec.scala
@@ -31,21 +31,21 @@ class NCIDLFragmentsSpec extends AnyFunSuite:
     // Fragment. One annotations order.
     @NCIntent("fragment=f term(x)~{# == 'x1'}")
     @NCIntent("intent=i1 term~{# == 'x2'} fragment(f)")
-    class M1 extends NCModelAdapter(CFG, mkEnPipeline)
+    class M1 extends NCModelAdapter(CFG, mkEmptyEnPipeline)
 
     // Fragment. Another annotations order.
     @NCIntent("intent=i1 term~{# == 'x2'} fragment(f)")
     @NCIntent("fragment=f term(x)~{# == 'x1'}")
-    class M2 extends NCModelAdapter(CFG, mkEnPipeline)
+    class M2 extends NCModelAdapter(CFG, mkEmptyEnPipeline)
 
     // Fragment. Reference from method to class.
     @NCIntent("fragment=f term(x)~{# == 'x1'}")
-    class M3 extends NCModelAdapter(CFG, mkEnPipeline):
+    class M3 extends NCModelAdapter(CFG, mkEmptyEnPipeline):
         @NCIntent("intent=i1 term~{# == 'x2'} fragment(f)")
         private def m(ctx: NCContext, im: NCIntentMatch): NCResult = null
 
     // Fragment. Reference from method (inside).
-    class M4 extends NCModelAdapter(CFG, mkEnPipeline) :
+    class M4 extends NCModelAdapter(CFG, mkEmptyEnPipeline) :
         @NCIntent("fragment=f term(x)~{# == 'x1'} intent=i1 term~{# == 'x2'} fragment(f)")
         private def m(ctx: NCContext, im: NCIntentMatch): NCResult = null
 
@@ -53,10 +53,10 @@ class NCIDLFragmentsSpec extends AnyFunSuite:
 
     // Missed fragment definition.
     @NCIntent("intent=i2 term~{# == 'x2'} fragment(f)")
-    class E1 extends NCModelAdapter(CFG, mkEnPipeline)
+    class E1 extends NCModelAdapter(CFG, mkEmptyEnPipeline)
 
     // Attempt to reference on fragment defined in method.
-    class E2 extends NCModelAdapter(CFG, mkEnPipeline):
+    class E2 extends NCModelAdapter(CFG, mkEmptyEnPipeline):
         @NCIntent("fragment=f term(x)~{# == 'x1'} intent=i1 term~{# == 'x2'} fragment(f)")
         private def m1(ctx: NCContext, im: NCIntentMatch): NCResult = null
 
@@ -64,7 +64,7 @@ class NCIDLFragmentsSpec extends AnyFunSuite:
         private def m2(ctx: NCContext, im: NCIntentMatch): NCResult = null
 
     // Attempt to reference on fragment defined in method.
-    class E3 extends NCModelAdapter(CFG, mkEnPipeline):
+    class E3 extends NCModelAdapter(CFG, mkEmptyEnPipeline):
         @NCIntent("fragment=f term(x)~{# == 'x1'} intent=i1 term~{# == 'x2'} fragment(f)")
         private def m2(ctx: NCContext, im: NCIntentMatch): NCResult = null
 
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
index 893c0f3e..754dcd91 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/NCEntityMapperSpec.scala
@@ -50,11 +50,9 @@ class NCEntityMapperSpec extends AnyFunSuite:
     private val mdl = new NCTestModelAdapter:
         override val getPipeline: NCPipeline =
             import NCSemanticTestElement as TE
-            val pl = mkEnPipeline
+            val pl = mkEnPipeline(TE("a"), TE("b"), TE("c"), TE("d"))
             val ms = pl.entMappers
 
-            pl.entParsers += NCTestUtils.mkEnSemanticParser(TE("a"), TE("b"), TE("c"), TE("d"))
-
             // Replaces [a, b] -> [ab]
             ms += Combiner("a", "b")
             // Replaces [c, d] -> [cd]
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestConfig.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestConfig.scala
index e15c74f9..545a6403 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestConfig.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestConfig.scala
@@ -17,7 +17,7 @@
 
 package org.apache.nlpcraft.nlp.util
 
-import org.apache.nlpcraft.NCModelConfig
+import org.apache.nlpcraft.{NCEntityParser, NCModelConfig}
 import org.apache.nlpcraft.internal.util.NCResourceReader as R
 import org.apache.nlpcraft.nlp.enrichers.*
 import org.apache.nlpcraft.nlp.parsers.*
@@ -29,8 +29,12 @@ final val EN_TOK_PARSER = new NCOpenNLPTokenParser(R.getPath("opennlp/en-token.b
 final val EN_TOK_STOP_ENRICHER = new NCEnStopWordsTokenEnricher
 final val EN_TOK_LEMMA_POS_ENRICHER =
     new NCOpenNLPLemmaPosTokenEnricher(R.getPath("opennlp/en-pos-maxent.bin"), R.getPath("opennlp/en-lemmatizer.dict"))
-final def mkEnPipeline: NCTestPipeline = NCTestPipeline(EN_TOK_PARSER)
+final def mkEmptyEnPipeline: NCTestPipeline = NCTestPipeline(EN_TOK_PARSER)
+final def mkEnPipeline(ep: NCEntityParser): NCTestPipeline =
+    val pl = mkEmptyEnPipeline
+    pl.entParsers += ep
+    pl
 final def mkEnPipeline(es: NCSemanticTestElement*): NCTestPipeline =
-    val pl = mkEnPipeline
+    val pl = mkEmptyEnPipeline
     pl.entParsers += NCTestUtils.mkEnSemanticParser(es*)
     pl
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestModelAdapter.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestModelAdapter.scala
index c32ed65b..81be545d 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestModelAdapter.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/util/NCTestModelAdapter.scala
@@ -24,7 +24,7 @@ import org.apache.nlpcraft.nlp.util.*
   *
   */
 abstract class NCTestModelAdapter extends NCModel:
-    val pipeline: NCTestPipeline = mkEnPipeline
+    val pipeline: NCTestPipeline = mkEmptyEnPipeline
 
     override val getConfig: NCModelConfig = CFG
     override val getPipeline: NCPipeline = pipeline
\ No newline at end of file