This is an automated email from the ASF dual-hosted git repository. aradzinski pushed a commit to branch master_test in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit a6412259ac76584a2b7965ca5660154f6641fe06 Author: Aaron Radzinski <[email protected]> AuthorDate: Thu Dec 16 12:12:39 2021 -0800 WIP (refactoring, cleanup). --- .../nlp/entity/parser/custom/NCElement.java | 72 -------- .../nlp/entity/parser/custom/NCElementBuilder.java | 187 --------------------- .../nlp/entity/parser/custom/NCEntityParser.java | 56 ------ .../entity/parser/custom/NCFileEntityParser.java | 59 ------- .../internal/nlp/entity/parser/custom/NCValue.java | 49 ------ .../nlp/entity/parser/custom/NCValueLoader.java | 64 ------- .../parser/opennlp/NCOpenNlpEntityParseImpl.scala | 33 ---- .../parser/opennlp/NCOpenNlpEntityParser.java | 53 ------ .../nlp/entity/parser/opennlp/NCOpenNlpModel.java | 54 ------ .../enricher/NCEnDictionaryTokenEnricher.java | 12 +- .../token/enricher/NCEnQuotesTokenEnricher.java | 12 +- .../enricher/NCEnSwearWordsTokenEnricher.java | 11 +- .../enricher/impl/NCEnDictionaryImpl.scala} | 44 ++--- .../NCEnQuotesImpl.scala} | 27 ++- .../NCEnSwearWordsImpl.scala} | 27 ++- .../nlpcraft/lightswitch/LightSwitchModel.java | 85 ---------- .../nlpcraft/lightswitch/LightSwitchModelTest.java | 124 -------------- 17 files changed, 54 insertions(+), 915 deletions(-) diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCElement.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCElement.java deleted file mode 100644 index 793131e..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCElement.java +++ /dev/null @@ -1,72 +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.internal.nlp.entity.parser.custom; - -import org.apache.nlpcraft.NCParameterized; - -import java.util.List; -import java.util.Optional; -import java.util.Set; - -/** - * - */ -public interface NCElement extends NCParameterized { - /** - * - * @return - */ - String getId(); - - /** - * - * @return - */ - String getDescription(); - - /** - * - * @return - */ - Optional<String> getParentId(); - - /** - * - * @return - */ - Set<String> getGroups(); - - /** - * - * @return - */ - Set<NCValue> getValues(); - - /** - * - * @return - */ - Optional<NCValueLoader> getValuesLoader(); - - /** - * - * @return - */ - List<String> getSynonyms(); -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCElementBuilder.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCElementBuilder.java deleted file mode 100644 index 4d4d335..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCElementBuilder.java +++ /dev/null @@ -1,187 +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.internal.nlp.entity.parser.custom; - -import org.apache.nlpcraft.NCParameterizedAdapter; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; - -/** - * - */ -public class NCElementBuilder { - private static class NCElementImpl extends NCParameterizedAdapter implements NCElement { - private final String id; - private final String desc; - - private String parentId; - private Set<String> groups; - private Set<NCValue> values; - private NCValueLoader valuesLoader; - private List<String> synonyms; - - NCElementImpl(String id, String desc) { - this.id = id; - this.desc = desc; - } - - void setParentId(String parentId) { - this.parentId = parentId; - } - - void setGroups(Set<String> groups) { - this.groups = groups; - } - - void setValues(Set<NCValue> values) { - this.values = values; - } - - void setValuesLoader(NCValueLoader valuesLoader) { - this.valuesLoader = valuesLoader; - } - - void setSynonyms(List<String> synonyms) { - this.synonyms = synonyms; - } - - @Override - public String getId() { - return id; - } - - @Override - public String getDescription() { - return desc; - } - - @Override - public Optional<String> getParentId() { - return Optional.ofNullable(parentId); - } - - @Override - public Set<String> getGroups() { - return groups == null ? Collections.emptySet() : groups; - } - - @Override - public Set<NCValue> getValues() { - return values == null ? Collections.emptySet() : values; - } - - @Override - public Optional<NCValueLoader> getValuesLoader() { - return Optional.ofNullable(valuesLoader); - } - - @Override - public List<String> getSynonyms() { - return synonyms; - } - } - - private NCElementImpl impl = null; - - /** - * - * @param id - * @param desc - */ - public NCElementBuilder(String id, String desc) { - impl = new NCElementImpl(id, desc); - } - - /** - * - * @param parentId - * @return - */ - public NCElementBuilder withParentId(String parentId) { - impl.setParentId(parentId); - - return this; - } - - /** - * - * @param groups - * @return - */ - public NCElementBuilder withGroups(Set<String> groups) { - impl.setGroups(groups); - - return this; - } - - /** - * - * @param values - * @return - */ - public NCElementBuilder withValues(Set<NCValue> values) { - impl.setValues(values); - - return this; - } - - /** - * - * @param loader - * @return - */ - public NCElementBuilder withValuesLoader(NCValueLoader loader) { - impl.setValuesLoader(loader); - - return this; - } - - /** - * - * @param props - * @return - */ - public NCElementBuilder withProperties(Map<String, String> props) { - // TODO: - return this; - } - - /** - * - * @param syns - * @return - */ - public NCElementBuilder withSynonyms(List<String> syns) { - impl.setSynonyms(syns); - - return this; - } - - /** - * - * @return - */ - public NCElement make() { - // TODO: add validation - return impl; - } -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCEntityParser.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCEntityParser.java deleted file mode 100644 index da94fc3..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCEntityParser.java +++ /dev/null @@ -1,56 +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.internal.nlp.entity.parser.custom; - -import org.apache.nlpcraft.NCEntity; -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; - -import java.util.List; - -/** - * - */ -public class NCEntityParser implements org.apache.nlpcraft.NCEntityParser { - private final List<NCMacro> macros; - private final List<NCElement> elements; - - /** - * - * @param macros - * @param elements - */ - public NCEntityParser(List<NCMacro> macros, List<NCElement> elements) { - this.macros = macros; - this.elements = elements; - } - - /** - * - * @param elements - */ - public NCEntityParser(List<NCElement> elements) { - this(null, elements); - } - - @Override - public List<NCEntity> parse(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - return null; - } -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCFileEntityParser.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCFileEntityParser.java deleted file mode 100644 index 3e11057..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCFileEntityParser.java +++ /dev/null @@ -1,59 +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.internal.nlp.entity.parser.custom; - -import org.apache.nlpcraft.NCEntity; -import org.apache.nlpcraft.NCEntityParser; -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; - -import java.util.List; -import java.util.Map; -import java.io.File; - -/** - * - */ -public class NCFileEntityParser implements NCEntityParser { - private final File file; - private final Map<String, NCValueLoader> loaders; - - /** - * - * @param file - * @param loaders - */ - public NCFileEntityParser(File file, Map<String, NCValueLoader> loaders) { - this.file = file; - this.loaders = loaders; - } - - /** - * - * @param file - */ - public NCFileEntityParser(File file) { - this(file, null); - } - - @Override - public List<NCEntity> parse(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - return null; - } -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCValue.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCValue.java deleted file mode 100644 index 0d0acb6..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCValue.java +++ /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 - * - * 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.internal.nlp.entity.parser.custom; - -import java.util.List; - -/** - * TODO: fix javadoc - * - * Model element's value. - * <p> - * Each model element can generally be recognized either by one of its synonyms or values. Elements and their values - * are analogous to types and instances of that type in programming languages. Each value - * has a name and optional set of its own synonyms by which that value, and ultimately its element, can be - * recognized by. Note that value name itself acts as an implicit synonym even when no additional synonyms added - * for that value. - * - * @see NCElement#getValues() - */ -public interface NCValue { - /** - * Gets value name. - * - * @return Value name. - */ - String getName(); - - /** - * Gets optional list of value's synonyms. - * - * @return Potentially empty list of value's synonyms. - */ - List<String> getSynonyms(); -} \ No newline at end of file diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCValueLoader.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCValueLoader.java deleted file mode 100644 index 090dc2d..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCValueLoader.java +++ /dev/null @@ -1,64 +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.internal.nlp.entity.parser.custom; - -import java.util.Set; - -/** - * TODO: fix javadoc - * - * Dynamic value loader that can be used by model elements to dynamically load or create their values. Note that - * the primary use case for this interface is the dynamic value loading for the models defines in JSON/YAML - * presentation. However, it's not technically limited to that use case only, and this interface can be - * set programmatically when model elements are created programmatically too. - * <p> - * <b>JSON</b> - * <br> - * When using JSON/YAML model presentation element values can be defined statically. However, in some - * cases, it is required to load these values from external sources like database or REST services while - * keeping the rest of the model declaration static (i.e. in JSON/YAML). To accomplish this you can - * define <code>valueLoader</code> property and provide a fully qualified class name that implements - * this interface. During the model instantiation an instance of that class will be created once per - * each model and class of loader and method {@link #load(NCElement)} will be called to load - * element's values. Note that you can use both statically defined values (i.e. <code>values</code> property) - * and dynamically loaded values together and they will be merged: - * <pre class="brush: js, highlight: [11]"> - * "elements": [ - * { - * "id": "my:id", - * "description": "My description.", - * "values": [ - * { - * "name": "name1", - * "synonyms": ["syn1", "syn2"] - * } - * ], - * "valueLoader": "my.package.MyLoader" - * } - * ] - * </pre> - */ -public interface NCValueLoader { - /** - * Loads values for given model element. - * - * @param owner Model element to which this value loader belongs to. - * @return Set of values, potentially empty but never {@code null}. - */ - Set<NCValue> load(NCElement owner); -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpEntityParseImpl.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpEntityParseImpl.scala deleted file mode 100644 index 37f60db..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpEntityParseImpl.scala +++ /dev/null @@ -1,33 +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.internal.nlp.entity.parser.opennlp - -import opennlp.tools.lemmatizer.DictionaryLemmatizer -import opennlp.tools.postag.{POSModel, POSTagger, POSTaggerME} -import org.apache.nlpcraft.{NCEntity, NCEntityParser, NCModelConfig, NCRequest, NCToken, NCTokenParser} - -import scala.concurrent.ExecutionContext -import scala.util.Using -import scala.util.control.Exception.catching -import java.io.InputStream -import java.util -import scala.jdk.CollectionConverters.ListHasAsScala - -class NCOpenNlpEntityParseImpl() extends NCEntityParser { - override def parse(req: NCRequest, cfg: NCModelConfig, toks: util.List[NCToken]): util.List[NCEntity] = ??? -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpEntityParser.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpEntityParser.java deleted file mode 100644 index d1ed3d2..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpEntityParser.java +++ /dev/null @@ -1,53 +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.internal.nlp.entity.parser.opennlp; - -import org.apache.nlpcraft.NCEntity; -import org.apache.nlpcraft.NCEntityParser; -import org.apache.nlpcraft.NCException; -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * - */ -public class NCOpenNlpEntityParser implements NCEntityParser { - private final List<NCOpenNlpModel> mdls; - - /** - * - * @param mdls - */ - public NCOpenNlpEntityParser(List<NCOpenNlpModel> mdls) { - this.mdls = mdls; - } - - @Override - public void start() { - // TODO: - } - - @Override - public List<NCEntity> parse(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - return null; - } -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpModel.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpModel.java deleted file mode 100644 index 056ab49..0000000 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/opennlp/NCOpenNlpModel.java +++ /dev/null @@ -1,54 +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.internal.nlp.entity.parser.opennlp; - -import java.io.File; - -/** - * - */ -public class NCOpenNlpModel { - private String elementId; - private File path; - - /** - * - * @param elementId - * @param path - */ - public NCOpenNlpModel(String elementId, File path) { - this.elementId = elementId; - this.path = path; - } - - /** - * - * @return - */ - public String getElementId() { - return elementId; - } - - /** - * - * @return - */ - public File getPath() { - return path; - } -} diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java index c9895e6..1c54626 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java @@ -17,19 +17,19 @@ package org.apache.nlpcraft.internal.nlp.token.enricher; -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; -import org.apache.nlpcraft.NCTokenEnricher; - +import org.apache.nlpcraft.*; +import org.apache.nlpcraft.internal.nlp.token.enricher.impl.NCEnDictionaryImpl; import java.util.List; /** * */ public class NCEnDictionaryTokenEnricher implements NCTokenEnricher { + private NCEnDictionaryImpl impl; + @Override public void enrich(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - + assert impl != null; + impl.enrich(req, cfg, toks); } } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnQuotesTokenEnricher.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnQuotesTokenEnricher.java index 14243a7..d22fe6f 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnQuotesTokenEnricher.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnQuotesTokenEnricher.java @@ -17,19 +17,19 @@ package org.apache.nlpcraft.internal.nlp.token.enricher; -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; -import org.apache.nlpcraft.NCTokenEnricher; - +import org.apache.nlpcraft.*; import java.util.List; +import org.apache.nlpcraft.internal.nlp.token.enricher.impl.NCEnQuotesImpl; /** * */ public class NCEnQuotesTokenEnricher implements NCTokenEnricher { + private NCEnQuotesImpl impl; + @Override public void enrich(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - + assert impl != null; + impl.enrich(req, cfg, toks); } } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnSwearWordsTokenEnricher.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnSwearWordsTokenEnricher.java index ed61cdb..0ac4276 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnSwearWordsTokenEnricher.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnSwearWordsTokenEnricher.java @@ -17,10 +17,8 @@ package org.apache.nlpcraft.internal.nlp.token.enricher; -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; -import org.apache.nlpcraft.NCTokenEnricher; +import org.apache.nlpcraft.*; +import org.apache.nlpcraft.internal.nlp.token.enricher.impl.NCEnSwearWordsImpl; import java.util.List; @@ -28,8 +26,11 @@ import java.util.List; * */ public class NCEnSwearWordsTokenEnricher implements NCTokenEnricher { + private NCEnSwearWordsImpl impl; + @Override public void enrich(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - + assert impl != null; + impl.enrich(req, cfg, toks); } } diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCMacro.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnDictionaryImpl.scala similarity index 61% rename from nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCMacro.java rename to nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnDictionaryImpl.scala index 428df40..acbe9d3 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/entity/parser/custom/NCMacro.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnDictionaryImpl.scala @@ -15,38 +15,18 @@ * limitations under the License. */ -package org.apache.nlpcraft.internal.nlp.entity.parser.custom; +package org.apache.nlpcraft.internal.nlp.token.enricher.impl -/** - * - */ -public class NCMacro { - private final String id; - private final String macro; - - /** - * - * @param id - * @param macro - */ - public NCMacro(String id, String macro) { - this.id = id; - this.macro = macro; - } - - /** - * - * @return - */ - public String getId() { - return id; - } +import org.apache.nlpcraft.* +/** + * + */ +class NCEnDictionaryImpl: /** - * - * @return - */ - public String getMacro() { - return macro; - } -} \ No newline at end of file + * + * @param req + * @param cfg + * @param toks + */ + def enrich(req: NCRequest, cfg: NCModelConfig, toks: java.util.List[NCToken]): Unit = ??? diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnQuotesImpl.scala similarity index 65% copy from nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java copy to nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnQuotesImpl.scala index c9895e6..64a9ee9 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnQuotesImpl.scala @@ -15,21 +15,18 @@ * limitations under the License. */ -package org.apache.nlpcraft.internal.nlp.token.enricher; +package org.apache.nlpcraft.internal.nlp.token.enricher.impl -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; -import org.apache.nlpcraft.NCTokenEnricher; - -import java.util.List; +import org.apache.nlpcraft.* /** - * - */ -public class NCEnDictionaryTokenEnricher implements NCTokenEnricher { - @Override - public void enrich(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - - } -} + * + */ +class NCEnQuotesImpl: + /** + * + * @param req + * @param cfg + * @param toks + */ + def enrich(req: NCRequest, cfg: NCModelConfig, toks: java.util.List[NCToken]): Unit = ??? diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnSwearWordsImpl.scala similarity index 65% copy from nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java copy to nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnSwearWordsImpl.scala index c9895e6..6f74460 100644 --- a/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/NCEnDictionaryTokenEnricher.java +++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/internal/nlp/token/enricher/impl/NCEnSwearWordsImpl.scala @@ -15,21 +15,18 @@ * limitations under the License. */ -package org.apache.nlpcraft.internal.nlp.token.enricher; +package org.apache.nlpcraft.internal.nlp.token.enricher.impl -import org.apache.nlpcraft.NCModelConfig; -import org.apache.nlpcraft.NCRequest; -import org.apache.nlpcraft.NCToken; -import org.apache.nlpcraft.NCTokenEnricher; - -import java.util.List; +import org.apache.nlpcraft.* /** - * - */ -public class NCEnDictionaryTokenEnricher implements NCTokenEnricher { - @Override - public void enrich(NCRequest req, NCModelConfig cfg, List<NCToken> toks) { - - } -} + * + */ +class NCEnSwearWordsImpl: + /** + * + * @param req + * @param cfg + * @param toks + */ + def enrich(req: NCRequest, cfg: NCModelConfig, toks: java.util.List[NCToken]): Unit = ??? diff --git a/nlpcraft/src/test/java/org/apache/nlpcraft/lightswitch/LightSwitchModel.java b/nlpcraft/src/test/java/org/apache/nlpcraft/lightswitch/LightSwitchModel.java deleted file mode 100644 index b719283..0000000 --- a/nlpcraft/src/test/java/org/apache/nlpcraft/lightswitch/LightSwitchModel.java +++ /dev/null @@ -1,85 +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.lightswitch; - -import org.apache.nlpcraft.NCModelAdapter; -import org.apache.nlpcraft.*; -import org.apache.nlpcraft.NCModelConfig; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * - */ -public class LightSwitchModel extends NCModelAdapter { - /** - * - * @param cfg - */ - public LightSwitchModel(NCModelConfig cfg) { - super(cfg); - } - - /** - * - * @param actTok - * @param locToks - * @return - */ - @NCIntent("intent=ls term(act)={has(tok_groups, 'act')} term(loc)={# == 'ls:loc'}*") - @NCIntentSample({ - "Turn the lights off in the entire house.", - "Turn off all lights now", - "Switch on the illumination in the master bedroom closet.", - "Off the lights on the 1st floor", - "Get the lights on.", - "Lights up in the kitchen.", - "Please, put the light out in the upstairs bedroom.", - "Set the lights on in the entire house.", - "Turn the lights off in the guest bedroom.", - "Could you please switch off all the lights?", - "Dial off illumination on the 2nd floor.", - "Turn down lights in 1st floor bedroom", - "Lights on at second floor kitchen", - "Please, no lights!", - "Kill off all the lights now!", - "Down the lights in the garage", - "Lights down in the kitchen!", - "Turn up the illumination in garage and master bedroom", - "Turn down all the light now!", - "No lights in the bedroom, please.", - "Light up the garage, please!", - "Kill the illumination now!" - }) - NCResult onMatch( - @NCIntentTerm("act") NCEntity actTok, - @NCIntentTerm("loc") List<NCEntity> locToks) { - String status = actTok.getId().equals("ls:on") ? "on" : "off"; - String locations = locToks.isEmpty() ? - "entire house" : - locToks.stream(). - map(p -> p.getTokens().stream().map(x -> x.getOriginalText()).collect(Collectors.joining(" "))). - collect(Collectors.joining(", ")); - - return new NCResult( - "Lights are [" + status + "] in [" + locations.toLowerCase() + "].", - NCResultType.ASK_RESULT - ); - } -} diff --git a/nlpcraft/src/test/java/org/apache/nlpcraft/lightswitch/LightSwitchModelTest.java b/nlpcraft/src/test/java/org/apache/nlpcraft/lightswitch/LightSwitchModelTest.java deleted file mode 100644 index 3371481..0000000 --- a/nlpcraft/src/test/java/org/apache/nlpcraft/lightswitch/LightSwitchModelTest.java +++ /dev/null @@ -1,124 +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.lightswitch; - -import org.apache.nlpcraft.NCModelClient; -import org.apache.nlpcraft.NCModelConfigAdapter; -import org.apache.nlpcraft.internal.nlp.entity.parser.custom.NCElement; -import org.apache.nlpcraft.internal.nlp.entity.parser.custom.NCElementBuilder; -import org.apache.nlpcraft.internal.nlp.entity.parser.custom.NCEntityParser; -import org.apache.nlpcraft.internal.nlp.entity.parser.custom.NCMacro; -import org.apache.nlpcraft.internal.nlp.token.parser.opennlp.NCOpenNlpTokenParser; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashSet; -import java.util.List; - -import static java.util.Arrays.asList; - -public class LightSwitchModelTest { - @Test - public void test() { - NCModelClient client = new NCModelClient(new LightSwitchModel(mkConfigManual())); - - client.start(); - - Assertions.assertNotNull(client.askSync("Hi!", null, null).getBody()); - - client.stop(); - } - - /** - * - * @return - */ - private static NCModelConfigAdapter mkConfigManual() { - List<NCMacro> macros = List.of( - new NCMacro("<ACTION>", "{turn|switch|dial|let|set|get|put}"), - new NCMacro("<KILL>", "{shut|kill|stop|eliminate}"), - new NCMacro("<ENTIRE_OPT>", "{entire|full|whole|total|_}"), - new NCMacro("<FLOOR_OPT>", "{upstairs|downstairs|{1st|first|2nd|second|3rd|third|4th|fourth|5th|fifth|top|ground} floor|_}"), - new NCMacro("<TYPE>", "{room|closet|attic|loft|{store|storage} {room|_}}"), - new NCMacro("<LIGHT>", "{all|_} {it|them|light|illumination|lamp|lamplight}") - ); - - List<NCElement> elements = - List.of( - new NCElementBuilder("ls:loc", "ls:loc"). - withSynonyms( - asList( - "<ENTIRE_OPT> <FLOOR_OPT> {kitchen|library|closet|garage|office|playroom|{dinning|laundry|play} <TYPE>}", - "<ENTIRE_OPT> <FLOOR_OPT> {master|kid|children|child|guest|_} {bedroom|bathroom|washroom|storage} {<TYPE>|_}", - "<ENTIRE_OPT> {house|home|building|{1st|first} floor|{2nd|second} floor}" - ) - ).make(), - new NCElementBuilder("ls:on", "ls:on"). - withGroups(new HashSet<>() {{ - add("act"); - }}). - withSynonyms( - asList( - "<ACTION> {on|up|_} <LIGHT> {on|up|_}", - "<LIGHT> {on|up}" - ) - ).make(), - new NCElementBuilder("ls:off", "ls:off"). - withGroups(new HashSet<>() {{ - add("act"); - }}). - withSynonyms( - asList( - "<ACTION> <LIGHT> {off|out|down}", - "{<ACTION>|<KILL>} {off|out|down} <LIGHT>", - "<KILL> <LIGHT>", - "<LIGHT> <KILL>", - "{out|no|off|down} <LIGHT>", - "<LIGHT> {out|off|down}" - ) - ).make() - ); - - NCModelConfigAdapter cfg = - new NCModelConfigAdapter( - "nlpcraft.lightswitch.ex", - "Light Switch Example Model", - "1.0", - new NCOpenNlpTokenParser( - "opennlp/en-token.bin", - "opennlp/en-pos-maxent.bin", - "opennlp/en-lemmatizer.dict" - ) - ); - - // Order is important. -// cfg.setTokenEnrichers( -// asList( -// new NCEnDictionaryTokenEnricher(), -// new NCEnQuotesTokenEnricher(), -// new NCEnSwearWordsTokenEnricher() -// ) -// ) - - cfg.setEntityParsers(asList(new NCEntityParser(macros, elements))); - -// // Don't need any entity enrichers. -// cfg.setEntityEnrichers(null) - return cfg; - } -}
