This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-491 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-491 by this push: new da4e436e WIP. da4e436e is described below commit da4e436e1a6c5620218c8badaa287677aa76d8de Author: Sergey Kamov <skhdlem...@gmail.com> AuthorDate: Mon Apr 4 22:43:10 2022 +0300 WIP. --- nlpcraft-examples/order/pom.xml | 5 +++++ .../nlpcraft/examples/order/OrderModel.scala | 25 +++++++++++++++++++++- .../nlpcraft/examples/order/OrderModelSpec.scala | 2 +- .../impl/NCStanfordNLPEntityParserImpl.scala | 4 +++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/nlpcraft-examples/order/pom.xml b/nlpcraft-examples/order/pom.xml index 41a42663..df729acd 100644 --- a/nlpcraft-examples/order/pom.xml +++ b/nlpcraft-examples/order/pom.xml @@ -37,6 +37,11 @@ <artifactId>nlpcraft</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>nlpcraft-stanford</artifactId> + <version>${project.version}</version> + </dependency> <!-- Test dependencies. --> <dependency> diff --git a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala index fccb9644..08a6b1c3 100644 --- a/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala +++ b/nlpcraft-examples/order/src/main/java/org/apache/nlpcraft/examples/order/OrderModel.scala @@ -18,6 +18,8 @@ package org.apache.nlpcraft.examples.order import com.typesafe.scalalogging.LazyLogging +import edu.stanford.nlp.pipeline.StanfordCoreNLP +import opennlp.tools.stemmer.PorterStemmer import org.antlr.v4.runtime.misc.Predicate import org.apache.nlpcraft.* import org.apache.nlpcraft.internal.util.NCResourceReader @@ -26,10 +28,20 @@ import org.apache.nlpcraft.nlp.entity.parser.* import scala.collection.mutable import org.apache.nlpcraft.NCResultType.* +import org.apache.nlpcraft.nlp.entity.parser.semantic.{NCSemanticEntityParser, NCSemanticStemmer} +import org.apache.nlpcraft.nlp.entity.parser.stanford.NCStanfordNLPEntityParser +import org.apache.nlpcraft.nlp.token.parser.stanford.NCStanfordNLPTokenParser +import java.util.Properties import scala.jdk.CollectionConverters.* object OrderModel extends LazyLogging: + private val STANFORD = + val props = new Properties() + props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner") + new StanfordCoreNLP(props) + private val TOK_PARSER = new NCStanfordNLPTokenParser(STANFORD) + private def extractPizzaKind(e: NCEntity): String = e.get[String]("ord:pizza:kind:value") private def extractPizzaSize(e: NCEntity): PizzaSize = PizzaSize.valueOf(e.get[String]("ord:pizza:size:value").toUpperCase) private def extractDrink(e: NCEntity): String = e.get[String]("ord:drink:value") @@ -78,7 +90,18 @@ import org.apache.nlpcraft.examples.order.OrderModel.* */ class OrderModel extends NCModelAdapter ( new NCModelConfig("nlpcraft.order.ex", "Order Example Model", "1.0"), - new NCPipelineBuilder().withSemantic("en", "order_model.yaml").build() + new NCPipelineBuilder(). + withTokenParser(TOK_PARSER). + withEntityParser(new NCStanfordNLPEntityParser(STANFORD, "number")). + withEntityParser(new NCSemanticEntityParser( + new NCSemanticStemmer(): + final private val ps = new PorterStemmer + override def stem(txt: String): String = ps.synchronized { ps.stem(txt) } + , + TOK_PARSER, + "order_model.yaml" + )). + build() ) with LazyLogging: private val ords = mutable.HashMap.empty[String, OrderState] diff --git a/nlpcraft-examples/order/src/test/java/org/apache/nlpcraft/examples/order/OrderModelSpec.scala b/nlpcraft-examples/order/src/test/java/org/apache/nlpcraft/examples/order/OrderModelSpec.scala index 7f437428..3ae7ed2e 100644 --- a/nlpcraft-examples/order/src/test/java/org/apache/nlpcraft/examples/order/OrderModelSpec.scala +++ b/nlpcraft-examples/order/src/test/java/org/apache/nlpcraft/examples/order/OrderModelSpec.scala @@ -43,7 +43,7 @@ class OrderModelSpec: if expResType != resp.getType then printDialog() - require(false, s"Unexpected type: ${resp.getType}, expected: ${expResType}.") + require(false, s"Unexpected type: ${resp.getType}, expected: $expResType.") ask("I want to order margherita medium size, marbonara, marinara and tea", ASK_DIALOG) ask("large size please", ASK_DIALOG) diff --git a/nlpcraft-stanford/src/main/java/org/apache/nlpcraft/nlp/entity/parser/stanford/impl/NCStanfordNLPEntityParserImpl.scala b/nlpcraft-stanford/src/main/java/org/apache/nlpcraft/nlp/entity/parser/stanford/impl/NCStanfordNLPEntityParserImpl.scala index d308a74c..395fa2e0 100644 --- a/nlpcraft-stanford/src/main/java/org/apache/nlpcraft/nlp/entity/parser/stanford/impl/NCStanfordNLPEntityParserImpl.scala +++ b/nlpcraft-stanford/src/main/java/org/apache/nlpcraft/nlp/entity/parser/stanford/impl/NCStanfordNLPEntityParserImpl.scala @@ -36,6 +36,8 @@ class NCStanfordNLPEntityParserImpl(stanford: StanfordCoreNLP, supported: JSet[S require(stanford != null) require(supported != null) + private val supportedLc = supported.asScala.map(_.toLowerCase) + override def parse(req: NCRequest, cfg: NCModelConfig, toksList: JList[NCToken]): JList[NCEntity] = val toks = toksList.asScala.toSeq val doc = new CoreDocument(req.getText) @@ -46,7 +48,7 @@ class NCStanfordNLPEntityParserImpl(stanford: StanfordCoreNLP, supported: JSet[S for (e <- doc.entityMentions().asScala) val typ = e.entityType().toLowerCase - if supported.contains(typ) then + if supportedLc.contains(typ) then val offsets = e.charOffsets() val t1 = toks.find(_.getStartCharIndex == offsets.first) lazy val t2 = toks.find(_.getEndCharIndex == offsets.second)