Rasa訓練數據格式nlu.json與nlu.md互轉





有執行命令和運行代碼兩種方法




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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章