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 62d6a60 WIP.
62d6a60 is described below
commit 62d6a60c437a5b283c3f6e4ef1da81898db4fb27
Author: Sergey Kamov <[email protected]>
AuthorDate: Tue Aug 24 11:53:36 2021 +0300
WIP.
---
.../examples/solarsystem/SolarSystemModel.scala | 118 +--------------------
.../api/SolarSystemOpenApiService.scala | 2 +-
.../SolarSystemDate.scala} | 59 +----------
.../intents/SolarSystemDiscoverer.scala | 40 +++++++
.../intents/SolarSystemIntentsAdapter.scala | 39 +++++++
.../intents/SolarSystemPlanetInfo.scala | 38 +++++++
.../loaders/SolarSystemDiscoversValueLoader.scala | 1 -
7 files changed, 125 insertions(+), 172 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 6bf36ac..e4426cc 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
@@ -18,119 +18,7 @@
package org.apache.nlpcraft.examples.solarsystem
import com.typesafe.scalalogging.LazyLogging
-import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService
-import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm,
NCModelFileAdapter, NCResult, NCToken}
+import org.apache.nlpcraft.model.{NCModelAddPackage, NCModelFileAdapter}
-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 = _
-
- override def onInit(): Unit = {
- api = SolarSystemOpenApiService.getInstance()
-
- logger.info("Solar System Model started.")
- }
-
- override def onDiscard(): Unit = {
- if (api != null)
- api.stop()
-
- logger.info("Solar System Model stopped.")
- }
-
- @NCIntentSample(
- Array(
- "Moon!",
- "give me information about Larissa",
- )
- )
- @NCIntent(
- "intent=planetInfo " +
- " options={" +
- " 'unused_usr_toks': false " +
- " }" +
- " term(planet)={tok_id() == 'planet'}"
- )
- def planetInfo(@NCIntentTerm("planet") planet: NCToken): NCResult =
- NCResult.text(api.bodyRequest().withFilter("id", "eq",
planet.getNormalizedText).execute().toString())
-
- @NCIntentSample(
- Array(
- "What was discovered by Asaph Hall",
- "What was discovered by Hall",
- "Galileo Galilei planets",
- "Galilei planets"
- )
- )
- @NCIntent(
- "intent=discoverer " +
- " options={" +
- " 'unused_usr_toks': true " +
- " }" +
- " term(discoverer)={tok_id() == 'discoverer'}"
- )
- 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(MONTH_OF_YEAR, 1).
- parseDefaulting(DAY_OF_MONTH, 1).
- toFormatter(),
- new DateTimeFormatterBuilder().
- appendPattern("??/MM/yyyy").
- parseDefaulting(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 ms = LocalDate.parse(dateStr,
p).atStartOfDay(ZoneOffset.UTC).toInstant.toEpochMilli
-
- Some(ms >= from && ms <= to)
- }
- catch {
- case _: DateTimeParseException => None
- }
- ).
- to(LazyList).
- headOption.
- getOrElse(throw new AssertionError(s"Template not found
for: $dateStr"))
- else
- false
- })
-
- NCResult.text(res.toString())
- }
-}
\ No newline at end of file
+@NCModelAddPackage(Array("org.apache.nlpcraft.examples.solarsystem.intents"))
+class SolarSystemModel extends NCModelFileAdapter("solarsystem_model.yaml")
with LazyLogging
\ No newline at end of file
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 f859583..3e94f84 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
@@ -22,9 +22,9 @@ import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.typesafe.scalalogging.LazyLogging
import
org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService.BodiesBean
-import java.net.{URI, URLEncoder}
import java.net.http.HttpClient.Version
import java.net.http.{HttpClient, HttpRequest, HttpResponse}
+import java.net.{URI, URLEncoder}
object SolarSystemOpenApiService {
case class BodiesBean(bodies: Seq[Map[String, Object]])
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/intents/SolarSystemDate.scala
similarity index 61%
copy from
nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/SolarSystemModel.scala
copy to
nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDate.scala
index 6bf36ac..8de6410 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/intents/SolarSystemDate.scala
@@ -15,66 +15,15 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.solarsystem
+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,
NCModelFileAdapter, NCResult, NCToken}
+import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm,
NCResult, NCToken}
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder,
DateTimeParseException}
-import java.time.temporal.ChronoField._
+import java.time.temporal.ChronoField.{DAY_OF_MONTH, MONTH_OF_YEAR}
import java.time.{LocalDate, ZoneOffset}
-class SolarSystemModel extends NCModelFileAdapter("solarsystem_model.yaml")
with LazyLogging {
- private var api: SolarSystemOpenApiService = _
-
- override def onInit(): Unit = {
- api = SolarSystemOpenApiService.getInstance()
-
- logger.info("Solar System Model started.")
- }
-
- override def onDiscard(): Unit = {
- if (api != null)
- api.stop()
-
- logger.info("Solar System Model stopped.")
- }
-
- @NCIntentSample(
- Array(
- "Moon!",
- "give me information about Larissa",
- )
- )
- @NCIntent(
- "intent=planetInfo " +
- " options={" +
- " 'unused_usr_toks': false " +
- " }" +
- " term(planet)={tok_id() == 'planet'}"
- )
- def planetInfo(@NCIntentTerm("planet") planet: NCToken): NCResult =
- NCResult.text(api.bodyRequest().withFilter("id", "eq",
planet.getNormalizedText).execute().toString())
-
- @NCIntentSample(
- Array(
- "What was discovered by Asaph Hall",
- "What was discovered by Hall",
- "Galileo Galilei planets",
- "Galilei planets"
- )
- )
- @NCIntent(
- "intent=discoverer " +
- " options={" +
- " 'unused_usr_toks': true " +
- " }" +
- " term(discoverer)={tok_id() == 'discoverer'}"
- )
- def discoverer(@NCIntentTerm("discoverer") discoverer: NCToken): NCResult =
- NCResult.text(api.bodyRequest().withFilter("discoveredBy", "cs",
discoverer.getNormalizedText).execute().toString())
-
+class SolarSystemDate extends SolarSystemIntentsAdapter {
@NCIntentSample(
Array(
"After 1900 year",
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
new file mode 100644
index 0000000..3747ac0
--- /dev/null
+++
b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemDiscoverer.scala
@@ -0,0 +1,40 @@
+/*
+ * 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
+ *
+ * http://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.examples.solarsystem.intents
+
+import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm,
NCResult, NCToken}
+
+class SolarSystemDiscoverer extends SolarSystemIntentsAdapter {
+ @NCIntentSample(
+ Array(
+ "What was discovered by Asaph Hall",
+ "What was discovered by Hall",
+ "Galileo Galilei planets",
+ "Galilei planets"
+ )
+ )
+ @NCIntent(
+ "intent=discoverer " +
+ " options={" +
+ " 'unused_usr_toks': true " +
+ " }" +
+ " term(discoverer)={tok_id() == 'discoverer'}"
+ )
+ def discoverer(@NCIntentTerm("discoverer") discoverer: NCToken): NCResult =
+ NCResult.text(api.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/SolarSystemIntentsAdapter.scala
b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemIntentsAdapter.scala
new file mode 100644
index 0000000..2cf76ad
--- /dev/null
+++
b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemIntentsAdapter.scala
@@ -0,0 +1,39 @@
+/*
+ * 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
+ *
+ * http://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.examples.solarsystem.intents
+
+import com.typesafe.scalalogging.LazyLogging
+import org.apache.nlpcraft.examples.solarsystem.api.SolarSystemOpenApiService
+import org.apache.nlpcraft.model.NCLifecycle
+
+private[intents] class SolarSystemIntentsAdapter extends NCLifecycle with
LazyLogging {
+ protected var api: SolarSystemOpenApiService = _
+
+ override def onInit(): Unit = {
+ api = SolarSystemOpenApiService.getInstance()
+
+ logger.info("Solar System API initialized.")
+ }
+
+ override def onDiscard(): Unit = {
+ if (api != null)
+ api.stop()
+
+ logger.info("Solar System API closed.")
+ }
+}
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
new file mode 100644
index 0000000..24c281a
--- /dev/null
+++
b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/intents/SolarSystemPlanetInfo.scala
@@ -0,0 +1,38 @@
+/*
+ * 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
+ *
+ * http://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.examples.solarsystem.intents
+
+import org.apache.nlpcraft.model.{NCIntent, NCIntentSample, NCIntentTerm,
NCResult, NCToken}
+
+class SolarSystemPlanetInfo extends SolarSystemIntentsAdapter {
+ @NCIntentSample(
+ Array(
+ "Moon!",
+ "give me information about Larissa",
+ )
+ )
+ @NCIntent(
+ "intent=planetInfo " +
+ " options={" +
+ " 'unused_usr_toks': false " +
+ " }" +
+ " term(planet)={tok_id() == 'planet'}"
+ )
+ def planetInfo(@NCIntentTerm("planet") planet: NCToken): NCResult =
+ NCResult.text(api.bodyRequest().withFilter("id", "eq",
planet.getNormalizedText).execute().toString())
+}
\ No newline at end of file
diff --git
a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala
b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala
index 7598419..21a3aa0 100644
---
a/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala
+++
b/nlpcraft-examples/solarsystem/src/main/java/org/apache/nlpcraft/examples/solarsystem/loaders/SolarSystemDiscoversValueLoader.scala
@@ -49,5 +49,4 @@ class SolarSystemDiscoversValueLoader extends NCValueLoader {
override def load(owner: NCElement): util.Set[NCValue] =
SolarSystemOpenApiService.getInstance().getAllDiscovers.map(mkValue).toSet.asJava
-
}