This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-468
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-468 by this push:
new 3c528b7 WIP.
3c528b7 is described below
commit 3c528b77661c6a3f32bc14ec04f06ef5a0fc5f9f
Author: Sergey Kamov <[email protected]>
AuthorDate: Thu Oct 14 20:59:45 2021 +0300
WIP.
---
.../java/org/apache/nlpcraft/NCSpecAdapter.java | 137 +++++++++++++++++++++
1 file changed, 137 insertions(+)
diff --git a/nlpcraft/src/test/java/org/apache/nlpcraft/NCSpecAdapter.java
b/nlpcraft/src/test/java/org/apache/nlpcraft/NCSpecAdapter.java
new file mode 100644
index 0000000..2c53065
--- /dev/null
+++ b/nlpcraft/src/test/java/org/apache/nlpcraft/NCSpecAdapter.java
@@ -0,0 +1,137 @@
+/*
+ * 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;
+
+import org.apache.nlpcraft.model.NCIntentMatch;
+import org.apache.nlpcraft.model.NCIntentRef;
+import org.apache.nlpcraft.model.NCModel;
+import org.apache.nlpcraft.model.NCModelAdapter;
+import org.apache.nlpcraft.model.NCModelBehaviour;
+import org.apache.nlpcraft.model.NCModelConfig;
+import org.apache.nlpcraft.model.NCRejection;
+import org.apache.nlpcraft.model.NCResult;
+import org.apache.nlpcraft.model.builders.NCModelBuilder;
+import org.apache.nlpcraft.model.builders.NCModelConfigBuilder;
+import
org.apache.nlpcraft.model.components.detectors.NCConfiguredWordsDetector;
+import
org.apache.nlpcraft.model.components.detectors.NCDefaultStopWordsDetector;
+import org.apache.nlpcraft.model.components.ner.opennlp.NCOpenNlpNerParser;
+import org.apache.nlpcraft.model.components.ner.synonyms.NCSynonymsNerElement;
+import org.apache.nlpcraft.model.components.ner.synonyms.NCSynonymsNerParser;
+import org.apache.nlpcraft.model.components.ner.synonyms.NCSynonymsNerValue;
+import
org.apache.nlpcraft.model.components.ner.synonyms.NCSynonymsNerValueLoader;
+import
org.apache.nlpcraft.model.components.ner.synonyms.builders.NCSynonymsNerElementBuilder;
+import
org.apache.nlpcraft.model.components.ner.synonyms.builders.NCSynonymsNerParserBuilder;
+import org.apache.nlpcraft.model.components.tokenizer.NCOpenNlpTokenizer;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+public class NCSpecAdapter {
+ private static class SomeClassWithIntents {
+ @NCIntentRef("remove:waypoint")
+ public void x() {
+ // No-op.
+ }
+ }
+
+ private static class MyModelWithIntents extends NCModelAdapter {
+ public MyModelWithIntents(NCModelConfig cfg) {
+ super(cfg);
+ }
+
+ @NCIntentRef("")
+ public void intent() {
+
+ }
+ }
+
+
+ @Test
+ public void test() throws Exception {
+ NCSynonymsNerParser ner1 =
+ new NCSynonymsNerParserBuilder().
+ withElements(new File("ner1.json")).
+ withMaxTotalSynonyms(30).
+ getNlpcraftNerParser();
+
+ NCSynonymsNerParser ner2 =
+ new NCSynonymsNerParserBuilder().
+ withMacros(new HashMap<>() { { put("<ACTION>",
"{turn|switch|dial|let|set|get|put}"); } }).
+ withElements(
+ Arrays.asList(
+ new NCSynonymsNerElementBuilder().
+ withId("elementID1").
+ withSynonyms(Arrays.asList("<ACTION> {on|up|_}",
"<ACTION> qq")).
+ getElement(),
+ new NCSynonymsNerElementBuilder().
+ withId("elementID2").
+ withValueLoader(new NCSynonymsNerValueLoader() {
+ @Override
+ public Set<NCSynonymsNerValue>
load(NCSynonymsNerElement owner) {
+ return null;
+ }
+ }).
+ getElement()
+ )
+ ).
+ getNlpcraftNerParser();
+
+ NCModelConfig cfg =
+ new NCModelConfigBuilder().
+ // Common.
+ withId("modleId").
+ withName("name").
+ withSuspiciousWordsDetector(new
NCConfiguredWordsDetector(Collections.singleton("bad"))).
+ withStopWordsDetector(
+ new NCDefaultStopWordsDetector(
+ new HashSet<>() {{ add("stop1"); add("stop2"); }},
+ null
+ )
+ ).
+ // Nlp tokenizer.
+ withTokenizer(new NCOpenNlpTokenizer()).
+ // NERs.
+ withNerParsers(
+ Arrays.asList(
+ new NCOpenNlpNerParser(new HashSet<>() {{ add("DATE");
add("PERSON") ;}}),
+ ner1,
+ ner2
+ )
+ ).
+ getConfig();
+
+ NCModel mdl = new MyModelWithIntents(cfg);
+
+ mdl.start();
+
+ // NCNlpcraftBuilder scan model for Intents/IntensRef by default. It
can be created any way.
+ NCNlpcraft nlp = new NCNlpcraftBuilder().getNlpcraft(mdl);
+
+ String reqId = nlp.ask("weather today", null, true, "user1");
+
+ nlp.cancel("user1", Collections.singletonList(reqId));
+
+ mdl.stop();
+ }
+}