【AI实战】一个语义解析的开源 python 库 Snips NLU

SnipsNLU是一个用于自然语言理解的Python开源库,它提供了意图识别和实体提取的功能。用户可以通过简单的pip安装,然后使用提供的样例数据集训练模型,实现对输入文本的理解,如查询天气等场景。训练数据以yaml格式定义,包括意图、槽位和示例语句。
摘要由CSDN通过智能技术生成

一个语义解析的开源 python 库 Snips NLU

源代码地址

https://github.com/snipsco/snips-nlu

文档

https://snips-nlu.readthedocs.io/en/latest/quickstart.html

Quickstart

pip install snips-nlu
python -m snips_nlu download en

示例

import json
from snips_nlu import SnipsNLUEngine

nlu_engine = SnipsNLUEngine()
nlu_engine.fit(sample_dataset)

parsing = nlu_engine.parse("What will be the weather in San Francisco next week?")
print(json.dumps(parsing, indent=2))
  • 返回结果
{
  "input": "What will be the weather in San Francisco next week?",
  "intent": {
    "intentName": "sampleGetWeather",
    "probability": 0.641227710154331
  },
  "slots": [
    {
      "range": {
        "start": 28,
        "end": 41
      },
      "rawValue": "San Francisco",
      "value": {
        "kind": "Custom",
        "value": "San Francisco"
      },
      "entity": "location",
      "slotName": "weatherLocation"
    },
    {
      "range": {
        "start": 42,
        "end": 51
      },
      "rawValue": "next week",
      "value": {
        "type": "value",
        "grain": "week",
        "precision": "exact",
        "latent": false,
        "value": "2018-02-12 00:00:00 +01:00"
      },
      "entity": "snips/datetime",
      "slotName": "weatherDate"
    }
  ]
}

简要教程

  • Training Data
    训练数据格式如下:
    dataset.yaml:
# turnLightOn intent
---
type: intent
name: turnLightOn
slots:
  - name: room
    entity: room
utterances:
  - Turn on the lights in the [room](kitchen)
  - give me some light in the [room](bathroom) please
  - Can you light up the [room](living room) ?
  - switch the [room](bedroom)'s lights on please

# turnLightOff intent
---
type: intent
name: turnLightOff
slots:
  - name: room
    entity: room
utterances:
  - Turn off the lights in the [room](entrance)
  - turn the [room](bathroom)'s light out please
  - switch off the light the [room](kitchen), will you?
  - Switch the [room](bedroom)'s lights off please

# setTemperature intent
---
type: intent
name: setTemperature
slots:
  - name: room
    entity: room
  - name: roomTemperature
    entity: snips/temperature
utterances:
  - Set the temperature to [roomTemperature](19 degrees) in the [room](bedroom)
  - please set the [room](living room)'s temperature to [roomTemperature](twenty two degrees celsius)
  - I want [roomTemperature](75 degrees fahrenheit) in the [room](bathroom) please
  - Can you increase the temperature to [roomTemperature](22 degrees) ?

# room entity
---
type: entity
name: room
automatically_extensible: no
values:
- bedroom
- [living room, main room, lounge]
- [garden, yard, backyard]
  • The Snips NLU Engine
from snips_nlu import SnipsNLUEngine

default_engine = SnipsNLUEngine()
import io
import json

from snips_nlu import SnipsNLUEngine
from snips_nlu.default_configs import CONFIG_EN

engine = SnipsNLUEngine(config=CONFIG_EN)
  • Training the engine
with io.open("dataset.json") as f:
    dataset = json.load(f)

engine.fit(dataset)

参考

1.https://snips-nlu.readthedocs.io/en/latest/quickstart.html
2.https://github.com/snipsco/snips-nlu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值