Ruamel yaml help

I’m trying to load nlu data to create data dynamically

from ruamel import yaml

data = yaml.safe_load(open('nlu.yml'))

print(data)

but the output is

{'version': '2.0', 'nlu': [{'intent': 'greet', 'examples': "- hey\n- hello\n- hi\n- hello there\n- good morning\n- good evening\n- moin\n- hey there\n- let's go\n- hey dude\n- goodmorning\n- goodevening\n- good afternoon\n"}, {'intent': 'goodbye', 'examples': '- good afternoon\n- cu\n- good by\n- cee you later\n- good night\n- bye\n- goodbye\n- have a nice day\n- see you around\n- bye bye\n- see you later\n'}, {'intent': 'affirm', 'examples': '- yes\n- y\n- indeed\n- of course\n- that sounds good\n- correct\n'}, {'intent': 'deny', 'examples': "- no\n- n\n- never\n- I don't think so\n- don't like that\n- no way\n- not really\n"}, {'intent': 'mood_great', 'examples': '- perfect\n- great\n- amazing\n- feeling like a king\n- wonderful\n- I am feeling very good\n- I am great\n- I am amazing\n- I am going to save the world\n- super stoked\n- extremely good\n- so so perfect\n- so good\n- so perfect\n'}, {'intent': 'mood_unhappy', 'examples': "- my day was horrible\n- I am sad\n- I don't feel very well\n- I am disappointed\n- super sad\n- I'm so sad\n- sad\n- very sad\n- unhappy\n- not good\n- not very good\n- extremly sad\n- so saad\n- so sad\n"}, {'intent': 'bot_challenge', 'examples': '- are you a bot?\n- are you a human?\n- am I talking to a bot?\n- am I talking to a human?\n'}]}

expected out needs to list of strings for the examples key but because of | in the yml files it is throwing this way did anyone handle this successfully using ruamel yaml

Can you type the expected output, please? From what I understood, you want something like this:

data = yaml.safe_load(open('nlu.yml'))
data = data['nlu']

new_data = {}

for i in data:
    intent = i['intent']
    #// example[2:] because every example starts with '- ' so we need to remove that
    #// [:-1] in the end because the last element is an empty string so we need to remove that
    examples = [example[2:] for example in i['examples'].split('\n')][:-1]
    new_data[intent] = examples

print(new_data)

output:

{
   "greet":[
      "hey",
      "hello",
      "hi",
      "hello there",
      "good morning",
      "good evening",
      "moin",
      "hey there",
      "let's go",
      "hey dude",
      "goodmorning",
      "goodevening",
      "good afternoon"
   ],
   "goodbye":[
      "good afternoon",
      "cu",
      "good by",
      "cee you later",
      "good night",
      "bye",
      "goodbye",
      "have a nice day",
      "see you around",
      "bye bye",
      "see you later"
   ],
   "affirm":[
      "yes",
      "y",
      "indeed",
      "of course",
      "that sounds good",
      "correct"
   ],
   "deny":[
      "no",
      "n",
      "never",
      "I don't think so",
      "don't like that",
      "no way",
      "not really"
   ],
   "mood_great":[
      "perfect",
      "great",
      "amazing",
      "feeling like a king",
      "wonderful",
      "I am feeling very good",
      "I am great",
      "I am amazing",
      "I am going to save the world",
      "super stoked",
      "extremely good",
      "so so perfect",
      "so good",
      "so perfect"
   ],
   "mood_unhappy":[
      "my day was horrible",
      "I am sad",
      "I don't feel very well",
      "I am disappointed",
      "super sad",
      "I'm so sad",
      "sad",
      "very sad",
      "unhappy",
      "not good",
      "not very good",
      "extremly sad",
      "so saad",
      "so sad"
   ],
   "bot_challenge":[
      "are you a bot?",
      "are you a human?",
      "am I talking to a bot?",
      "am I talking to a human?"
   ]
}
1 Like

Hi @ChrisRahme thanks for writing the script

I wrote a similar one as a temporary solution.

I was looking for something that directly reads pipe multi-sting YAML without these additional scripts.

So that for writing again it would be easier if I add few more elements(which is the desired use case) for formation again I need to write a similar script. So I was thinking if there’s something I missed.

Ah sorry, I don’t know much about Ruamel :slight_smile:

Np :slight_smile:

I have seen the source code of rasa and I’m using these

from rasa.shared.nlu.training_data.formats.rasa_yaml import RasaYAMLReader, RasaYAMLWriter

from rasa.shared.nlu.training_data.message import Message

from rasa.shared.nlu.training_data.training_data import TrainingData

import pickle

import uuid

import yaml

c1 = RasaYAMLReader()

w1 = RasaYAMLWriter()

c2 = RasaYAMLReader()

w2 = RasaYAMLWriter()

I feel like there is something we can do about that with Ruamel so ask here if some one had used that.

1 Like