Rasa训练数据格式nlu.json与nlu.md互转

文章目录

  • JSON转Markdown
    • 执行命令
    • 运行代码
  • Markdown转JSON
    • 执行命令
    • 运行代码
  • 转中文
  • 参考文献





有执行命令和运行代码两种方法




JSON转Markdown

{
  "rasa_nlu_data": {
    "common_examples": [
      {
        "text": "hey",
        "intent": "greet",
        "entities": []
      },
      {
        "text": "hello",
        "intent": "greet",
        "entities": []
      },
      {
        "text": "i'm looking for a place in the north of town",
        "intent": "restaurant_search",
        "entities": [
          {
            "start": 31,
            "end": 36,
            "value": "north",
            "entity": "location"
          }
        ]
      },
      {
        "text": "yes",
        "intent": "affirm",
        "entities": []
      },
      {
        "text": "show me a mexican place in the centre",
        "intent": "restaurant_search",
        "entities": [
          {
            "start": 31,
            "end": 37,
            "value": "centre",
            "entity": "location"
          },
          {
            "start": 10,
            "end": 17,
            "value": "mexican",
            "entity": "cuisine"
          }
        ]
      },
      {
        "text": "bye",
        "intent": "goodbye",
        "entities": []
      },
      {
        "text": "goodbye",
        "intent": "goodbye",
        "entities": []
      },
      {
        "text": "i am looking for an indian spot",
        "intent": "restaurant_search",
        "entities": [
          {
            "start": 20,
            "end": 26,
            "value": "indian",
            "entity": "cuisine"
          }
        ]
      },
      {
        "text": "central indian restaurant",
        "intent": "restaurant_search",
        "entities": [
          {
            "start": 0,
            "end": 7,
            "value": "central",
            "entity": "location"
          },
          {
            "start": 8,
            "end": 14,
            "value": "indian",
            "entity": "cuisine"
          }
        ]
      }
    ]
  }
}

执行命令

rasa data convert nlu --data data/nlu.md --out data/nlu.json -f json

运行代码

from rasa.nlu.training_data import load_data

input_file = 'nlu.json'
output_file = 'nlu.md'

with open(output_file, 'w') as f:
    f.write(load_data(input_file).as_markdown())




Markdown转JSON

## intent:affirm
- yes

## intent:goodbye
- bye
- goodbye

## intent:greet
- hey
- hello

## intent:restaurant_search
- i'm looking for a place in the [north](location) of town
- show me a [mexican](cuisine) place in the [centre](location)
- i am looking for an [indian](cuisine) spot
- [central](location) [indian](cuisine) restaurant

执行命令

rasa data convert nlu --data data/nlu.json --out data/nlu.md -f md

运行代码

from rasa.nlu.training_data import load_data

input_file = 'nlu.md'
output_file = 'nlu.json'

with open(output_file, 'w') as f:
    f.write(load_data(input_file).as_json())




转中文

encoding='utf-8'

language='zh'

from rasa.nlu.training_data import load_data

input_file = 'nlu.json'
output_file = 'nlu.md'

with open(output_file, 'w', encoding='utf-8') as f:
    f.write(load_data(input_file, language='zh').as_markdown())




参考文献

  1. Training Data Format
  2. Convert a Rasa NLU training file from JSON to Markdown format
  3. mmand Line Interface

你可能感兴趣的:(Python)