You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2021/02/25 01:22:44 UTC

[incubator-nlpcraft] branch NLPCRAFT-251 updated: WIP.

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

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


The following commit(s) were added to refs/heads/NLPCRAFT-251 by this push:
     new f56b70e  WIP.
f56b70e is described below

commit f56b70e63dfa75a66e3bca7d93a94146931be7e3
Author: Aaron Radzinski <ar...@apache.org>
AuthorDate: Wed Feb 24 17:22:30 2021 -0800

    WIP.
---
 .../nlpcraft/common/makro/NCMacroParser.scala      | 11 ++-
 .../apache/nlpcraft/model/NCMacroProcessor.java    | 86 ++++++++++++++++++++++
 2 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroParser.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroParser.scala
index 709d088..898fa53 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroParser.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroParser.scala
@@ -161,7 +161,16 @@ class NCMacroParser {
         
         U.distinct(NCMacroCompiler.compile(s).toList map trimDupSpaces map processEscapes)
     }
-    
+
+    /**
+     * Expand given string.
+     *
+     * @param txt Text to expand.
+     */
+    @throws[NCE]
+    def expandJava(txt: String): java.util.Set[String] =
+        expand(txt).toSet.asJava
+
     /**
       * Checks macro name.
       *
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java
new file mode 100644
index 0000000..8a1460e
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java
@@ -0,0 +1,86 @@
+/*
+ * 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.makro.NCMacroParser;
+
+import java.util.Set;
+
+/**
+ * Standalone synonym macro DSL processor.
+ * <p>
+ * This processor provides the same macro support as the built-in macro support in YAML/JSON models. It is
+ * requires when the model is generated programmatically rather than using YAML/JSON, and synonyms need to be
+ * processed in the same way. In such cases, this class can be used to manually process synonyms macro DSL.
+ * <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/src/main/scala/org/apache/nlpcraft/examples/">examples</a>.
+ */
+public class NCMacroProcessor {
+    private final NCMacroParser impl = new NCMacroParser();
+
+    /**
+     * Expands given macro DSL string.
+     *
+     * @param s Macro DSL string to expand.
+     * @return Set of macro expansions for a given macro DSL string.
+     */
+    public Set<String> expand(String s) {
+        return impl.expandJava(s);
+    }
+
+    /**
+     * Adds or overrides given macro.
+     *
+     * @param name Macro name (typically an upper case string).
+     *     It must start with '&lt;' and end with '&gt;' symbol.
+     * @param macro Value of the macro (any arbitrary string).
+     * @return {@code true} if an existing macro was overridden, {@code false} otherwise.
+     */
+    public boolean addMacro(String name, String macro) {
+        boolean f = impl.hasMacro(name);
+
+        impl.addMacro(name, macro);
+
+        return f;
+    }
+
+    /**
+     * Removes macro.
+     *
+     * @param name Name of the macro to remove.
+     * @return {@code true} if given macro was indeed found and removed, {@code false} otherwise.
+     */
+    public boolean removeMacro(String name) {
+        boolean f = impl.hasMacro(name);
+
+        impl.removeMacro(name);
+
+        return f;
+    }
+
+    /**
+     * Tests whether this processor has given macro.
+     *
+     * @param name Name of the macro to test.
+     * @return {@code true} if macro was found, {@code false} otherwise.
+     */
+    public boolean hasMacro(String name) {
+        return impl.hasMacro(name);
+    }
+}