This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-479
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-479 by this push:
new be4358e Examples refactoring.
be4358e is described below
commit be4358e32b4794568568a7fac66a1fd0daa211bd
Author: Sergey Kamov <[email protected]>
AuthorDate: Fri Feb 25 14:52:51 2022 +0300
Examples refactoring.
---
.../examples/lightswitch/LightSwitchModelRu.scala | 2 +-
nlpcraft-examples/time/pom.xml | 6 -
.../apache/nlpcraft/examples/time/TimeModel.java | 7 +-
.../time}/utils/cities/CitiesDataProvider.java | 2 +-
.../nlpcraft/examples/time}/utils/cities/City.java | 2 +-
.../examples/time}/utils/cities/CityData.java | 2 +-
.../examples/time}/utils/keycdn/GeoData.java | 2 +-
.../examples/time}/utils/keycdn/GeoManager.java | 2 +-
.../examples/time}/utils/keycdn/Response.java | 2 +-
.../examples/time}/utils/keycdn/ResponseData.java | 2 +-
.../src/main/resources/cities_timezones.txt | 0
nlpcraft-examples/utils/pom.xml | 70 -----
nlpcraft-examples/weather/README.md | 49 ----
nlpcraft-examples/weather/pom.xml | 86 -------
.../nlpcraft/examples/weather/WeatherModel.java | 281 ---------------------
.../openweathermap/OpenWeatherMapException.java | 42 ---
.../openweathermap/OpenWeatherMapService.java | 214 ----------------
.../weather/src/main/resources/weather_model.json | 59 -----
.../examples/weather/NCModelValidationSpec.scala | 46 ----
pom.xml | 7 +-
20 files changed, 12 insertions(+), 871 deletions(-)
diff --git
a/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelRu.scala
b/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelRu.scala
index 67af24a..b36b1b2 100644
---
a/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelRu.scala
+++
b/nlpcraft-examples/lightswitch-ru/src/main/java/org/apache/nlpcraft/examples/lightswitch/LightSwitchModelRu.scala
@@ -62,7 +62,7 @@ class LightSwitchModelRu extends NCModel:
"Выруби электричество!",
"Включи свет в детской",
"Включай повсюду освещение",
- "Зажигай лампы в детской комнате",
+ "Включайте лампы в детской комнате",
"Свет на кухне пожалуйста приглуши",
"Нельзя ли повсюду выключить свет",
"Пожалуйста без света",
diff --git a/nlpcraft-examples/time/pom.xml b/nlpcraft-examples/time/pom.xml
index 7988f1c..a2e5e3b 100644
--- a/nlpcraft-examples/time/pom.xml
+++ b/nlpcraft-examples/time/pom.xml
@@ -38,12 +38,6 @@
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft-examples-utils</artifactId>
- <version>${project.version}</version>
- </dependency>
-
<!-- Test dependencies. -->
<dependency>
<groupId>org.junit.jupiter</groupId>
diff --git
a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
index 6d8d29e..1d63e6a 100644
---
a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/TimeModel.java
@@ -21,11 +21,8 @@ import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.yaml.*;
import org.apache.nlpcraft.*;
-import org.apache.nlpcraft.examples.utils.cities.CitiesDataProvider;
-import org.apache.nlpcraft.examples.utils.cities.City;
-import org.apache.nlpcraft.examples.utils.cities.CityData;
-import org.apache.nlpcraft.examples.utils.keycdn.GeoManager;
-import org.apache.nlpcraft.examples.utils.keycdn.GeoData;
+import org.apache.nlpcraft.examples.time.utils.cities.*;
+import org.apache.nlpcraft.examples.time.utils.keycdn.*;
import org.apache.nlpcraft.nlp.entity.parser.opennlp.NCOpenNLPEntityParser;
import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser;
import
org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer;
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CitiesDataProvider.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/CitiesDataProvider.java
similarity index 98%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CitiesDataProvider.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/CitiesDataProvider.java
index 2b14e7a..3e85565 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CitiesDataProvider.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/CitiesDataProvider.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.cities;
+package org.apache.nlpcraft.examples.time.utils.cities;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.nlpcraft.NCException;
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/City.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/City.java
similarity index 97%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/City.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/City.java
index 72cf102..718b308 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/City.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/City.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.cities;
+package org.apache.nlpcraft.examples.time.utils.cities;
import java.util.Objects;
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CityData.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/CityData.java
similarity index 96%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CityData.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/CityData.java
index 4ba42e9..3fa40c8 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CityData.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/cities/CityData.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.cities;
+package org.apache.nlpcraft.examples.time.utils.cities;
/**
* City data holder.
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoData.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/GeoData.java
similarity index 98%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoData.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/GeoData.java
index 85c7238..2a3e940 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoData.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/GeoData.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.keycdn;
+package org.apache.nlpcraft.examples.time.utils.keycdn;
import com.google.gson.annotations.SerializedName;
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoManager.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/GeoManager.java
similarity index 98%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoManager.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/GeoManager.java
index 756fc5a..9de10ec 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoManager.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/GeoManager.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.keycdn;
+package org.apache.nlpcraft.examples.time.utils.keycdn;
import com.google.gson.Gson;
import org.apache.nlpcraft.NCRequest;
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/Response.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/Response.java
similarity index 97%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/Response.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/Response.java
index eca6deb..147b55a 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/Response.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/Response.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.keycdn;
+package org.apache.nlpcraft.examples.time.utils.keycdn;
/**
* Service https://tools.keycdn.com/geo response part bean.
diff --git
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/ResponseData.java
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/ResponseData.java
similarity index 95%
rename from
nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/ResponseData.java
rename to
nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/ResponseData.java
index 20a9524..a130d8a 100644
---
a/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/ResponseData.java
+++
b/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/utils/keycdn/ResponseData.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nlpcraft.examples.utils.keycdn;
+package org.apache.nlpcraft.examples.time.utils.keycdn;
/**
* Service https://tools.keycdn.com/geo response bean.
diff --git a/nlpcraft-examples/utils/src/main/resources/cities_timezones.txt
b/nlpcraft-examples/time/src/main/resources/cities_timezones.txt
similarity index 100%
rename from nlpcraft-examples/utils/src/main/resources/cities_timezones.txt
rename to nlpcraft-examples/time/src/main/resources/cities_timezones.txt
diff --git a/nlpcraft-examples/utils/pom.xml b/nlpcraft-examples/utils/pom.xml
deleted file mode 100644
index 587e0c4..0000000
--- a/nlpcraft-examples/utils/pom.xml
+++ /dev/null
@@ -1,70 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<!--
- 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.
--->
-
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <name>NLPCraft Examples Utils</name>
- <artifactId>nlpcraft-examples-utils</artifactId>
-
- <parent>
- <artifactId>nlpcraft-parent</artifactId>
- <groupId>org.apache.nlpcraft</groupId>
- <version>1.0.0</version>
- <relativePath>../../pom.xml</relativePath>
- </parent>
-
- <dependencies>
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft</artifactId>
- <version>${project.version}</version>
- </dependency>
-
- <!-- Test dependencies. -->
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft</artifactId>
- <version>${project.version}</version>
- <type>test-jar</type>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-engine</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>${maven.compiler.plugin.ver}</version>
- <configuration>
- <source>${java.ver}</source>
- <target>${java.ver}</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
-</project>
\ No newline at end of file
diff --git a/nlpcraft-examples/weather/README.md
b/nlpcraft-examples/weather/README.md
deleted file mode 100644
index 589cdc3..0000000
--- a/nlpcraft-examples/weather/README.md
+++ /dev/null
@@ -1,49 +0,0 @@
-<!--
- 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.
--->
-
-<img src="https://nlpcraft.apache.org/images/nlpcraft_logo_black.gif"
height="80px">
-<br>
-
-[](https://raw.githubusercontent.com/apache/opennlp/master/LICENSE)
-[](https://github.com/apache/incubator-nlpcraft/actions)
-[](https://nlpcraft.apache.org/docs.html)
-[](https://gitter.im/apache-nlpcraft/community)
-
-### Weather Service Example
-This example demonstrates relatively complete NLI-based weather service with
JSON output and a non-trivial
-intent matching logic. It uses Apple's [Dark Sky](https://darksky.net) API
weather provider REST service for the actual
-weather information.
-
-### Documentation
-See [WeatherBot](https://nlpcraft.apache.org/examples/weather_bot.html) guide
for more instructions on how to run this example.
-
-For any questions, feedback or suggestions:
-
- * View & run other
[examples](https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft-examples)
- * Read [documentation](https://nlpcraft.apache.org/docs.html), latest
[Javadoc](https://nlpcraft.apache.org/apis/latest/index.html) and [REST
APIs](https://nlpcraft.apache.org/using-rest.html)
- * Download & Maven/Grape/Gradle/SBT
[instructions](https://nlpcraft.apache.org/download.html)
- * File a bug or improvement in
[JIRA](https://issues.apache.org/jira/projects/NLPCRAFT)
- * Post a question at [Stack
Overflow](https://stackoverflow.com/questions/ask) using <code>nlpcraft</code>
tag
- * Access [GitHub](https://github.com/apache/incubator-nlpcraft) mirror
repository.
- * Join project developers on
[[email protected]](mailto:[email protected])
-
-### Copyright
-Copyright (C) 2021 Apache Software Foundation
-
-<img src="https://www.apache.org/img/ASF20thAnniversary.jpg" height="64px"
alt="ASF Logo">
-
-
diff --git a/nlpcraft-examples/weather/pom.xml
b/nlpcraft-examples/weather/pom.xml
deleted file mode 100644
index 4e26bc3..0000000
--- a/nlpcraft-examples/weather/pom.xml
+++ /dev/null
@@ -1,86 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<!--
- 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.
--->
-
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <name>NLPCraft Example Weather</name>
- <artifactId>nlpcraft-example-weather</artifactId>
-
- <parent>
- <artifactId>nlpcraft-parent</artifactId>
- <groupId>org.apache.nlpcraft</groupId>
- <version>1.0.0</version>
- <relativePath>../../pom.xml</relativePath>
- </parent>
-
- <properties>
- <apache.httpcomponents.ver>4.5.13</apache.httpcomponents.ver>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft</artifactId>
- <version>${project.version}</version>
- </dependency>
-
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft-stanford</artifactId>
- <version>${project.version}</version>
- </dependency>
-
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft-examples-utils</artifactId>
- <version>${project.version}</version>
- </dependency>
-
- <!-- Test dependencies. -->
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-engine</artifactId>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>nlpcraft</artifactId>
- <version>${project.version}</version>
- <type>test-jar</type>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>${maven.compiler.plugin.ver}</version>
- <configuration>
- <source>${java.ver}</source>
- <target>${java.ver}</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
-</project>
\ No newline at end of file
diff --git
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
deleted file mode 100644
index 1a37d7a..0000000
---
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/WeatherModel.java
+++ /dev/null
@@ -1,281 +0,0 @@
-/*
- * 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.weather;
-
-import com.google.gson.Gson;
-import edu.stanford.nlp.pipeline.StanfordCoreNLP;
-import org.apache.nlpcraft.NCEntity;
-import org.apache.nlpcraft.NCIntent;
-import org.apache.nlpcraft.NCIntentMatch;
-import org.apache.nlpcraft.NCIntentSample;
-import org.apache.nlpcraft.NCIntentTerm;
-import org.apache.nlpcraft.NCModel;
-import org.apache.nlpcraft.NCModelConfig;
-import org.apache.nlpcraft.NCModelPipeline;
-import org.apache.nlpcraft.NCModelPipelineBuilder;
-import org.apache.nlpcraft.NCRejection;
-import org.apache.nlpcraft.NCResult;
-import org.apache.nlpcraft.NCResultType;
-import org.apache.nlpcraft.examples.utils.cities.CitiesDataProvider;
-import org.apache.nlpcraft.examples.utils.cities.City;
-import org.apache.nlpcraft.examples.utils.cities.CityData;
-import org.apache.nlpcraft.examples.utils.keycdn.GeoManager;
-import org.apache.nlpcraft.examples.utils.keycdn.GeoData;
-import
org.apache.nlpcraft.examples.weather.openweathermap.OpenWeatherMapException;
-import
org.apache.nlpcraft.examples.weather.openweathermap.OpenWeatherMapService;
-import org.apache.nlpcraft.nlp.entity.parser.semantic.NCSemanticEntityParser;
-import
org.apache.nlpcraft.nlp.entity.parser.semantic.impl.en.NCEnSemanticPorterStemmer;
-import
org.apache.nlpcraft.nlp.entity.parser.stanford.NCStanfordNLPEntityParser;
-import org.apache.nlpcraft.nlp.token.parser.stanford.NCStanfordNLPTokenParser;
-
-import java.time.Instant;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Properties;
-import java.util.Set;
-
-import static java.time.temporal.ChronoUnit.DAYS;
-
-/**
- * Weather example data model.
- * <p>
- * This is a relatively complete weather service with JSON output and a
non-trivial
- * intent matching logic. It uses OpenWeather API weather provider REST
service for the actual
- * weather information (https://openweathermap.org/api/one-call-api).
- * <p>
- * NOTE: you must provide OpenWeather API key in 'OWM_API_KEY' system property.
- * See https://openweathermap.org/api for more information.
- * <p>
- * See 'README.md' file in the same folder for running and testing
instructions.
- */
-@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
-public class WeatherModel implements NCModel {
- // System property for OpenWeatherMap API key.
- public final String OWM_API_KEY = "OWM_API_KEY";
-
- // Please register your own account at https://openweathermap.org/api and
- // replace this demo token with your own.
- // We are using the One Call API
(https://openweathermap.org/api/one-call-api) in this example
- private final OpenWeatherMapService openWeather;
-
- // Geo manager.
- private final GeoManager geoMrg = new GeoManager();
-
- // Default shift in days for history and forecast.
- private static final int DAYS_SHIFT_BACK = 5;
- private static final int DAYS_SHIFT_FORWARD = 7;
-
- // GSON instance.
- private static final Gson GSON = new Gson();
-
- static private final Map<City, CityData> citiesData =
CitiesDataProvider.get();
-
- // Keywords for 'local' weather.
- private static final Set<String> LOCAL_WORDS = new
HashSet<>(Arrays.asList("my", "local", "hometown"));
-
- private final NCModelConfig cfg;
- private final NCModelPipeline pipeline;
-
- /**
- * Extracts geolocation (city) from given solver context that is suitable
for Dark Sky API weather service.
- *
- * @param ctx Intent solver context.
- * @param geoEntOpt Optional geo entity.
- * @return Geo location.
- */
- private CityData prepGeo(NCIntentMatch ctx, Optional<NCEntity> geoEntOpt)
throws NCRejection {
- if (geoEntOpt.isPresent()) {
- String cityName = geoEntOpt.get().mkText();
-
- Optional<Map.Entry<City, CityData>> dataOpt =
- citiesData.entrySet().stream().filter(p ->
p.getKey().getName().equalsIgnoreCase(cityName)).findAny();
-
- if (!dataOpt.isPresent()) {
- throw new NCRejection(String.format("Latitude and longitude
not found for: %s", cityName));
- }
-
- return dataOpt.get().getValue();
- }
-
- Optional<GeoData> geoOpt = geoMrg.get(ctx.getContext().getRequest());
-
- if (geoOpt.isEmpty())
- throw new NCRejection("City cannot be determined.");
-
- // Manually process request for local weather. We need to separate
between 'local Moscow weather'
- // and 'local weather' which are different. Basically, if there is
word 'local/my/hometown' in the user
- // input and there is no city in the current sentence - this is a
request for the weather at user's
- // current location, i.e. we should implicitly assume user's location
and clear conversion context.
- // In all other cases - we take location from either current sentence
or conversation STM.
-
- // NOTE: we don't do this separation on intent level as it is easier
to do it here instead of
- // creating more intents with almost identical callbacks.
-
- @SuppressWarnings("SuspiciousMethodCalls")
- boolean hasLocalWord =
- ctx.getVariant().getEntities().stream().anyMatch(t ->
LOCAL_WORDS.contains(t.mkText().toLowerCase()));
-
- if (hasLocalWord)
- // Because we implicitly assume user's current city at this point
we need to clear
- // 'nlpcraft:city' tokens from conversation since they would no
longer be valid.
- ctx.getContext().getConversation().clearStm(t ->
t.getId().equals("nlpcraft:city"));
-
- // Try current user location.
- GeoData geo = geoOpt.get();
-
- return new CityData(geo.getTimezoneName(), geo.getLatitude(),
geo.getLongitude());
- }
-
- /**
- * A callback for the intent match.
- *
- * @param ctx Intent match context.
- * @param indEntsOpt List of optional indicator elements.
- * @param cityEntOpt Optional GEO token for city.
- * @param dateEntOpt Optional date token.
- * @return Callback result.
- */
- @NCIntent(
- "intent=req " +
- "term~{# == 'wt:phen'}* " + // Zero or more weather phenomenon.
- "term(ind)~{" +
- "@isIndicator = has(ent_groups, 'indicator') " + // Just to demo
term variable usage.
- "@isIndicator" +
- "}* " + // Optional indicator words (zero or more).
- "term(city)~{# == 'nlpcraft:city'}? " + // Optional city.
- "term(date)~{# == 'nlpcraft:date'}?" // Optional date (overrides
indicator words).
- )
- // NOTE: each samples group will reset conversation STM during
auto-testing.
- @NCIntentSample({
- "Current forecast?",
- "Chance of rain in Berlin now?"
- })
- // NOTE: each samples group will reset conversation STM during
auto-testing.
- @NCIntentSample({
- "Moscow forecast?",
- "Chicago history"
- })
- // NOTE: each samples group will reset conversation STM during
auto-testing.
- @NCIntentSample({
- "What's the local weather forecast?",
- "What's the weather in Moscow?",
- "What's the current forecast for LA?",
- "What is the weather like outside?",
- "How's the weather?",
- "What's the weather forecast for the rest of the week?",
- "What's the weather forecast this week?",
- "What's the weather out there?",
- "Is it cold outside?",
- "Is it hot outside?",
- "Will it rain today?",
- "When it will rain in Delhi?",
- "Is there any possibility of rain in Delhi?",
- "Is it raining now?",
- "Is there any chance of rain today?",
- "Was it raining in Beirut three days ago?",
- "How about yesterday?"
- })
- public NCResult onMatch(
- NCIntentMatch ctx,
- @NCIntentTerm("ind") List<NCEntity> indEntsOpt,
- @NCIntentTerm("city") Optional<NCEntity> cityEntOpt,
- @NCIntentTerm("date") Optional<NCEntity> dateEntOpt
- ) {
- try {
- Instant now = Instant.now();
-
- Instant from = now;
- Instant to = now;
-
- if (indEntsOpt.stream().anyMatch(tok ->
tok.getId().equals("wt:hist")))
- from = from.minus(DAYS_SHIFT_BACK, DAYS);
- else if (indEntsOpt.stream().anyMatch(tok ->
tok.getId().equals("wt:fcast")))
- to = from.plus(DAYS_SHIFT_FORWARD, DAYS);
-
- if (dateEntOpt.isPresent()) { // Date token overrides any
indicators.
- NCEntity dateEnt = dateEntOpt.get();
-
- // TODO: from NNE ?
- from = Instant.now();
- to = Instant.now();
- }
-
- CityData cd = prepGeo(ctx, cityEntOpt); // Handles optional city
too.
-
- double lat = cd.getLatitude();
- double lon = cd.getLongitude();
-
- NCResult res = new NCResult();
-
- res.setType(NCResultType.ASK_RESULT);
- res.setBody(GSON.toJson(from == to ? openWeather.getCurrent(lat,
lon) : openWeather.getTimeMachine(lat, lon, from, to)));
-
- return res;
- }
- catch (OpenWeatherMapException e) {
- throw new NCRejection(e.getLocalizedMessage());
- }
- catch (NCRejection e) {
- throw e;
- }
- catch (Exception e) {
- throw new NCRejection("Weather provider error.", e);
- }
- }
-
- /**
- *
- * @param apiKey OpenWeatherMap API key.
- */
- public WeatherModel(String apiKey) {
- Properties props = new Properties();
- props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner");
- StanfordCoreNLP stanford = new StanfordCoreNLP(props);
-
- NCStanfordNLPTokenParser tp = new NCStanfordNLPTokenParser(stanford);
-
- this.cfg = new NCModelConfig("nlpcraft.weather.ex", "Weather Example
Model", "1.0");
- this.pipeline = new NCModelPipelineBuilder(
- tp,
- new NCStanfordNLPEntityParser(stanford, "date", "city"),
- new NCSemanticEntityParser(new NCEnSemanticPorterStemmer(), tp,
"weather_model.json")
- ).build();
-
- openWeather = new OpenWeatherMapService(apiKey, 5, 7);
- }
-
- /**
- *
- */
- public void close() {
- openWeather.stop();
- }
-
- @Override
- public NCModelConfig getConfig() {
- return cfg;
- }
-
- @Override
- public NCModelPipeline getPipeline() {
- return pipeline;
- }
-}
diff --git
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapException.java
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapException.java
deleted file mode 100644
index 5f64269..0000000
---
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapException.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.weather.openweathermap;
-
-/**
- * Open Weather Map exception.
- */
-public class OpenWeatherMapException extends RuntimeException {
- /**
- * Creates new exception.
- *
- * @param msg Error message.
- */
- public OpenWeatherMapException(String msg) {
- super(msg);
- }
-
- /**
- * Creates new exceptions.
- *
- * @param msg Error message.
- * @param cause Optional cause.
- */
- public OpenWeatherMapException(String msg, Throwable cause) {
- super(msg, cause);
- }
-}
\ No newline at end of file
diff --git
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
deleted file mode 100644
index 2ae8a65..0000000
---
a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/openweathermap/OpenWeatherMapService.java
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
- * 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.weather.openweathermap;
-
-import com.google.gson.Gson;
-import com.google.gson.reflect.TypeToken;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.lang.reflect.Type;
-import java.net.URI;
-import java.net.http.HttpClient;
-import java.net.http.HttpRequest;
-import java.net.http.HttpResponse;
-import java.time.Duration;
-import java.time.Instant;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-
-import static java.time.temporal.ChronoUnit.DAYS;
-import static java.time.temporal.ChronoUnit.SECONDS;
-import static java.util.concurrent.TimeUnit.MILLISECONDS;
-
-/**
- * OpenWeather API weather provider. See https://openweathermap.org/api for
details.
- */
-public class OpenWeatherMapService {
- // GSON response type.
- private static final Type TYPE_RESP = new TypeToken<HashMap<String,
Object>>() { }.getType();
-
- // Access key.
- private final String key;
-
- // Maximum days (looking backwards) in seconds.
- private final int maxDaysBackSecs;
-
- // Maximum days (looking forwards) in seconds.
- private final int maxDaysForwardSecs;
-
- // HTTP client instance.
- private final HttpClient httpClient;
-
- // GSON instance.
- private static final Gson GSON = new Gson();
-
- // Can be configured.
- private final ExecutorService pool =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- /**
- *
- */
- private static final Logger log =
LoggerFactory.getLogger(OpenWeatherMapService.class);
-
- /**
- * Constructor.
- *
- * @param key Service key.
- * @param maxDaysBack Max days (looking back) configuration value.
- * @param maxDaysForward Max days (looking forward) configuration value.
- */
- public OpenWeatherMapService(String key, int maxDaysBack, int
maxDaysForward) {
- this.key = key;
- this.maxDaysBackSecs = maxDaysBack * 24 * 60 * 60;
- this.maxDaysForwardSecs = maxDaysForward * 24 * 60 * 60;
- this.httpClient = HttpClient.newBuilder().build();
- }
-
- /**
- * Stop method.
- */
- public void stop() {
- pool.shutdown();
-
- try {
- //noinspection ResultOfMethodCallIgnored
- pool.awaitTermination(Long.MAX_VALUE, MILLISECONDS);
- }
- catch (InterruptedException e) {
- log.error("Error stopping pool.", e);
- }
- }
-
- /**
- * @param lat Latitude.
- * @param lon Longitude.
- * @param d Date.
- * @return REST call result.
- */
- private Map<String, Object> get(double lat, double lon, long d) {
- return get("https://api.openweathermap.org/data/2.5/onecall?" +
- "lat=" + lat +
- "&lon=" + lon +
- "&dt=" + d +
- "&exclude=current,minutely,hourly,daily,alerts&appid=" + key
- );
- }
-
- /**
- * @param url REST endpoint URL.
- * @return REST call result.
- */
- private Map<String, Object> get(String url) {
- // Ack.
- System.out.println("REST URL prepared: " + url);
-
- HttpRequest req =
HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
-
- try {
- HttpResponse<String> resp = httpClient.send(req,
HttpResponse.BodyHandlers.ofString());
-
- return GSON.fromJson(resp.body(), TYPE_RESP);
- }
- catch (Exception e) {
- e.printStackTrace(System.err);
-
- throw new OpenWeatherMapException("Unable to answer due to weather
data provider error.");
- }
- }
-
- /**
- * See https://openweathermap.org/api/one-call-api#hist_parameter to
extract fields.
- *
- * @param lat Latitude.
- * @param lon Longitude.
- * @param from From date.
- * @param to To date.
- * @return List of REST call results.
- * @throws OpenWeatherMapException Thrown in case of any provider errors.
- */
- public List<Map<String, Object>> getTimeMachine(double lat, double lon,
Instant from, Instant to) throws OpenWeatherMapException {
- assert from != null;
- assert to != null;
-
- log.debug("OpenWeather time machine API call [lat={}, lon={}, from={},
to={}]", lat, lon, from, to);
-
- Instant now = Instant.now();
- long forwardSeconds = to.getEpochSecond() - now.getEpochSecond();
- long backSeconds = now.getEpochSecond() - from.getEpochSecond();
-
- if (Duration.between(from, to).get(SECONDS) > maxDaysForwardSecs &&
forwardSeconds > 0)
- throw new OpenWeatherMapException(String.format("Forward Request
period is too long [from=%s, to=%s]", from, to));
-
- if (Duration.between(from, to).get(SECONDS) > maxDaysBackSecs &&
backSeconds > 0 && to.getEpochSecond() <= now.getEpochSecond())
- throw new OpenWeatherMapException(String.format("Backward Request
period is too long [from=%s, to=%s]", from, to));
-
- long durMs = to.toEpochMilli() - from.toEpochMilli();
-
- int n = (int) (durMs / 86400000 + (durMs % 86400000 == 0 ? 0 : 1));
-
- class Pair {
- private final int shift;
- private final Map<String, Object> reguest;
-
- Pair(int left, Map<String, Object> reguest) {
- this.shift = left;
- this.reguest = reguest;
- }
-
- int getShift() {
- return this.shift;
- }
- Map<String, Object> getReguest() {
- return this.reguest;
- }
- }
-
- return IntStream.range(0, n).
- mapToObj(shift -> pool.submit(() -> new Pair(shift, get(lat, lon,
from.plus(shift, DAYS).getEpochSecond())))).
- map(p -> {
- try {
- return p.get();
- }
- catch (ExecutionException | InterruptedException e) {
- throw new OpenWeatherMapException("Error executing weather
request.", e);
- }
- }).
- sorted(Comparator.comparing(Pair::getShift)).
- map(Pair::getReguest).
- collect(Collectors.toList());
- }
-
- /**
- * See https://openweathermap.org/api/one-call-api#hist_parameter to
extract fields.
- *
- * @param lat Latitude.
- * @param lon Longitude.
- * @return REST call result.
- * @throws OpenWeatherMapException Thrown in case of any provider errors.
- */
- public Map<String, Object> getCurrent(double lat, double lon) throws
OpenWeatherMapException {
- return get("https://api.openweathermap.org/data/2.5/forecast?lat=" +
lat + "&lon=" + lon + "&appid=" + key);
- }
-}
\ No newline at end of file
diff --git a/nlpcraft-examples/weather/src/main/resources/weather_model.json
b/nlpcraft-examples/weather/src/main/resources/weather_model.json
deleted file mode 100644
index e1059b3..0000000
--- a/nlpcraft-examples/weather/src/main/resources/weather_model.json
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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.
- */
-
-{
- "elements": [
- {
- "id": "wt:phen",
- "description": "Weather phenomenon.",
- "synonyms": [
- "{high sea|severe weather|hail|heat wave|cold
wave|derecho|supercell|avalanche|cyclone|wildfire|landslide|firestorm|dust
storm|thunder snow|winter
storm|cloudburst|shower|condensation|precipitation|drizzle|rainstorm|rain
storm|rainfall|rain|storm|sun|sunshine|cloud|hot|cold|dry|wet|wind|hurricane|typhoon|sand-storm|sand
storm|tornado|humid|fog|snow|smog|black
ice|haze|thundershower|thundersnow|sleet|drought|wildfire|blizzard|avalanche|mist|thunderstorm}",
- "{weather
{condition|temp|temperature|data|_}|condition|temp|temperature}"
- ]
- },
- {
- "id": "wt:hist",
- "description": "History (past) indicator.",
- "groups": [
- "indicator"
- ],
- "synonyms": [
- "{history|past|previous}"
- ]
- },
- {
- "id": "wt:curr",
- "description": "Current indicator.",
- "groups": [
- "indicator"
- ],
- "synonyms": [
- "{current|today|now|right now}"
- ]
- },
- {
- "id": "wt:fcast",
- "description": "Forecast (future) indicator.",
- "groups": [
- "indicator"
- ],
- "synonyms": [
- "{future|forecast|prognosis|prediction}"
- ]
- }
- ]
-}
\ No newline at end of file
diff --git
a/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
b/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
deleted file mode 100644
index 4febb07..0000000
---
a/nlpcraft-examples/weather/src/test/java/org/apache/nlpcraft/examples/weather/NCModelValidationSpec.scala
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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.weather
-
-import org.apache.nlpcraft.NCModelClient
-import org.junit.jupiter.api.*
-
-import scala.util.Using
-
-/**
- * JUnit model validation.
- */
-class NCModelValidationSpec:
- private final val propName = "OWM_API_KEY"
-
- private var mdl: WeatherModel = _
-
- @BeforeEach
- def setUp(): Unit =
- // Set your own API key here.
- var apiKey: String = System.getProperty(propName)
- if apiKey == null then apiKey = System.getenv(propName)
- // Default value, used for tests.
- if apiKey == null then apiKey = "8a51a2eb343bf87dc55ffd352f5641ea"
- mdl = new WeatherModel(apiKey)
-
- @AfterEach
- def tearDown(): Unit = if mdl != null then mdl.close()
-
- @Test
- def test(): Unit = Using.resource(new NCModelClient(mdl)) { client =>
client.validateSamples() }
diff --git a/pom.xml b/pom.xml
index 49c430d..867af16 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,8 +11,7 @@
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.
+ 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.
-->
@@ -389,10 +388,8 @@
<profile>
<id>examples</id>
<modules>
- <module>nlpcraft-examples/utils</module>
- <module>nlpcraft-examples/lightswitch</module>
<module>nlpcraft-examples/time</module>
- <module>nlpcraft-examples/weather</module>
+ <module>nlpcraft-examples/lightswitch</module>
<module>nlpcraft-examples/lightswitch-ru</module>
</modules>
</profile>