Rasa2.x训练数据格式互转

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"
          }
        ]
      }
    ],
        "regex_features": [{
     
        "name": "greet",
        "pattern": "hey[^\\\\s]*"
      },
      {
     
        "name": "zipcode",
        "pattern": "[0-9]{5}"
      }],
    	"lookup_tables": [
	    	{
     "name": "city",
	        "elements": [
	          "shanghai",
	          "beijing",
	          "tianjing",
	          "jiaxing",
	          "hangzhou"]}
    	],
    	"entity_synonyms": [
	    	{
     "value": "New York City",
	        "synonyms": [
	          "NYC",
	          "nyc",
	          "the big apple"
	        ]}
    	]
  }
}

命令行

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())

markdown转YAML

## 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 -f yaml --data=./data/nlu --out=./data/nlu

rasa data convert core -f yaml --data=./data/stories --out=./data/stories

rasa data convert nlu -f yaml --data={
     SOURCE_DIR} --out={
     TARGET_DIR}
rasa data convert nlg -f yaml --data={
     SOURCE_DIR} --out={
     TARGET_DIR}
rasa data convert core -f yaml --data={
     SOURCE_DIR} --out={
     TARGET_DIR}

其他转换

  • 只有md 和json可以输出, input_file可以yaml
from rasa.nlu.convert import convert_training_data

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

# convert_training_data(data_file=input_file, out_file=output_file, output_format="md", language="en")
convert_training_data(data_file=input_file, out_file=output_file, output_format="json", language="zh")

欢迎关注公众号:

Rasa2.x训练数据格式互转_第1张图片

你可能感兴趣的:(RASA,自然语言处理)