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

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

  • 源代码地址
  • 文档
  • Quickstart
  • 示例
  • 简要教程
  • 参考

源代码地址

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

你可能感兴趣的:(人工智能,语义解析,Snips,NLU)