This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/master by this push:
new 5cfcdd0 Added Java and Kotlin examples for LightSwitch.
5cfcdd0 is described below
commit 5cfcdd02226cc02cb5742ed77e2e2a496d21b8d1
Author: Aaron Radzinski <[email protected]>
AuthorDate: Thu Jan 14 16:30:09 2021 -0800
Added Java and Kotlin examples for LightSwitch.
---
.../examples/lightswitch/LightSwitchJavaModel.java | 80 ++++++++++++++++++++++
.../examples/lightswitch/LightSwitchKotlinModel.kt | 80 ++++++++++++++++++++++
pom.xml | 68 ++++++++++++++++++
3 files changed, 228 insertions(+)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java
new file mode 100644
index 0000000..1d7923c
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchJavaModel.java
@@ -0,0 +1,80 @@
+/*
+ * 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.lightswitch;
+
+import org.apache.nlpcraft.model.*;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * This example provides very simple implementation for NLI-powered light
switch.
+ * You can say something like this:
+ * <ul>
+ * <li>"Turn the lights off in the entire house."</li>
+ * <li>"Switch on the illumination in the master bedroom closet."</li>
+ * </ul>
+ * You can easily modify intent callbacks to perform the actual light
switching using
+ * HomeKit or Arduino-based controllers.
+ * <p>
+ * See 'README.md' file in the same folder for running and testing
instructions.
+ */
+public class LightSwitchJavaModel extends NCModelFileAdapter {
+ public LightSwitchJavaModel() {
+ // Loading the model from the file in the classpath.
+
super("org/apache/nlpcraft/examples/lightswitch/lightswitch_model.yaml");
+ }
+
+ /**
+ * Intent and its on-match callback.
+ *
+ * @param actTok Token from 'act' term (guaranteed to be one).
+ * @param locToks Tokens from 'loc' term (zero or more).
+ * @return Query result to be sent to the REST caller.
+ */
+ @NCIntentRef("ls")
+ @NCIntentSample({
+ "Turn the lights off in the entire house.",
+ "Switch on the illumination in the master bedroom closet.",
+ "Get the lights on.",
+ "Lights up in the kitchen.",
+ "Please, put the light out in the upstairs bedroom.",
+ "Set the lights on in the entire house.",
+ "Turn the lights off in the guest bedroom.",
+ "Could you please switch off all the lights?",
+ "Dial off illumination on the 2nd floor.",
+ "Please, no lights!",
+ "Kill off all the lights now!",
+ "No lights in the bedroom, please.",
+ "Light up the garage, please!",
+ "Kill the illumination now!"
+ })
+ NCResult onMatch(
+ @NCIntentTerm("act") NCToken actTok,
+ @NCIntentTerm("loc") List<NCToken> locToks) {
+ String status = actTok.getId().equals("ls:on") ? "on" : "off";
+ String locations = locToks.isEmpty() ?
+ "entire house" :
+ locToks.stream().map(t ->
(String)t.meta("nlpcraft:nlp:origtext")).collect(Collectors.joining(", "));
+
+ // Add HomeKit, Arduino or other integration here.
+
+ // By default - just return a descriptive action string.
+ return NCResult.text("Lights are [" + status + "] in [" +
locations.toLowerCase() + "].");
+ }
+}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt
new file mode 100644
index 0000000..57c02bc
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchKotlinModel.kt
@@ -0,0 +1,80 @@
+/*
+ * 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.lightswitch
+
+import org.apache.nlpcraft.model.NCModelFileAdapter
+import org.apache.nlpcraft.model.NCIntentRef
+import org.apache.nlpcraft.model.NCIntentSample
+import org.apache.nlpcraft.model.NCIntentTerm
+import org.apache.nlpcraft.model.NCToken
+import org.apache.nlpcraft.model.NCResult
+import java.util.stream.Collectors
+
+/**
+ * This example provides very simple implementation for NLI-powered light
switch.
+ * You can say something like this:
+ *
+ * * "Turn the lights off in the entire house."
+ * * "Switch on the illumination in the master bedroom closet."
+ *
+ * You can easily modify intent callbacks to perform the actual light
switching using
+ * HomeKit or Arduino-based controllers.
+ *
+ *
+ * See 'README.md' file in the same folder for running and testing
instructions.
+ */
+class LightSwitchKotlinModel :
NCModelFileAdapter("org/apache/nlpcraft/examples/lightswitch/lightswitch_model.yaml")
{
+ /**
+ * Intent and its on-match callback.
+ *
+ * @param actTok Token from 'act' term (guaranteed to be one).
+ * @param locToks Tokens from 'loc' term (zero or more).
+ * @return Query result to be sent to the REST caller.
+ */
+ @NCIntentRef("ls")
+ @NCIntentSample(
+ "Turn the lights off in the entire house.",
+ "Switch on the illumination in the master bedroom closet.",
+ "Get the lights on.",
+ "Lights up in the kitchen.",
+ "Please, put the light out in the upstairs bedroom.",
+ "Set the lights on in the entire house.",
+ "Turn the lights off in the guest bedroom.",
+ "Could you please switch off all the lights?",
+ "Dial off illumination on the 2nd floor.",
+ "Please, no lights!",
+ "Kill off all the lights now!",
+ "No lights in the bedroom, please.",
+ "Light up the garage, please!",
+ "Kill the illumination now!"
+ )
+ fun onMatch(
+ @NCIntentTerm("act") actTok: NCToken,
+ @NCIntentTerm("loc") locToks: List<NCToken>
+ ): NCResult {
+ val status = if (actTok.id == "ls:on") "on" else "off"
+ val locations = if (locToks.isEmpty()) "entire house" else
locToks.stream()
+ .map { t: NCToken -> t.meta<Any>("nlpcraft:nlp:origtext") as
String }
+ .collect(Collectors.joining(", "))
+
+ // Add HomeKit, Arduino or other integration here.
+
+ // By default - just return a descriptive action string.
+ return NCResult.text("Lights are [" + status + "] in [" +
locations.toLowerCase() + "].")
+ }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 1c59570..6852d13 100644
--- a/pom.xml
+++ b/pom.xml
@@ -73,6 +73,7 @@
<properties>
<!-- Major Scala version. -->
<scala.base>2.12</scala.base>
+ <kotlin.ver>1.4.21</kotlin.ver>
<!-- Versions. -->
<ignite.ver>2.9.1</ignite.ver>
@@ -162,6 +163,7 @@
the end user, when required.
-->
<stanford.corenlp.ver>3.9.2</stanford.corenlp.ver>
+ <kotlin.version>1.4.30-M1</kotlin.version>
</properties>
<dependencyManagement>
@@ -501,6 +503,48 @@
</dependencies>
</dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-stdlib-jdk8</artifactId>
+ <version>${kotlin.ver}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-test</artifactId>
+ <version>${kotlin.ver}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <repositories>
+ <repository>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ <id>bintray.kotlin.eap</id>
+ <name>Bintray Kotlin EAP Repository</name>
+ <url>https://dl.bintray.com/kotlin/kotlin-eap</url>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ <id>bintray.kotlin.eap</id>
+ <name>Bintray Kotlin EAP Repository</name>
+ <url>https://dl.bintray.com/kotlin/kotlin-eap</url>
+ </pluginRepository>
+ </pluginRepositories>
+
<build>
<plugins>
<plugin>
@@ -593,6 +637,30 @@
</execution>
</executions>
</plugin>
+ <plugin>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-maven-plugin</artifactId>
+ <version>${kotlin.ver}</version>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>test-compile</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>test-compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <jvmTarget>1.8</jvmTarget>
+ </configuration>
+ </plugin>
</plugins>
</build>