This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-206
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-206 by this push:
new e5d8ac1 WIP.
e5d8ac1 is described below
commit e5d8ac1c297b543bbee1d8d161b1a36f5c8de8ba
Author: Aaron Radzinski <[email protected]>
AuthorDate: Wed Feb 17 13:28:50 2021 -0800
WIP.
---
.../apache/nlpcraft/common/debug/NCLogHolder.scala | 4 +-
.../apache/nlpcraft/examples/echo/EchoModel.scala | 2 +-
.../scala/org/apache/nlpcraft/model/NCContext.java | 13 +----
.../org/apache/nlpcraft/model/NCConversation.java | 19 ++-----
.../scala/org/apache/nlpcraft/model/NCRequest.java | 9 ++--
.../nlpcraft/model/NCTokenPredicateContext.java | 51 ++++++++++++++++++
.../nlpcraft/model/NCTokenPredicateResult.java | 60 ++++++++++++++++++++++
.../model/intent/impl/ver2/NCBaseDslCompiler.scala | 50 ++++++++++++++++--
.../model/intent/utils/ver2/NCDslTermContext.scala | 5 +-
.../probe/mgrs/nlp/NCProbeEnrichmentManager.scala | 1 -
.../probe/mgrs/nlp/impl/NCRequestImpl.scala | 2 +-
11 files changed, 174 insertions(+), 42 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/debug/NCLogHolder.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/debug/NCLogHolder.scala
index 5e9b5b6..b8cb4f8 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/debug/NCLogHolder.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/debug/NCLogHolder.scala
@@ -183,8 +183,8 @@ class NCLogHolder extends Serializable {
company = compJs,
remoteAddress = req.getRemoteAddress.orElse(null),
clientAgent = req.getClientAgent.orElse(null),
- data = if (req.getData.isPresent) {
- val str = req.getData.get
+ data = if (req.getJsonData.isPresent) {
+ val str = req.getJsonData.get
try
NCUtils.jsonToObject(str)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/echo/EchoModel.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/echo/EchoModel.scala
index 6384062..a24b91a 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/echo/EchoModel.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/echo/EchoModel.scala
@@ -54,7 +54,7 @@ class EchoModel extends NCModelAdapter("nlpcraft.echo.ex",
"Echo Example Model",
val map = new util.HashMap[String, Any]()
- map.put("srvReqId", ctx.getServerRequestId)
+ map.put("srvReqId", ctx.getRequest.getServerRequestId)
map.put("sentence", sm)
NCResult.json(new Gson().toJson(map))
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCContext.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCContext.java
index 9e5028d..a75cb8e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCContext.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCContext.java
@@ -28,10 +28,10 @@ import java.util.*;
*/
public interface NCContext extends Serializable {
/**
- * Tests if given token is part of this query.
+ * Tests if given token is part of the query this context is associated
with.
*
* @param tok Token to check.
- * @return {@code true} if given token is from this sentence, {@code
false} otherwise.
+ * @return {@code true} if given token is from the sentence associated
with this context, {@code false} otherwise.
*/
boolean isOwnerOf(NCToken tok);
@@ -44,15 +44,6 @@ public interface NCContext extends Serializable {
Collection<? extends NCVariant> getVariants();
/**
- * Gets globally unique ID of the current request. Server request is
defined as a processing of
- * a one user input sentence. Note that the model can be accessed (i.e.
intents can be matched) multiple times
- * during the processing of a single request, and therefore multiple
contexts may return the same server request ID.
- *
- * @return Server request ID.
- */
- String getServerRequestId();
-
- /**
* Gets model instance for this query.
*
* @return Model.
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCConversation.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCConversation.java
index 10f4e7e..6352dfa 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCConversation.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCConversation.java
@@ -18,7 +18,6 @@
package org.apache.nlpcraft.model;
import java.util.List;
-import java.util.Map;
import java.util.function.Predicate;
/**
@@ -31,15 +30,15 @@ import java.util.function.Predicate;
* Note also that tokens in STM automatically expire (i.e. context is
"forgotten") after a certain period of time and/or
* based on the depth of the conversation since the last mention.
* <p>
- * You can also maintain user state-machine between requests using method
{@link #getUserData()}. This
- * method returns mutable thread-safe container that can hold any arbitrary
user data while supporting the same
+ * You can also maintain user state-machine between requests using metadata.
Conversation's metadata is a
+ * mutable thread-safe container that can hold any arbitrary user data while
supporting the same
* expiration logic as the rest of the conversation elements (i.e. tokens and
previously matched intent IDs).
*
* @see NCContext#getConversation()
* @see NCModelView#getConversationDepth()
* @see NCModelView#getConversationTimeout()
*/
-public interface NCConversation {
+public interface NCConversation extends NCMetadata {
/**
* Gets an ordered list of tokens stored in the conversation STM for the
current
* user and data model. Tokens in the returned list are ordered by their
conversational depth, i.e.
@@ -89,16 +88,4 @@ public interface NCConversation {
* @param filter Dialog flow filter based on IDs of previously matched
intents.
*/
void clearDialog(Predicate<String/* Intent ID. */> filter);
-
- /**
- * Gets modifiable user data container that can be used to store user data
in the conversation.
- * Note that this data will expire the same way as other elements in the
conversation (i.e. tokens and
- * previously matched intents).
- * <p>
- * Note that you should not cache or clone the data from this container
because it won't be properly expired
- * in that case. You can, however, cache the return reference itself, if
required.
- *
- * @return Mutable and thread-safe user data container.
- */
- Map<String, Object> getUserData();
}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCRequest.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCRequest.java
index ffb91fb..ebca5f2 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCRequest.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCRequest.java
@@ -20,7 +20,7 @@ package org.apache.nlpcraft.model;
import java.util.*;
/**
- * Supplemental information about the user request.
+ * Information about the user request.
*
* @see NCContext#getRequest()
*/
@@ -82,10 +82,9 @@ public interface NCRequest {
Optional<String> getClientAgent();
/**
- * Gets optional JSON data passed in with user request.
+ * Gets optional JSON data passed in with the user request.
*
- * @return Optional user input data.
- * @see NCUser#getProperties()
+ * @return Optional JSON data.
*/
- Optional<String> getData();
+ Optional<String> getJsonData();
}
\ No newline at end of file
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCTokenPredicateContext.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCTokenPredicateContext.java
new file mode 100644
index 0000000..5888f07
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCTokenPredicateContext.java
@@ -0,0 +1,51 @@
+/*
+ * 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 java.util.Optional;
+
+/**
+ * Context passed into custom user-defined token predicate. Token predicates
can be used in intent and synonym DSL.
+ * <p>
+ * Read full documentation in <a target=_
href="https://nlpcraft.apache.org/intent-matching.html">Intent Matching</a>
section and review
+ * <a target=_
href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/">examples</a>.
+ *
+ * @see NCTokenPredicateResult
+ */
+public interface NCTokenPredicateContext {
+ /**
+ * Gets information about the user request.
+ *
+ * @return Information about the user request.
+ */
+ NCRequest getRequest();
+
+ /**
+ * Gets a token this predicate is matching against.
+ *
+ * @return Token this predicate is matching against.
+ */
+ NCToken getToken();
+
+ /**
+ * Gets metadata of the intent this token predicate is associated with, if
available.
+ *
+ * @return Intent metadata, if available.
+ */
+ Optional<NCMetadata> getIntentMeta();
+}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCTokenPredicateResult.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCTokenPredicateResult.java
new file mode 100644
index 0000000..5d368be
--- /dev/null
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCTokenPredicateResult.java
@@ -0,0 +1,60 @@
+/*
+ * 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;
+
+/**
+ * Result value of user-defined token predicate. Token predicates can be used
in intent and synonym DSL.
+ * <p>
+ * Read full documentation in <a target=_
href="https://nlpcraft.apache.org/intent-matching.html">Intent Matching</a>
section and review
+ * <a target=_
href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/">examples</a>.
+ *
+ * @see NCTokenPredicateContext
+ */
+public class NCTokenPredicateResult {
+ private final boolean result;
+ private final boolean wasTokenUsed;
+
+ /**
+ * Creates token predicate result.
+ *
+ * @param result Token predicate result.
+ * @param wasTokenUsed Whether or not a token was used by this predicate
(if result is {@code true}).
+ */
+ NCTokenPredicateResult(boolean result, boolean wasTokenUsed) {
+ this.result = result;
+ this.wasTokenUsed = wasTokenUsed;
+ }
+
+ /**
+ * Gets result of this predicate.
+ *
+ * @return Predicate result.
+ */
+ boolean getResult() {
+ return result;
+ }
+
+ /**
+ * Whether or not a token was used by this predicate (if result is {@code
true}).
+ *
+ * @return {@code true} if token was used by this predicate, {@code false}
otherwise.
+ */
+ boolean wasTokenUsed() {
+ return wasTokenUsed;
+ }
+}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCBaseDslCompiler.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCBaseDslCompiler.scala
index 0e914dc..1f6ce2f 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCBaseDslCompiler.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCBaseDslCompiler.scala
@@ -476,6 +476,7 @@ trait NCBaseDslCompiler {
def doReqMeta(): Unit = get1Str() match { case (s, _) ⇒
pushAny(termCtx.reqMeta.get(s).orNull, false) }
def doSysMeta(): Unit = get1Str() match { case (s, _) ⇒
pushAny(U.sysEnv(s).orNull, false) }
def doUserMeta(): Unit = get1Str() match { case (s, _) ⇒
pushAny(termCtx.usrMeta.get(s).orNull, false) }
+ def doConvMeta(): Unit = get1Str() match { case (s, _) ⇒
pushAny(termCtx.convMeta.get(s).orNull, false) }
def doCompMeta(): Unit = get1Str() match { case (s, _) ⇒
pushAny(termCtx.compMeta.get(s).orNull, false) }
def doIntentMeta(): Unit = get1Str() match { case (s, _) ⇒
pushAny(termCtx.intentMeta.get(s).orNull, false) }
@@ -523,6 +524,21 @@ trait NCBaseDslCompiler {
def doAtan2(): Unit = get2Doubles() match { case (a1: JDouble, a2:
JDouble, f) ⇒ pushDouble(Math.atan2(a1, a2), f) }
/*
+ * User operations.
+ */
+ def doUserId(): Unit = pushLong(termCtx.req.getUser.getId, false)
+
+ /*
+ * Company operations.
+ */
+ def doCompId(): Unit = pushLong(termCtx.req.getCompany.getId,
false)
+
+ /*
+ * Request operations.
+ */
+ def doReqId(): Unit = pushAny(termCtx.req.getServerRequestId,
false)
+
+ /*
* Date-time operations.
*/
def doYear(): Unit = pushLong(LocalDate.now.getYear,false)
@@ -555,6 +571,7 @@ trait NCBaseDslCompiler {
case "user_meta" ⇒ doUserMeta()
case "company_meta" ⇒ doCompMeta()
case "sys_meta" ⇒ doSysMeta()
+ case "conv_meta" ⇒ doConvMeta()
// Converts JSON to map.
case "json" ⇒ doJson()
@@ -572,6 +589,31 @@ trait NCBaseDslCompiler {
case "start_idx" ⇒ pushLong(tok.getStartCharIndex, true)
case "end_idx" ⇒ pushLong(tok.getEndCharIndex, true)
+ // Request data.
+ case "req_id" ⇒ doReqId()
+ case "req_normtext" ⇒
+ case "req_tstamp" ⇒
+ case "req_addr" ⇒
+ case "req_agent" ⇒
+
+ // User data.
+ case "user_id" ⇒ doUserId()
+ case "user_fname" ⇒
+ case "user_lname" ⇒
+ case "user_email" ⇒
+ case "user_admin" ⇒
+ case "user_signup_tstamp" ⇒
+
+ // Company data.
+ case "comp_id" ⇒ doCompId()
+ case "comp_name" ⇒
+ case "comp_website" ⇒
+ case "comp_country" ⇒
+ case "comp_region" ⇒
+ case "comp_city" ⇒
+ case "comp_addr" ⇒
+ case "comp_postcode" ⇒
+
// String functions.
case "trim" ⇒ doTrim()
case "strip" ⇒ doTrim()
@@ -580,10 +622,10 @@ trait NCBaseDslCompiler {
case "is_alpha" ⇒ doIsAlpha()
case "is_alphanum" ⇒ doIsAlphaNum()
case "is_whitespace" ⇒ doIsWhitespace()
- case "is_numeric" ⇒ doIsNum()
- case "is_numeric_space" ⇒ doIsNumSpace()
- case "is_alpha_space" ⇒ doIsAlphaSpace()
- case "is_alphanum_space" ⇒ doIsAlphaNumSpace()
+ case "is_num" ⇒ doIsNum()
+ case "is_numspace" ⇒ doIsNumSpace()
+ case "is_alphaspace" ⇒ doIsAlphaSpace()
+ case "is_alphanumspace" ⇒ doIsAlphaNumSpace()
case "substring" ⇒
case "index" ⇒
case "regex" ⇒
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTermContext.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTermContext.scala
index 9b9457a..073ba27 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTermContext.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/utils/ver2/NCDslTermContext.scala
@@ -18,6 +18,7 @@
package org.apache.nlpcraft.model.intent.utils.ver2
import org.apache.nlpcraft.common.ScalaMeta
+import org.apache.nlpcraft.model.NCRequest
/**
*
@@ -26,5 +27,7 @@ case class NCDslTermContext(
intentMeta: ScalaMeta,
reqMeta: ScalaMeta,
usrMeta: ScalaMeta,
- compMeta: ScalaMeta
+ compMeta: ScalaMeta,
+ convMeta: ScalaMeta,
+ req: NCRequest
)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
index e304822..9c61b50 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
@@ -567,7 +567,6 @@ object NCProbeEnrichmentManager extends NCService with
NCOpenCensusModelStats {
val ctx: NCContext = new NCContext {
override lazy val getRequest: NCRequest = req
override lazy val getModel: NCModel = mdl.model
- override lazy val getServerRequestId: String = srvReqId
override lazy val getConversation: NCConversation = new
NCConversation {
override def getTokens: util.List[NCToken] = conv.getTokens()
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
index 129a5a7..2941324 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
@@ -40,7 +40,7 @@ case class NCRequestImpl(meta: Map[String, Any], srvReqId:
String) extends NCReq
override lazy val getReceiveTimestamp: Long =
meta("RECEIVE_TSTAMP").asInstanceOf[Long] // UTC.
override lazy val getClientAgent: Optional[String] = getOpt("USER_AGENT")
override lazy val getRemoteAddress: Optional[String] =
getOpt("REMOTE_ADDR")
- override lazy val getData: Optional[String] = getOpt("DATA")
+ override lazy val getJsonData: Optional[String] = getOpt("DATA")
override lazy val getCompany: NCCompany = new NCCompanyImpl(
meta("COMPANY_ID").asInstanceOf[Long],
meta("COMPANY_NAME").asInstanceOf[String],