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 5e96bee WIP on refactoring.
5e96bee is described below
commit 5e96bee95423fa0950fd7791fc86798f60da51b8
Author: Nikita Ivanov <>
AuthorDate: Mon Nov 29 14:12:01 2021 -0800
WIP on refactoring.
---
.../main/scala/org/apache/nlpcraft/NCStart.scala | 23 ----
.../org/apache/nlpcraft/common/util/NCUtils.scala | 1 -
.../org/apache/nlpcraft/model/NCMetadata.java | 120 ---------------------
.../scala/org/apache/nlpcraft/model/NCResult.java | 3 +-
.../scala/org/apache/nlpcraft/model/NCToken.java | 49 +++------
.../nlpcraft/model/impl/NCMetadataAdapter.java | 50 ---------
6 files changed, 15 insertions(+), 231 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
deleted file mode 100644
index 7fb367c..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/NCStart.scala
+++ /dev/null
@@ -1,23 +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
- *
- * https://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
-
-/**
- *
- */
-object NCStart extends App
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index 5838a2d..6cb7341 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -526,7 +526,6 @@ object NCUtils extends LazyLogging:
try
body(this)
logger.trace(s"Thread exited: $name")
-
catch
case _: InterruptedException => logger.trace(s"Thread
interrupted: $name")
case e: Throwable => prettyError(logger, s"Unexpected
error during '$name' thread execution:", e)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMetadata.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMetadata.java
deleted file mode 100644
index 91c10f4..0000000
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMetadata.java
+++ /dev/null
@@ -1,120 +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.model;
-
-import org.apache.nlpcraft.common.*;
-import java.util.*;
-
-/**
- * Provides support for mutable runtime-only metadata.
- * <p>
- * Read full documentation in <a target=_
href="https://nlpcraft.apache.org/data-model.html">Data Model</a> section and
review
- * <a target=_
href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft-examples">examples</a>.
- */
-public interface NCMetadata {
- /**
- * Factory for creating metadata out of standard map.
- *
- * @param map Map to convert to metadata.
- * @return Newly created metadata container.
- */
- static NCMetadata apply(Map<String, Object> map) {
- return () -> map;
- }
-
- /**
- * Gets mutable metadata underlying container.
- * Returned map can be used to mutate the metadata or perform any other
operations.
- *
- * @return Mutable, underlying metadata container.
- * @see #meta(String)
- * @see #metaOpt(String)
- * @see #meta(String, Object)
- */
- Map<String, Object> getMetadata();
-
- /**
- * Shortcut method to get given optional metadata property. Equivalent to:
- * <pre class="brush: java">
- * Optional.ofNullable((T)getMetadata().get(prop));
- * </pre>
- *
- * @param prop Metadata property name.
- * @param <T> Type of the metadata property.
- * @return Metadata optional property value.
- */
- @SuppressWarnings("unchecked")
- default <T> Optional<T> metaOpt(String prop) {
- return Optional.ofNullable((T)getMetadata().get(prop));
- }
-
- /**
- * Shortcut method to get given metadata property. Equivalent to:
- * <pre class="brush: java">
- * (T)getMetadata().get(prop);
- * </pre>
- *
- * @param prop Metadata property name.
- * @param <T> Type of the metadata property.
- * @return Metadata property value or {@code null} if given metadata
property not found.
- */
- @SuppressWarnings("unchecked")
- default <T> T meta(String prop) {
- return (T)getMetadata().get(prop);
- }
-
- /**
- * Shortcut method to get given mandatory metadata property. Equivalent to:
- * <pre class="brush: java">
- * T t = (T)getMetadata().get(prop);
- * if (t == null)
- * throw new NCException("Mandatory metadata property not found: "
+ prop);
- * else
- * return t;
- * </pre>
- *
- * @param prop Metadata property name.
- * @param <T> Type of the metadata property.
- * @return Metadata property value or throws an exception if given
metadata property not found.
- * @throws NCException Thrown if given metadata property not found.
- */
- default <T> T metax(String prop) throws NCException {
- T t = meta(prop);
-
- if (t == null)
- throw new NCException("Mandatory metadata property not found: " +
prop);
- else
- return t;
- }
-
- /**
- * Shortcut method to get given metadata property. Equivalent to:
- * <pre class="brush: java">
- * getMetadata().get(tokId, prop, dflt);
- * </pre>
- *
- * @param prop Metadata property name.
- * @param dflt Default value to return if specified one isn't set.
- * @param <T> Type of the metadata property.
- * @return Metadata property value or default value if one isn't set.
- */
- @SuppressWarnings("unchecked")
- default <T> T meta(String prop, T dflt) {
- return (T)getMetadata().getOrDefault(prop, dflt);
- }
-}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
index 5ff6681..84c4b08 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCResult.java
@@ -19,7 +19,6 @@ package org.apache.nlpcraft.model;
import org.apache.nlpcraft.common.NCException;
import org.apache.nlpcraft.common.util.*;
-import org.apache.nlpcraft.model.impl.NCMetadataAdapter;
import java.io.Serializable;
import java.util.Collection;
@@ -58,7 +57,7 @@ import java.util.Collection;
* accordingly. For example, the REST client interfacing between NLPCraft and
Amazon Alexa or Apple HomeKit can only
* accept {@code text} result type and ignore everything else.
*/
-public class NCResult extends NCMetadataAdapter implements Serializable,
NCMetadata {
+public class NCResult implements Serializable {
/** Data Model result text. */
private String body;
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCToken.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCToken.java
index b815511..4e153ca 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCToken.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCToken.java
@@ -27,7 +27,7 @@ import java.util.List;
*
* @see NCElement
*/
-public interface NCToken extends NCMetadata {
+public interface NCToken {
/**
* Gets reference to the model this token belongs to.
*
@@ -36,11 +36,11 @@ public interface NCToken extends NCMetadata {
NCModel getModel();
/**
- * Gets ID of the server request this token is part of.
+ * Gets ID of the request this token is part of.
*
- * @return ID of the server request this token is part of.
+ * @return ID of the request this token is part of.
*/
- String getServerRequestId();
+ String getRequestId();
/**
* If this token represents user defined model element this method returns
@@ -144,9 +144,7 @@ public interface NCToken extends NCMetadata {
*
* @return Whether this token is a stopword.
*/
- default boolean isStopWord() {
- return meta("nlpcraft:nlp:stopword");
- }
+ boolean isStopWord();
/**
* A shortcut method checking whether this token represents a free word. A
free word is a
@@ -160,10 +158,7 @@ public interface NCToken extends NCMetadata {
*
* @return Whether this token is a freeword.
*/
- default boolean isFreeWord() {
- return meta("nlpcraft:nlp:freeword");
- }
-
+ boolean isFreeWord();
/**
* A shortcut method that gets original user input text for this token.
* <p>
@@ -175,9 +170,7 @@ public interface NCToken extends NCMetadata {
*
* @return Original user input text for this token.
*/
- default String getOriginalText() {
- return meta("nlpcraft:nlp:origtext");
- }
+ String getOriginalText();
/**
* A shortcut method that gets index of this token in the sentence.
@@ -190,9 +183,7 @@ public interface NCToken extends NCMetadata {
*
* @return Index of this token in the sentence.
*/
- default int getIndex() {
- return meta("nlpcraft:nlp:index");
- }
+ int getIndex();
/**
* A shortcut method that gets normalized user input text for this token.
@@ -205,9 +196,7 @@ public interface NCToken extends NCMetadata {
*
* @return Normalized user input text for this token.
*/
- default String getNormalizedText() {
- return meta("nlpcraft:nlp:normtext");
- }
+ String getNormalizedText();
/**
* A shortcut method on whether this token is a swear word. NLPCraft has
built-in list of
@@ -221,9 +210,7 @@ public interface NCToken extends NCMetadata {
*
* @return Whether this token is a swear word.
*/
- default boolean isSwear() {
- return meta("nlpcraft:nlp:swear");
- }
+ boolean isSwear();
/**
* A shortcut method to get lemma of this token, i.e. a canonical form of
this word. Note that
@@ -240,9 +227,7 @@ public interface NCToken extends NCMetadata {
*
* @return Lemma of this token, i.e. a canonical form of this word.
*/
- default String getLemma() {
- return meta("nlpcraft:nlp:lemma");
- }
+ String getLemma();
/**
* A shortcut method to get stem of this token. Note that stemming and
lemmatization allow to reduce
@@ -258,9 +243,7 @@ public interface NCToken extends NCMetadata {
*
* @return Stem of this token.
*/
- default String getStem() {
- return meta("nlpcraft:nlp:stem");
- }
+ String getStem();
/**
* A shortcut method to get Penn Treebank POS tag for this token. Note
that additionally to standard Penn
@@ -274,9 +257,7 @@ public interface NCToken extends NCMetadata {
*
* @return Penn Treebank POS tag for this token.
*/
- default String getPos() {
- return meta("nlpcraft:nlp:pos");
- }
+ String getPos();
/**
* A shortcut method that gets internal globally unique system ID of the
token.
@@ -288,7 +269,5 @@ public interface NCToken extends NCMetadata {
*
* @return Internal globally unique system ID of the token.
*/
- default String getUnid() {
- return meta("nlpcraft:nlp:unid");
- }
+ String getUnid();
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/NCMetadataAdapter.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/NCMetadataAdapter.java
deleted file mode 100644
index b3b9d09..0000000
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/impl/NCMetadataAdapter.java
+++ /dev/null
@@ -1,50 +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.model.impl;
-
-import org.apache.nlpcraft.model.NCMetadata;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Abstract adapter for metadata.
- */
-public abstract class NCMetadataAdapter implements NCMetadata {
- final private Map<String, Object> meta;
-
- /**
- *
- */
- protected NCMetadataAdapter() {
- meta = new HashMap<>();
- }
-
- /**
- *
- * @param meta Metadata container to use.
- */
- protected NCMetadataAdapter(Map<String, Object> meta) {
- this.meta = meta;
- }
-
- @Override
- public Map<String, Object> getMetadata() {
- return meta;
- }
-}