This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-305
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit 2054954361911567dfbd1d34f4f21e82cecd6226
Author: Sergey Kamov <[email protected]>
AuthorDate: Mon Apr 26 17:49:31 2021 +0300

    Comments added.
---
 .../nplcraft/example/minecraft/NCExampleMod.java   | 38 ++++++++++++++++-
 .../example/minecraft/utils/GameFilesDump.java     |  2 +-
 ...FIllMatchProcessor.kt => FillMatchProcessor.kt} | 33 +++++++++++----
 .../nlpcraft/example/minecraft/MinecraftModel.kt   | 36 +++++++++-------
 .../org/apache/nlpcraft/example/minecraft/Utils.kt | 49 ----------------------
 .../nlpcraft/example/minecraft/ValueLoaders.kt     |  5 +++
 .../example/minecraft/NCMinecraftModelSpec.kt      |  2 +-
 7 files changed, 91 insertions(+), 74 deletions(-)

diff --git 
a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCExampleMod.java
 
b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCExampleMod.java
index 6a5b3c8..90c9c9f 100644
--- 
a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCExampleMod.java
+++ 
b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCExampleMod.java
@@ -41,15 +41,23 @@ import java.nio.file.Paths;
 import java.util.HashSet;
 import java.util.Set;
 
+/**
+ * Minecraft MOD. It contains simple REST client, which forward user request 
to server, and replaces initial request by server answers.
+ * Note that it requires started NLPCraft server ad probe with loaded 
`nlpcraft.minecraft.ex` model.
+ */
 @Mod("nlpcraft_mod")
 public class NCExampleMod {
+    // Initial configuration values for communication with NLPCraft server.
+    // These values can be overridden by 'nlpcraft-settings.json' file values.
     private static final String DFLT_EMAIL = "[email protected]";
     private static final String DFLT_PSWD = "admin";
     private static final String DFLT_HOST = "0.0.0.0";
     private static final int DFLT_PORT = 8081;
 
-    private static final Logger LOGGER = LogManager.getLogger();
+    // Model ID which should be loaded in probe.
     private static final String MODEL_ID = "nlpcraft.minecraft.ex";
+
+    private static final Logger LOGGER = LogManager.getLogger();
     private static final Gson GSON = new Gson();
 
     private final Set<String> convCmds = new HashSet<>();
@@ -59,6 +67,7 @@ public class NCExampleMod {
     private MinecraftServer server;
     private String token;
 
+    // Serialization and deserialization classes.
     private static class AskParams {
         private String mdlId;
         private String acsTok;
@@ -143,6 +152,13 @@ public class NCExampleMod {
         return s;
     }
 
+    /**
+     * Ask method.
+     * @param txt Request.
+     * @return Server response.
+     *
+     * @throws Exception If any error occurs.
+     */
     private NCResponse askProbe(String txt) throws Exception {
         assert baseUrl != null;
 
@@ -163,7 +179,7 @@ public class NCExampleMod {
             resp = post("ask/sync", GSON.toJson(params), NCResponse.class);
         }
         catch (UnauthorizedException e) {
-            // Token can be expired.
+            // Token can be expired, it tries to connect and process given 
command again.
             this.token = signin();
 
             params.acsTok = this.token;
@@ -181,10 +197,25 @@ public class NCExampleMod {
         return resp;
     }
 
+    /**
+     * Signin method.
+     * @return Server token.
+     *
+     * @throws Exception If any error occurs.
+     */
     private String signin() throws Exception {
         return post("signin", GSON.toJson(creds), NCSignResponse.class).acsTok;
     }
 
+    /**
+     * REST post method.
+     * @param url Request URL.
+     * @param postJson Parameters.
+     * @param clazz Response class type.
+     * @return Response.
+     *
+     * @throws Exception If any error occurs.
+     */
     private <T> T post(String url, String postJson, Class<T> clazz) throws 
Exception {
         assert baseUrl != null;
 
@@ -214,6 +245,9 @@ public class NCExampleMod {
         }
     }
 
+    /**
+     * Loads settings. It tries to read `"nlpcraft-settings.json` properties 
file or use default values if file is not found.
+     */
     private void loadSettings() {
         try (
             Reader reader =
diff --git 
a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/GameFilesDump.java
 
b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/GameFilesDump.java
index b2e06a2..59d6d7e 100644
--- 
a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/GameFilesDump.java
+++ 
b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/GameFilesDump.java
@@ -26,7 +26,7 @@ import net.minecraft.util.registry.Registry;
 import net.minecraftforge.registries.ForgeRegistryEntry;
 
 /**
- * Utility for getting data from minecraft.
+ * Utility for getting data from minecraft. These values are used for 
preparing synonyms for user defined elements.
  */
 public class GameFilesDump {
     private final static Gson GSON = new Gson();
diff --git 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/FIllMatchProcessor.kt
 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/FillMatchProcessor.kt
similarity index 78%
rename from 
nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/FIllMatchProcessor.kt
rename to 
nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/FillMatchProcessor.kt
index f576a52..7bf20ad 100644
--- 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/FIllMatchProcessor.kt
+++ 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/FillMatchProcessor.kt
@@ -22,7 +22,25 @@ import 
org.apache.nlpcraft.example.minecraft.MinecraftObjectValueLoader.Companio
 import org.apache.nlpcraft.model.*
 import java.util.*
 
-class FIllMatchProcessor {
+/**
+ * Special processor for support 'fillIntent' intent processing.
+ * Is is designed as separated class to simplify main model class.
+ */
+class FillMatchProcessor {
+    internal data class Coordinate(val x: Int = 0, val y: Int = 0, val z: Int 
= 0) {
+        override fun toString(): String {
+            return "$x $y $z"
+        }
+
+        fun relative(): String {
+            return "~$x ~$y ~$z"
+        }
+
+        fun relativeRotated(): String {
+            return "^$x ^$y ^$z"
+        }
+    }
+
     companion object {
         fun process(
             ctx: NCIntentMatch,
@@ -67,16 +85,17 @@ class FIllMatchProcessor {
                 x.partTokens.stream()
                     .filter { it.id == "nlpcraft:num" }
                     .findAny()
-                    .map { it.toInt() }
+                    .map { it.meta<Double>("nlpcraft:num:from").toInt() }
             }.orElse(default)
         }
 
         private fun findPlayer(position: NCToken): String {
-            return position.partTokens.stream()
-                .filter { it.id == "mc:player" }
-                .findAny()
-                .orElseThrow { AssertionError("Player wasn't found") }
-                .player()
+            val part = position.partTokens.stream()
+                    .filter { it.id == "mc:player" }
+                    .findAny()
+                    .orElseThrow { AssertionError("Player wasn't found") }
+
+            return if (part.lemma == "i" || part.lemma == "my") "@p" else 
part.originalText ?: "@p"
         }
     }
 }
diff --git 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt
 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt
index 58dfe74..ad793d5 100644
--- 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt
+++ 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt
@@ -23,8 +23,19 @@ import 
org.apache.nlpcraft.example.minecraft.MinecraftObjectValueLoader.Companio
 import org.apache.nlpcraft.model.*
 import java.util.*
 
-@Suppress("unused")
+/**
+ * Minecraft example data model.
+ * TODO:
+ * <p>
+ * See 'README.md' file in the same folder for running and testing 
instructions.
+ */
 class MinecraftModel : NCModelFileAdapter("minecraft.yaml") {
+    private fun checkAmbiguous(ctx: NCIntentMatch) {
+        if (ctx.isAmbiguous) {
+            throw NCRejection("Ambiguous request")
+        }
+    }
+
     @NCIntentRef("weatherIntent")
     @NCIntentSample(
         "make it rain",
@@ -33,9 +44,7 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") {
         "heavy storm is coming"
     )
     fun onWeatherMatch(ctx: NCIntentMatch, @NCIntentTerm("arg") tok: NCToken): 
NCResult {
-        if (ctx.isAmbiguous) {
-            throw NCRejection("Ambiguous request")
-        }
+        checkAmbiguous(ctx)
 
         return NCResult.text("weather ${tok.id}")
     }
@@ -48,9 +57,7 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") {
         "it's midnight"
     )
     fun onTimeMatch(ctx: NCIntentMatch, @NCIntentTerm("arg") tok: NCToken): 
NCResult {
-        if (ctx.isAmbiguous) {
-            throw NCRejection("Ambiguous request")
-        }
+        checkAmbiguous(ctx)
 
         val time: Int = when (tok.id) {
             "morning" -> 23000
@@ -67,7 +74,7 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") {
 
     @NCIntentRef("giveIntent")
     @NCIntentSample(
-        "give me iron sword",
+        "give my iron sword",
         "give me 10 grass blocks",
         "give #PlayerName a jigsaw",
         "give #PlayerName 1 kilogram of feathers",
@@ -79,13 +86,14 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") 
{
         @NCIntentTerm("action") target: NCToken,
         @NCIntentTerm("quantity") quantity: Optional<NCToken>
     ): NCResult {
-        if (ctx.isAmbiguous) {
-            throw NCRejection("Ambiguous request")
-        }
+        checkAmbiguous(ctx)
 
         val itemRegistry = dumps["item"]!![item.value]!!
-        val player = target.partTokens[1].player()
-        val itemQuantity = quantity.map(NCToken::toInt).orElse(1)
+
+        val part = target.partTokens[1]
+        val player = if (part.lemma == "i" || part.lemma == "my") "@p" else 
part.originalText ?: "@p"
+
+        val itemQuantity = if (quantity.isPresent) 
quantity.get().meta<Double>("nlpcraft:num:from").toInt() else 1
 
         return NCResult.text("give $player $itemRegistry $itemQuantity")
     }
@@ -106,6 +114,6 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") 
{
         @NCIntentTerm("len") length: Optional<NCToken>,
         @NCIntentTerm("position") position: NCToken,
     ): NCResult {
-        return FIllMatchProcessor.process(ctx, shape, block, length, position)
+        return FillMatchProcessor.process(ctx, shape, block, length, position)
     }
 }
diff --git 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/Utils.kt
 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/Utils.kt
deleted file mode 100644
index 31fdf47..0000000
--- 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/Utils.kt
+++ /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.
- *
- */
-
-package org.apache.nlpcraft.example.minecraft
-
-import org.apache.nlpcraft.model.NCToken
-
-private var firstPersonWords = setOf("me", "my", "i")
-
-internal fun NCToken.normText(): String {
-    return this.meta("nlpcraft:nlp:normtext")
-}
-
-internal fun NCToken.toInt(): Int {
-    return this.meta<Double>("nlpcraft:num:from").toInt()
-}
-
-internal fun NCToken.player(): String {
-    return if (firstPersonWords.contains(this.normText())) "@p" else 
this.originalText ?: "@p"
-}
-
-internal data class Coordinate(val x: Int = 0, val y: Int = 0, val z: Int = 0) 
{
-    override fun toString(): String {
-        return "$x $y $z"
-    }
-
-    fun relative(): String {
-        return "~$x ~$y ~$z"
-    }
-
-    fun relativeRotated(): String {
-        return "^$x ^$y ^$z"
-    }
-}
\ No newline at end of file
diff --git 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/ValueLoaders.kt
 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/ValueLoaders.kt
index 89b44e3..def4d26 100644
--- 
a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/ValueLoaders.kt
+++ 
b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/ValueLoaders.kt
@@ -26,6 +26,11 @@ import org.apache.nlpcraft.model.NCModelFileAdapter
 import org.apache.nlpcraft.model.NCValue
 import org.apache.nlpcraft.model.NCValueLoader
 
+/**
+ * Data loader from JSON data files.
+ * These files which prepared via 'minectaft-mod' module 
org.apache.nplcraft.example.minecraft.utils.GameFilesDump
+ * for this supported `minecraft` server version.
+ */
 class MinecraftObjectValueLoader : NCValueLoader {
     private data class Dump(val version: String, val data: Map<String, String>)
 
diff --git 
a/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt
 
b/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt
index b32fa53..34e7f0d 100644
--- 
a/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt
+++ 
b/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt
@@ -22,7 +22,7 @@ import org.apache.nlpcraft.NCTestContext
 import org.apache.nlpcraft.NCTestEnvironment
 import org.junit.jupiter.api.Test
 import kotlin.test.assertEquals
-
+import kotlin.test.assertTrue
 
 @NCTestEnvironment(model = MinecraftModel::class, startClient = true)
 class NCMinecraftModelSpec : NCTestContext() {

Reply via email to