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/05 09:59:39 UTC
[incubator-nlpcraft] branch NLPCRAFT-431-430 updated: Build scripts
fixes.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-431-430
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-431-430 by this push:
new 5dbd366 Build scripts fixes.
5dbd366 is described below
commit 5dbd366f894deffbd272d9a197000ee7ee7971d8
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Sun Sep 5 12:59:32 2021 +0300
Build scripts fixes.
---
.../nlp/enrichers/numeric/NCNumericEnricher.scala | 28 ++++++-----
.../mgrs/nlp/enrichers/NCEnrichersTestBeans.scala | 18 +++++--
.../enrichers/numeric/NCEnricherNumericSpec.scala | 58 ++++++++++++++++++++++
3 files changed, 88 insertions(+), 16 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/nlp/enrichers/numeric/NCNumericEnricher.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/nlp/enrichers/numeric/NCNumericEnricher.scala
index b89ff99..670a4dc 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/nlp/enrichers/numeric/NCNumericEnricher.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/nlp/enrichers/numeric/NCNumericEnricher.scala
@@ -223,6 +223,14 @@ object NCNumericEnricher extends NCServerEnricher {
"isToPositiveInfinity" -> (to == MAX_VALUE)
)
+ def mkAndAssign(toks: Seq[NCNlpSentenceToken], typ: String, params: (String, Any)*):NCNlpSentenceNote = {
+ val note = NCNlpSentenceNote(toks.map(_.index), "nlpcraft:num", params:_*)
+
+ toks.foreach(_.add(note))
+
+ note
+ }
+
unitDataOpt match {
case Some(unitData) =>
def extend(): Seq[(String, Any)] = {
@@ -233,17 +241,17 @@ object NCNumericEnricher extends NCServerEnricher {
}
if (unitData.tokens == toks)
- Seq(NCNlpSentenceNote(toks.map(_.index), "nlpcraft:num", extend():_*))
+ Seq(mkAndAssign(toks, "nlpcraft:num", extend():_*))
else {
- val n1 = NCNlpSentenceNote(
- toks.filter(t => !unitData.tokens.contains(t)).map(_.index), "nlpcraft:num", params.clone():_*
+ Seq(
+ mkAndAssign(
+ toks.filter(t => !unitData.tokens.contains(t)), "nlpcraft:num", params.clone():_*
+ ),
+ mkAndAssign(toks, "nlpcraft:num", extend():_*)
)
- val n2 = NCNlpSentenceNote(toks.map(_.index), "nlpcraft:num", extend():_*)
-
- Seq(n1, n2)
}
- case None => Seq(NCNlpSentenceNote(toks.map(_.index), "nlpcraft:num", params:_*))
+ case None => Seq(mkAndAssign(toks, "nlpcraft:num", params:_*))
}
}
@@ -334,9 +342,6 @@ object NCNumericEnricher extends NCServerEnricher {
case _ => throw new AssertionError(s"Illegal note type: ${p._2}.")
}
- for (note <- notes)
- prepToks.foreach(_.add(note))
-
processed ++= ts1
processed ++= ts2
}
@@ -455,9 +460,6 @@ object NCNumericEnricher extends NCServerEnricher {
)
processed ++= num.tokens
-
- for (note <- notes)
- num.tokens.foreach(_.add(note))
}
}
}
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala
index fa54b2d..b4d2f71 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/NCEnrichersTestBeans.scala
@@ -64,11 +64,20 @@ case class NCTestCoordinateToken(text: String, latitude: Double, longitude: Doub
override def toString: String = s"$text(coordinate)<lon=$longitude, lat=$longitude>"
}
-case class NCTestNumericToken(text: String, from: Double, to: Double) extends NCTestToken {
+case class NCTestNumericToken(text: String, from: Double, to: Double, unit: Option[String] = None) extends NCTestToken {
require(text != null)
override def id: String = "nlpcraft:num"
- override def toString: String = s"$text(num)<from=$from, to=$to>"
+ override def toString: String = {
+ var s = s"$text(num)<from=$from, to=$to>"
+
+ unit match {
+ case Some(u) => s = s"$s($u)"
+ case None => // No-op.
+ }
+
+ s
+ }
}
case class NCTestCityToken(text: String, city: String) extends NCTestToken {
@@ -305,10 +314,13 @@ object NCTestToken {
longitude = t.meta("nlpcraft:coordinate:longitude")
)
case "nlpcraft:num" =>
+ val unit: Optional[String] = t.metaOpt("nlpcraft:num:unit")
+
NCTestNumericToken(
txt,
from = t.meta("nlpcraft:num:from"),
- to = t.meta("nlpcraft:num:to")
+ to = t.meta("nlpcraft:num:to"),
+ unit = unit.asScala
)
case "nlpcraft:date" => NCTestDateToken(txt)
case "nlpcraft:city" => NCTestCityToken(txt, city = t.meta("nlpcraft:city:city"))
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/numeric/NCEnricherNumericSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/numeric/NCEnricherNumericSpec.scala
new file mode 100644
index 0000000..1ff1c93
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/probe/mgrs/nlp/enrichers/numeric/NCEnricherNumericSpec.scala
@@ -0,0 +1,58 @@
+/*
+ * 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
+ *
+ * https://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.probe.mgrs.nlp.enrichers.numeric
+
+import org.apache.nlpcraft.NCTestEnvironment
+import org.apache.nlpcraft.probe.mgrs.nlp.enrichers.{NCDefaultTestModel, NCEnricherBaseSpec, NCTestNumericToken => num, NCTestNlpToken => nlp, NCTestUserToken => usr}
+import org.junit.jupiter.api.Test
+
+/**
+ * Limit enricher test.
+ */
+@NCTestEnvironment(model = classOf[NCDefaultTestModel], startClient = true)
+class NCEnricherNumericSpec extends NCEnricherBaseSpec {
+ /**
+ *
+ * @throws Exception
+ */
+ @Test
+ def test(): Unit =
+ runBatch(
+ // Doesn't check `limit` for given sentence.
+ _ => checkExists(
+ "23 A",
+ num(text = "23 A", 23, 23, unit = Some("are")),
+ ),
+ _ => checkExists(
+ "23 A",
+ num(text = "23", 23, 23),
+ usr(text = "A", id = "A")
+ ),
+
+ _ => checkAll(
+ "23 mile",
+ Seq(
+ num(text = "23 mile", 23, 23, unit = Some("mile"))
+ ),
+ Seq(
+ num(text = "23", 23, 23),
+ nlp(text = "mile")
+ )
+ )
+ )
+}