使用 NLPCraft 和 Groovy™ 将自然语言转换为操作
发布时间:2023-03-10 07:22PM (最后更新时间:2023-03-13 01:32PM)
本博客探讨了如何将 Apache NLPCraft 与 Groovy 结合使用。
Apache NLPCraft (孵化中) 是一个将自然语言转化为行动的库。它围绕一个用于定义自然语言意图的先进意图定义语言 (IDL) 和一个完全确定性的意图匹配算法进行设计。
groovy-data-science GitHub 仓库中有一个使用 Apache NLPCraft 之前的 0.9.0 版本的示例。早期版本支持 Java、Scala2、Kotlin 和 Groovy。LanguageProcessingNLPCraft 项目中的示例展示了如何与这四种语言中的模型进行交互。
该项目最近宣布发布 1.0.0 版本,该版本代表了超过 18 个月的深度重构。新版本提供了许多增强功能,并支持 Scala3 用于其模型。如果您将广泛使用 NLPCraft,那么 Scala3 可能是您作为编程语言的最佳选择。话虽如此,由于我们是在 JVM 上,某些集成步骤并不太难。我们将展示如何使用 Groovy 作为客户端语言。
控制室内电灯开关
首先,关于示例的一些背景。我们试图确定英语命令背后打开和关闭室内灯光的意图。我们将使用该项目预定义的模型,该模型是其示例的一部分。它使用 YAML 和 Scala3 代码的组合进行定义。需要注意的关键是,该模型由许多关键字组成,包括动作和位置。如果这些匹配,则会触发电灯开关(
ls
)意图。在我们的示例中,动作和位置很快就会变得更加明显。
我们的示例使用英语模型,但 NLPCraft 可以支持任何自然语言。他们还提供法语和其他语言的电灯开关模型。
我们还需要添加 NLPCraft 依赖项:org.apache.nlpcraft:nlpcraft:1.0.0
和 org.apache.nlpcraft:nlpcraft-example-lightswitch:1.0.0
(用于预编译模型)。您可以在脚本中使用 @Grab
语句或将依赖项添加到构建文件中。
注意
|
根据您使用的 Groovy 版本和模块,如果您看到有关 Jackson databind 版本的异常,只需排除 Groovy 引用的 Jackson 版本即可。它们比 Scala 预期的要新。 |
最后,这是 Groovy 代码
import org.apache.nlpcraft.NCModelClient
import org.apache.nlpcraft.examples.lightswitch.LightSwitchModel
import static scala.collection.immutable.HashMap$.MODULE$ as ScalaMap
var data = ScalaMap.empty() // no optional data
var user = 'someUserId'
var expectedIntent = 'ls' // from model
var phrases = [
'Turn on the lights in the master bedroom',
'Please, no lights!',
'Turn up the illumination in garage and 1st floor'
]
new NCModelClient(new LightSwitchModel()).withCloseable { client ->
phrases.each { phrase ->
var result = client.ask(phrase, user, data)
println result.body
assert result.intentId.get() == expectedIntent
}
}
在这里,我们只是打印模型类中回调返回的结果。通常,此时我们会集成 HomeKit、Arduino 或其他某种集成。我们还会检查是否触发了 ls
意图。
当我们运行此脚本时,我们的断言通过,输出如下
Lights are [on] in [master bedroom]. Lights are [off] in [entire house]. Lights are [on] in [1st floor, garage].
询问时间
现在让我们看看 NLPCraft
time
示例。这是一个非常简单的世界时间机器人实现。
该模型再次定义为 YAML 和 Scala3 代码的组合。预编译版本可从 Maven central 获取,为 org.apache.nlpcraft:nlpcraft-example-time:1.0.0
。我们使用了一个启用了 DefaultScalaModule
用于 YAML 处理的本地构建版本。如果还没有,我们还需要 Groovy 端的 groovy-yaml
模块。
该模型定义了两个意图
-
如果找到城市位置的匹配,则应触发
intent2
。 -
如果您看起来是在没有城市的情况下询问时间,则会触发
intent1
;这对应于查找本地时间。
以下是 Groovy 客户端代码
import groovy.yaml.YamlSlurper
import org.apache.nlpcraft.NCModelClient
import org.apache.nlpcraft.examples.time.TimeModel
import static scala.collection.immutable.HashMap$.MODULE$ as ScalaMap
var data = ScalaMap.empty() // no optional data
var user = 'someUserId'
var phrases = [
"What time is it now in New York City?" : 'intent2',
"What's the current time in Singapore?" : 'intent2',
"Show me time of the day in London." : 'intent2',
"Can you please give me Tokyo's current date and time." : 'intent2',
"What's the local time?" : 'intent1'
]
new NCModelClient(new TimeModel()).withCloseable { client ->
phrases.each { phrase, expected ->
var result = client.ask(phrase, user, data)
assert result.intentId.get() == expected
var body = new YamlSlurper().parseText(result.body)
body.with{ println "$city, $country: $localTime" }
}
}
同样,我们所有的断言都通过了,输出如下
New york city, United states: 12 Mar. 2023, 10:09:41 pm Singapore, Singapore: 13 Mar. 2023, 10:09:41 am London, United kingdom: 13 Mar. 2023, 2:09:41 am Tokyo, Japan: 13 Mar. 2023, 11:09:41 am Brisbane, Australia: 13 Mar. 2023, 12:09:42 pm