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/06 12:03:04 UTC
[incubator-nlpcraft] branch NLPCRAFT-383 updated: WIP.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-383
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-383 by this push:
new 0631a2a WIP.
0631a2a is described below
commit 0631a2a2ecdb396474d8acc077967e6deab68c53
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Mon Sep 6 15:02:52 2021 +0300
WIP.
---
.../examples/solarsystem/SolarSystemModel.scala | 2 +-
.../api/SolarSystemOpenApiService.scala | 39 +++++++++++++--------
.../intents/SolarSystemDiscoverer.scala | 15 +++++---
...emDate.scala => SolarSystemDiscoveryDate.scala} | 40 ++++++++++++++++------
.../intents/SolarSystemPlanetInfo.scala | 13 ++++---
.../src/main/resources/solarsystem_model.yaml | 3 +-
.../solarsystem/NCSolarSystemModelSpec.scala | 8 +++++
7 files changed, 84 insertions(+), 36 deletions(-)
diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala
index aa5c804..3338615 100644
--- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala
+++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala
@@ -21,7 +21,7 @@ import com.typesafe.scalalogging.LazyLogging
import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService
import org.apache.nlpcraft.model.{NCModelAddPackage, NCModelFileAdapter}
-@NCModelAddPackage(Array("org.apache.nlpcraft.examples.solarsystem.api"))
+@NCModelAddPackage(Array("org.apache.nlpcraft.examples.solarsystem.intents"))
class SolarSystemModel extends NCModelFileAdapter("solarsystem_model.yaml") with LazyLogging {
protected var api: SolarSystemOpenApiService = _
diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala
index 3e94f84..f5ba5b8 100644
--- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala
+++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/api/SolarSystemOpenApiService.scala
@@ -27,26 +27,31 @@ import java.net.http.{HttpClient, HttpRequest, HttpResponse}
import java.net.{URI, URLEncoder}
object SolarSystemOpenApiService {
+ private final val URL_BODIES = "https://api.le-systeme-solaire.net/rest/bodies"
+ private final val MAPPER = new ObjectMapper().registerModule(DefaultScalaModule)
+
case class BodiesBean(bodies: Seq[Map[String, Object]])
private var s: SolarSystemOpenApiService = _
- def getInstance(): SolarSystemOpenApiService =
- this.synchronized {
- if (s == null) {
- s = new SolarSystemOpenApiService
+ def getInstance(): SolarSystemOpenApiService = {
+ if (s == null)
+ this.synchronized {
+ if (s == null) {
+ s = new SolarSystemOpenApiService
- s.start()
+ // Skips errors processing for simplifying.
+ s.start()
+ }
}
- s
- }
+ s
+ }
}
-class SolarSystemOpenApiService extends LazyLogging {
- private final val URL_BODIES = "https://api.le-systeme-solaire.net/rest/bodies"
- private final val MAPPER = new ObjectMapper().registerModule(DefaultScalaModule)
+import SolarSystemOpenApiService._
+class SolarSystemOpenApiService extends LazyLogging {
private var client: HttpClient = _
private var planets: Map[String, String] = _
@@ -84,17 +89,23 @@ class SolarSystemOpenApiService extends LazyLogging {
if (params != null)
url = s"$url${getSeparator}data=${params.mkString(",")}"
- if (filter != null)
- url = s"$url${getSeparator}filter=${filter.data},${filter.oper},${URLEncoder.encode(filter.value, "UTF-8")}"
+ if (filter != null) {
+ val v = URLEncoder.encode(filter.value, "UTF-8")
+
+ url = s"$url${getSeparator}filter=${filter.data},${filter.oper},$v"
+ }
logger.info(s"Request prepared: $url")
val respJs = client.sendAsync(
- HttpRequest.newBuilder(URI.create(url)).header("Content-Type", "application/json").GET().build(),
+ HttpRequest.newBuilder(URI.create(url)).
+ header("Content-Type", "application/json").GET().build(),
HttpResponse.BodyHandlers.ofString()
).get().body()
- logger.info(s"Response received: $respJs")
+ val respLog = if (respJs.length > 100) s"${respJs.take(100)} ....." else respJs
+
+ logger.info(s"Response received: $respLog")
MAPPER.readValue(respJs, classOf[BodiesBean]).bodies
}
diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoverer.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoverer.scala
index fcc2497..914c100 100644
--- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoverer.scala
+++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoverer.scala
@@ -31,11 +31,16 @@ class SolarSystemDiscoverer {
)
@NCIntent(
"intent=discoverer " +
- " options={" +
- " 'unused_usr_toks': true " +
- " }" +
- " term(discoverer)={tok_id() == 'discoverer'}"
+ " options={'unused_usr_toks': true}" +
+ " term(discoverer)={tok_id() == 'discoverer'}"
)
def discoverer(@NCIntentTerm("discoverer") discoverer: NCToken): NCResult =
- NCResult.text(SolarSystemOpenApiService.getInstance().bodyRequest().withFilter("discoveredBy", "cs", discoverer.getNormalizedText).execute().toString())
+ NCResult.text(
+ SolarSystemOpenApiService.
+ getInstance().
+ bodyRequest().
+ withFilter("discoveredBy", "cs", discoverer.getNormalizedText).
+ execute().
+ toString()
+ )
}
\ No newline at end of file
diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDate.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoveryDate.scala
similarity index 69%
rename from nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDate.scala
rename to nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoveryDate.scala
index 709fcd5..b6cf2d9 100644
--- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDate.scala
+++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoveryDate.scala
@@ -17,6 +17,7 @@
package org.apache.nlpcraft.examples.solarsystem.intents
+import com.typesafe.scalalogging.LazyLogging
import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService
import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm, NCResult, NCToken}
@@ -24,7 +25,7 @@ import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder, DateTimePa
import java.time.temporal.ChronoField.{DAY_OF_MONTH, MONTH_OF_YEAR}
import java.time.{LocalDate, ZoneOffset}
-class SolarSystemDate {
+class SolarSystemDiscoveryDate extends LazyLogging {
@NCIntentSample(
Array(
"After 1900 year",
@@ -32,13 +33,17 @@ class SolarSystemDate {
)
)
@NCIntent(
- "intent=date " +
- " options={" +
- " 'unused_usr_toks': true " +
- " }" +
- " term(date)={tok_id() == 'nlpcraft:date'} "
+ "intent=discoveryDate " +
+ " options={'unused_usr_toks': true}" +
+ " term(year)={" +
+ " tok_id() == 'nlpcraft:num' && " +
+ " (" +
+ " meta_tok('nlpcraft:num:unit') == 'year' || " +
+ " meta_tok('nlpcraft:num:from') >= 1610 && meta_tok('nlpcraft:num:from') <= year" +
+ " )" +
+ "}"
)
- def date(@NCIntentTerm("date") date: NCToken): NCResult = {
+ def date(@NCIntentTerm("year") year: NCToken): NCResult = {
// API doesn't support filter by dates.
// We do it here.
var res = SolarSystemOpenApiService.getInstance().bodyRequest().execute()
@@ -57,18 +62,25 @@ class SolarSystemDate {
toFormatter()
)
- val from: Long = date.metax("nlpcraft:date:from")
- val to: Long = date.metax("nlpcraft:date:to")
+ val from: Double = year.metax("nlpcraft:num:from")
+ val to: Double = year.metax("nlpcraft:num:to")
+
+ println("year="+year.getMetadata)
+ println("WAS="+res.size)
+ println("from="+from + ", to=" + to)
res = res.filter(row => {
val dateStr = row("discoveryDate").asInstanceOf[String]
+
+
+ val x =
if (dateStr.nonEmpty)
supportedFmts.flatMap(p =>
try {
- val ms = LocalDate.parse(dateStr, p).atStartOfDay(ZoneOffset.UTC).toInstant.toEpochMilli
+ val years = LocalDate.parse(dateStr, p).atStartOfDay(ZoneOffset.UTC).getYear
- Some(ms >= from && ms <= to)
+ Some(years >= from && years <= to)
}
catch {
case _: DateTimeParseException => None
@@ -79,8 +91,14 @@ class SolarSystemDate {
getOrElse(throw new AssertionError(s"Template not found for: $dateStr"))
else
false
+
+ println("dateStr="++dateStr + ", x="+x)
+
+ x
})
+ logger.info(s"Request result filtered with years range ${from.toLong}-${to.toLong}, rows=${res.size}")
+
NCResult.text(res.toString())
}
}
\ No newline at end of file
diff --git a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemPlanetInfo.scala b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemPlanetInfo.scala
index b164a9b..a674c2e 100644
--- a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemPlanetInfo.scala
+++ b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemPlanetInfo.scala
@@ -29,11 +29,16 @@ class SolarSystemPlanetInfo {
)
@NCIntent(
"intent=planetInfo " +
- " options={" +
- " 'unused_usr_toks': false " +
- " }" +
+ " options={'unused_usr_toks': false }" +
" term(planet)={tok_id() == 'planet'}"
)
def planetInfo(@NCIntentTerm("planet") planet: NCToken): NCResult =
- NCResult.text(SolarSystemOpenApiService.getInstance().bodyRequest().withFilter("id", "eq", planet.getNormalizedText).execute().toString())
+ NCResult.text(
+ SolarSystemOpenApiService.
+ getInstance().
+ bodyRequest().
+ withFilter("id", "eq", planet.getNormalizedText).
+ execute().
+ toString()
+ )
}
\ No newline at end of file
diff --git a/nlpcraft-examples/solarsystem/src/main/resources/solarsystem_model.yaml b/nlpcraft-examples/solarsystem/src/main/resources/solarsystem_model.yaml
index 93c4ac0..996070c 100644
--- a/nlpcraft-examples/solarsystem/src/main/resources/solarsystem_model.yaml
+++ b/nlpcraft-examples/solarsystem/src/main/resources/solarsystem_model.yaml
@@ -19,7 +19,8 @@ id: "nlpcraft.solarsystem.ex"
name: "Solar System Example Model"
version: "1.0"
description: "NLI-powered Solar system data chat example model."
-enabledBuiltInTokens: ["nlpcraft:num", "nlpcraft:limit", "nlpcraft:sort", "nlpcraft:relation", "nlpcraft:date"]
+# Note that "nlpcraft:date" excluded because this NER doesn't support some dates period (before 1900 year)
+enabledBuiltInTokens: ["nlpcraft:num", "nlpcraft:limit", "nlpcraft:sort", "nlpcraft:relation"]
permutateSynonyms: false
sparse: false
diff --git a/nlpcraft-examples/solarsystem/src/test/java/org/apache/nlpcraft/examples/solarsystem/NCSolarSystemModelSpec.scala b/nlpcraft-examples/solarsystem/src/test/java/org/apache/nlpcraft/examples/solarsystem/NCSolarSystemModelSpec.scala
index 921b57b..21b880b 100644
--- a/nlpcraft-examples/solarsystem/src/test/java/org/apache/nlpcraft/examples/solarsystem/NCSolarSystemModelSpec.scala
+++ b/nlpcraft-examples/solarsystem/src/test/java/org/apache/nlpcraft/examples/solarsystem/NCSolarSystemModelSpec.scala
@@ -29,4 +29,12 @@ class NCSolarSystemModelSpec extends NCTestContext {
checkIntent("moon", "planetInfo")
checkIntent("give me information about Larissa", "planetInfo")
}
+
+ @Test
+ def planetDate(): Unit = {
+ checkIntent("After 1900 year", "discoveryDate")
+ checkIntent("Before 1900 year", "discoveryDate")
+ checkIntent("between 1900 and 1907 years", "discoveryDate")
+ checkIntent("between 1900 and 1907", "discoveryDate")
+ }
}