This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-478 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-478 by this push: new dbea6fc WIP. dbea6fc is described below commit dbea6fcfb89fe5023bc3b6c53d996584de174505 Author: Sergey Kamov <skhdlem...@gmail.com> AuthorDate: Mon Feb 21 22:02:52 2022 +0300 WIP. --- nlpcraft-examples/time/pom.xml | 6 + .../apache/nlpcraft/examples/time/TimeModel.java | 10 +- nlpcraft-examples/{time => utils}/pom.xml | 16 +-- .../examples/utils}/cities/CitiesDataProvider.java | 2 +- .../nlpcraft/examples/utils}/cities/City.java | 2 +- .../nlpcraft/examples/utils}/cities/CityData.java | 2 +- .../examples/utils}/keycdn/GeoManager.java | 6 +- .../examples/utils}/keycdn/beans/GeoDataBean.java | 2 +- .../examples/utils}/keycdn/beans/ResponseBean.java | 2 +- .../utils}/keycdn/beans/ResponseDataBean.java | 2 +- nlpcraft-examples/weather/pom.xml | 6 + .../nlpcraft/examples/weather/WeatherModel.java | 10 +- .../weather/cities/CitiesDataProvider.java | 80 ----------- .../nlpcraft/examples/weather/cities/City.java | 81 ----------- .../nlpcraft/examples/weather/cities/CityData.java | 67 --------- .../examples/weather/keycdn/GeoManager.java | 152 --------------------- .../examples/weather/keycdn/beans/GeoDataBean.java | 122 ----------------- .../weather/keycdn/beans/ResponseBean.java | 81 ----------- .../weather/keycdn/beans/ResponseDataBean.java | 43 ------ pom.xml | 1 + 20 files changed, 40 insertions(+), 653 deletions(-) diff --git a/nlpcraft-examples/time/pom.xml b/nlpcraft-examples/time/pom.xml index a2e5e3b..7988f1c 100644 --- a/nlpcraft-examples/time/pom.xml +++ b/nlpcraft-examples/time/pom.xml @@ -38,6 +38,12 @@ <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 50f535a..2bce8f4 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 @@ -22,11 +22,11 @@ import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.yaml.*; import org.apache.commons.lang3.text.WordUtils; import org.apache.nlpcraft.*; -import org.apache.nlpcraft.examples.time.cities.CitiesDataProvider; -import org.apache.nlpcraft.examples.time.cities.City; -import org.apache.nlpcraft.examples.time.cities.CityData; -import org.apache.nlpcraft.examples.time.keycdn.GeoManager; -import org.apache.nlpcraft.examples.time.keycdn.beans.GeoDataBean; +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.beans.GeoDataBean; 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/time/pom.xml b/nlpcraft-examples/utils/pom.xml similarity index 95% copy from nlpcraft-examples/time/pom.xml copy to nlpcraft-examples/utils/pom.xml index a2e5e3b..587e0c4 100644 --- a/nlpcraft-examples/time/pom.xml +++ b/nlpcraft-examples/utils/pom.xml @@ -21,8 +21,8 @@ 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 Time</name> - <artifactId>nlpcraft-example-time</artifactId> + <name>NLPCraft Examples Utils</name> + <artifactId>nlpcraft-examples-utils</artifactId> <parent> <artifactId>nlpcraft-parent</artifactId> @@ -40,18 +40,18 @@ <!-- 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> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/CitiesDataProvider.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CitiesDataProvider.java similarity index 98% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/CitiesDataProvider.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CitiesDataProvider.java index 0ec13b8..2b14e7a 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/CitiesDataProvider.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CitiesDataProvider.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.cities; +package org.apache.nlpcraft.examples.utils.cities; import org.apache.commons.lang3.tuple.Pair; import org.apache.nlpcraft.NCException; diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/City.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/City.java similarity index 97% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/City.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/City.java index 148cf18..72cf102 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/City.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/City.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.cities; +package org.apache.nlpcraft.examples.utils.cities; import java.util.Objects; diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/CityData.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CityData.java similarity index 97% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/CityData.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CityData.java index 8e71690..4ba42e9 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/cities/CityData.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/cities/CityData.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.cities; +package org.apache.nlpcraft.examples.utils.cities; /** * City data holder. diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/GeoManager.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoManager.java similarity index 96% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/GeoManager.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoManager.java index 6393cb2..d3eef00 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/GeoManager.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/GeoManager.java @@ -15,12 +15,12 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.keycdn; +package org.apache.nlpcraft.examples.utils.keycdn; import com.google.gson.Gson; import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.examples.time.keycdn.beans.GeoDataBean; -import org.apache.nlpcraft.examples.time.keycdn.beans.ResponseBean; +import org.apache.nlpcraft.examples.utils.keycdn.beans.GeoDataBean; +import org.apache.nlpcraft.examples.utils.keycdn.beans.ResponseBean; import java.io.BufferedReader; import java.io.IOException; diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/GeoDataBean.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/GeoDataBean.java similarity index 98% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/GeoDataBean.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/GeoDataBean.java index a6a212b..889a4bd 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/GeoDataBean.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/GeoDataBean.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.keycdn.beans; +package org.apache.nlpcraft.examples.utils.keycdn.beans; import com.google.gson.annotations.SerializedName; diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/ResponseBean.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/ResponseBean.java similarity index 97% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/ResponseBean.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/ResponseBean.java index c95d451..4840cdf 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/ResponseBean.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/ResponseBean.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.keycdn.beans; +package org.apache.nlpcraft.examples.utils.keycdn.beans; /** * Service https://tools.keycdn.com/geo response part bean. diff --git a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/ResponseDataBean.java b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/ResponseDataBean.java similarity index 95% rename from nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/ResponseDataBean.java rename to nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/ResponseDataBean.java index 40b9449..4994867 100644 --- a/nlpcraft-examples/time/src/main/java/org/apache/nlpcraft/examples/time/keycdn/beans/ResponseDataBean.java +++ b/nlpcraft-examples/utils/src/main/java/org/apache/nlpcraft/examples/utils/keycdn/beans/ResponseDataBean.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.nlpcraft.examples.time.keycdn.beans; +package org.apache.nlpcraft.examples.utils.keycdn.beans; /** * Service https://tools.keycdn.com/geo response bean. diff --git a/nlpcraft-examples/weather/pom.xml b/nlpcraft-examples/weather/pom.xml index 31d3fec..4e26bc3 100644 --- a/nlpcraft-examples/weather/pom.xml +++ b/nlpcraft-examples/weather/pom.xml @@ -48,6 +48,12 @@ <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/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 index 1baaba9..20ccb56 100644 --- 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 @@ -31,11 +31,11 @@ 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.weather.cities.CitiesDataProvider; -import org.apache.nlpcraft.examples.weather.cities.City; -import org.apache.nlpcraft.examples.weather.cities.CityData; -import org.apache.nlpcraft.examples.weather.keycdn.GeoManager; -import org.apache.nlpcraft.examples.weather.keycdn.beans.GeoDataBean; +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.beans.GeoDataBean; 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; diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/CitiesDataProvider.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/CitiesDataProvider.java deleted file mode 100644 index 17c37f4..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/CitiesDataProvider.java +++ /dev/null @@ -1,80 +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.cities; - -import org.apache.commons.lang3.tuple.Pair; -import org.apache.nlpcraft.NCException; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Collectors; - -/** - * City-timezone map provider. - */ -public class CitiesDataProvider { - /** - * Creates and returns cities timezone map for all cities with a population > 15000 or capitals. - * - * @return Cities timezone map. - */ - public static Map<City, CityData> get() throws NCException { - try { - List<String> lines = new ArrayList<>(); - - try (BufferedReader reader = - new BufferedReader(new InputStreamReader( - Objects.requireNonNull( - CitiesDataProvider.class. - getClassLoader(). - getResourceAsStream("cities_timezones.txt")) - ))) { - String line = reader.readLine(); - - while (line != null) { - lines.add(line); - - line = reader.readLine(); - } - } - - return - lines.stream(). - filter(p -> !p.startsWith("#")). - map(String::trim). - filter(p -> !p.isEmpty()). - map(p -> p.split("\t")). - map(p -> Arrays.stream(p).map(String::trim).toArray(String[]::new)). - map(arr -> - Pair.of( - new City(arr[0], arr[1]), - new CityData(arr[2], Double.parseDouble(arr[3]), Double.parseDouble(arr[4]))) - ). - collect(Collectors.toMap(Pair::getKey, Pair::getValue)); - } - catch (IOException e) { - throw new NCException("Failed to read data file.", e); - } - } -} diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/City.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/City.java deleted file mode 100644 index 069458a..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/City.java +++ /dev/null @@ -1,81 +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.cities; - -import java.util.Objects; - -/** - * City data object. - */ -public class City { - private final String name; - private final String country; - - /** - * Creates new city object. - * - * @param name City name. - * @param country City country. - */ - public City(String name, String country) { - this.name = name; - this.country = country; - } - - /** - * Gets city name. - * - * @return City name. - */ - public String getName() { - return name; - } - - /** - * Gets city country. - * - * @return City country. - */ - public String getCountry() { - return country; - } - - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - City city = (City) o; - - return (Objects.equals(name, city.name)) && (Objects.equals(country, city.country)); - } - - @Override - public int hashCode() { - int result = name != null ? name.hashCode() : 0; - - result = 31 * result + (country != null ? country.hashCode() : 0); - - return result; - } -} diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/CityData.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/CityData.java deleted file mode 100644 index 7dedaee..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/cities/CityData.java +++ /dev/null @@ -1,67 +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.cities; - -/** - * City data holder. - */ -public class CityData { - private final String timezone; - private final double latitude; - private final double longitude; - - /** - * Creates new city data holder. - * - * @param timezone City timezone - * @param latitude City latitude. - * @param longitude City longitude. - */ - public CityData(String timezone, double latitude, double longitude) { - this.timezone = timezone; - this.latitude = latitude; - this.longitude = longitude; - } - - /** - * Gets timezone. - * - * @return City timezone. - */ - public String getTimezone() { - return timezone; - } - - /** - * Gets latitude. - * - * @return City latitude. - */ - public double getLatitude() { - return latitude; - } - - /** - * Gets longitude. - * - * @return City longitude. - */ - public double getLongitude() { - return longitude; - } -} diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/GeoManager.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/GeoManager.java deleted file mode 100644 index 89ae1ff..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/GeoManager.java +++ /dev/null @@ -1,152 +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.keycdn; - -import com.google.gson.Gson; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.examples.weather.keycdn.beans.GeoDataBean; -import org.apache.nlpcraft.examples.weather.keycdn.beans.ResponseBean; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.zip.GZIPInputStream; - -/** - * Geo data finder. - * - * There are following restrictions to simplify example: - * - * 1. Finder's cache is never cleared. - * 2. Implementation is not thread safe. - * 3. Errors just forwarded to error console. - * 4. Cache, which used to avoid rate-limiting requests (3 requests per second, see https://tools.keycdn.com/geo), - * applied only to successfully received GEO data. - */ -public class GeoManager { - private static final String URL = "https://tools.keycdn.com/geo.json?host="; - private static final Gson GSON = new Gson(); - - private final Map<String, GeoDataBean> cache = new HashMap<>(); - private String externalIp = null; - - /** - * Gets optional geo data by given sentence. - * - * @param sen Sentence. - * @return Geo data. Optional. - */ - public Optional<GeoDataBean> get(NCRequest sen) { - if (externalIp == null) { - try { - externalIp = getExternalIp(); - } - catch (IOException e) { - System.err.println("External IP cannot be detected for localhost."); - - return Optional.empty(); - } - } - - try { - GeoDataBean geo = cache.get(externalIp); - - if (geo != null) - return Optional.of(geo); - - HttpURLConnection conn = (HttpURLConnection)(new URL(URL + externalIp).openConnection()); - - // This service requires "User-Agent" property with its own format. - conn.setRequestProperty("User-Agent", "keycdn-tools:https://nlpcraft.apache.org"); - - try (InputStream in = conn.getInputStream()) { - String enc = conn.getContentEncoding(); - - InputStream stream = enc != null && enc.equals("gzip") ? new GZIPInputStream(in) : in; - - ResponseBean resp = - GSON.fromJson(new BufferedReader(new InputStreamReader(stream)), ResponseBean.class); - - if (!resp.getStatus().equals("success")) - throw new IOException( - MessageFormat.format( - "Unexpected response [status={0}, description={1}]", - resp.getStatus(), - resp.getDescription()) - ); - - geo = resp.getData().getGeo(); - - cache.put(externalIp, geo); - - return Optional.of(geo); - } - } - catch (Exception e) { - System.err.println( - MessageFormat.format( - "Unable to answer due to IP location finder (keycdn) error for host: {0}", - externalIp - ) - ); - - e.printStackTrace(System.err); - - return Optional.empty(); - } - } - - /** - * Gets external IP. - * - * @return External IP. - * @throws IOException If any errors occur. - */ - private static String getExternalIp() throws IOException { - try (BufferedReader in = - new BufferedReader(new InputStreamReader(new URL("https://checkip.amazonaws.com").openStream()))) { - return in.readLine(); - } - } - - /** - * Gets Silicon Valley location. Used as default value for each example service. - * This default location definition added here just for accumulating all GEO manipulation logic in one class. - * - * @return Silicon Valley location. - */ - public GeoDataBean getSiliconValley() { - GeoDataBean geo = new GeoDataBean(); - - geo.setCityName(""); - geo.setCountryName("United States"); - geo.setTimezoneName("America/Los_Angeles"); - geo.setTimezoneName("America/Los_Angeles"); - geo.setLatitude(37.7749); - geo.setLongitude(122.4194); - - return geo; - } -} diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/GeoDataBean.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/GeoDataBean.java deleted file mode 100644 index 31730ff..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/GeoDataBean.java +++ /dev/null @@ -1,122 +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.keycdn.beans; - -import com.google.gson.annotations.SerializedName; - -/** - * Service https://tools.keycdn.com/geo response part bean. Geo data holder. - */ -public class GeoDataBean { - @SerializedName("country_name") private String countryName; - @SerializedName("city") private String cityName; - @SerializedName("latitude") private double latitude; - @SerializedName("longitude") private double longitude; - @SerializedName("timezone") private String timezoneName; - - /** - * Gets country name. - * - * @return Country name. - */ - public String getCountryName() { - return countryName; - } - - /** - * Sets country name. - * - * @param countryName Country name to set. - */ - public void setCountryName(String countryName) { - this.countryName = countryName; - } - - /** - * Gets city name. - * - * @return City name. - */ - public String getCityName() { - return cityName; - } - - /** - * Set city name. - * - * @param cityName City name to set. - */ - public void setCityName(String cityName) { - this.cityName = cityName; - } - - /** - * Gets latitude. - * - * @return Latitude. - */ - public double getLatitude() { - return latitude; - } - - /** - * Sets latitude. - * - * @param latitude Latitude to set. - */ - public void setLatitude(double latitude) { - this.latitude = latitude; - } - - /** - * Gets longitude. - * - * @return Longitude. - */ - public double getLongitude() { - return longitude; - } - - /** - * Sets longitude. - * - * @param longitude Longitude to set. - */ - public void setLongitude(double longitude) { - this.longitude = longitude; - } - - /** - * Get timezone name. - * - * @return Timezone name. - */ - public String getTimezoneName() { - return timezoneName; - } - - /** - * Sets timezone name. - * - * @param timezoneName Timezone name to set. - */ - public void setTimezoneName(String timezoneName) { - this.timezoneName = timezoneName; - } -} - diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/ResponseBean.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/ResponseBean.java deleted file mode 100644 index 46d53fb..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/ResponseBean.java +++ /dev/null @@ -1,81 +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.keycdn.beans; - -/** - * Service https://tools.keycdn.com/geo response part bean. - */ -public class ResponseBean { - private String status; - private String description; - private ResponseDataBean data; - - /** - * Gets response status. - * - * @return Response status. - */ - public String getStatus() { - return status; - } - - /** - * Sets response status. - * - * @param status Response status to set. - */ - public void setStatus(String status) { - this.status = status; - } - - /** - * Gets response data. - * - * @return Response data. - */ - public ResponseDataBean getData() { - return data; - } - - /** - * Sets response data. - * - * @param data Response data to set. - */ - public void setData(ResponseDataBean data) { - this.data = data; - } - - /** - * Gets response description. - * - * @return Response description. - */ - public String getDescription() { - return description; - } - - /** - * Sets response description. - * - * @param description Response description to set. - */ - public void setDescription(String description) { - this.description = description; - } -} diff --git a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/ResponseDataBean.java b/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/ResponseDataBean.java deleted file mode 100644 index 3b5250a..0000000 --- a/nlpcraft-examples/weather/src/main/java/org/apache/nlpcraft/examples/weather/keycdn/beans/ResponseDataBean.java +++ /dev/null @@ -1,43 +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.keycdn.beans; - -/** - * Service https://tools.keycdn.com/geo response bean. - */ -public class ResponseDataBean { - private GeoDataBean geo; - - /** - * Gets geo data holder. - * - * @return Geo data holder. - */ - public GeoDataBean getGeo() { - return geo; - } - - /** - * Sets get data holder. - * - * @param geo Geo data holder to set. - */ - public void setGeo(GeoDataBean geo) { - this.geo = geo; - } -} diff --git a/pom.xml b/pom.xml index 2c4ac70..ade79d9 100644 --- a/pom.xml +++ b/pom.xml @@ -389,6 +389,7 @@ <profile> <id>examples</id> <modules> + <module>nlpcraft-examples/utils</module> <module>nlpcraft-examples/echo</module> <module>nlpcraft-examples/helloworld</module> <module>nlpcraft-examples/lightswitch</module>