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 283c29d WIP.
283c29d is described below
commit 283c29dd11899df4b9cfcec29ee57d3948334820
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Aug 6 19:24:29 2021 +0300
WIP.
---
.../examples/solarsystem/SolarSystemModel.scala | 68 +++++++++++++++++++++-
1 file changed, 67 insertions(+), 1 deletion(-)
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 d6a1c3e..818d600 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,6 +21,10 @@ import com.typesafe.scalalogging.LazyLogging
import org.apache.nlpcraft.examples.solarsystem.tools.SolarSystemOpenApiService
import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm,
NCModelFileAdapter, NCResult, NCToken}
+import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder,
DateTimeParseException}
+import java.time.temporal.ChronoField
+import java.time.{LocalDate, ZoneOffset}
+
class SolarSystemModel extends NCModelFileAdapter("solarsystem_model.yaml")
with LazyLogging {
private var api: SolarSystemOpenApiService = _
@@ -70,4 +74,66 @@ class SolarSystemModel extends
NCModelFileAdapter("solarsystem_model.yaml") with
)
def discoverer(@NCIntentTerm("discoverer") discoverer: NCToken): NCResult =
NCResult.text(api.bodyRequest().withFilter("discoveredBy", "cs",
discoverer.getNormalizedText).execute().toString())
-}
+
+ @NCIntentSample(
+ Array(
+ "After 1900 year",
+ "After 1900 year",
+ )
+ )
+ @NCIntent(
+ "intent=date " +
+ " options={" +
+ " 'unused_usr_toks': true " +
+ " }" +
+ " term(date)={tok_id() == 'nlpcraft:date'} "
+ )
+ def date(@NCIntentTerm("date") date: NCToken): NCResult = {
+ // API doesn't support filter by dates.
+ // We do it here.
+ var res = api.bodyRequest().execute()
+
+ val supportedFmts =
+ Seq (
+ DateTimeFormatter.ofPattern("dd/MM/yyyy"),
+ new DateTimeFormatterBuilder().
+ appendPattern("yyyy").
+ parseDefaulting(ChronoField.MONTH_OF_YEAR, 1).
+ parseDefaulting(ChronoField.DAY_OF_MONTH, 1).
+ toFormatter(),
+ new DateTimeFormatterBuilder().
+ appendPattern("??/MM/yyyy").
+ parseDefaulting(ChronoField.DAY_OF_MONTH, 1).
+ toFormatter()
+ )
+
+ val from: Long = date.metax("nlpcraft:date:from")
+ val to: Long = date.metax("nlpcraft:date:to")
+
+ res = res.filter(row => {
+ val dateStr = row("discoveryDate").asInstanceOf[String]
+
+ if (dateStr.nonEmpty) {
+ supportedFmts.flatMap(p => {
+ try {
+ val time = LocalDate.parse(dateStr,
p).atStartOfDay(ZoneOffset.UTC).toInstant.toEpochMilli
+
+ Some(time >= from && time <= to)
+ }
+ catch {
+ case _: DateTimeParseException => None
+ }
+ }).
+ to(LazyList).
+ headOption.
+ getOrElse(throw new AssertionError(s"Template not found
for: $dateStr"))
+ }
+ else
+ false
+ })
+
+ println("after=" + res.size)
+
+ NCResult.text(res.toString())
+ }
+}
\ No newline at end of file