Write NLU Data Programmatically

I was trying to write the YML files of rasa using the following python code. But it doesn’t load successfully when I try to start training it.

yaml.dump(data_, file, default_flow_style=False, sort_keys=False)

How can I write the data using Rasa’s format that loads correctly? I am assuming that my training data and config file will change dynamically.

Thanks a lot

1 Like

I will recommend writing a custom training data importer instead of a converter to yaml

1 Like

@Mushahid2521 Heya!

If I get you right; do you want to convert or write .yml for your training data as per Rasa Format? Where do you had data, I mean in excel, txt etc

Yes. Let’s say I have an initial rasa project. I want to open the data files like nlu.yml, stories.yml, rules.yml, domain.yml and append new intents, responses etc and save it back again.

@Mushahid2521 You want to open, write or create?

Hello Team. Can I get more info on this ? Assume I have csv file with intents and corresponding training examples, how do I programatically convert it to the way rasa expects and successfully train the model.

Thanks

@sML Is this still a issue or you solved this?

Hi @nik202. yes, managed to solve it. Did not find a direct option to convert from csv/json to yaml in the way rasa wants; had to convert to md first, and then used rasa convert. Any other better way than this?

Thanks.

@sML no worries, I will give you one method, to directly convert to yml training format ok?

Demo code:

 class GetAnswer(Action):
     def __init__(self):
         self.faq_data = pd.read_csv('./data/faq_d.csv')
         qs = list(self.faq_d['question'])
         with open("./data/nlu/faq.yml", "wt", encoding="utf-8") as f:
             f.write('version: "2.0"\n')
             f.write("nlu: \n- intent: question\n  examples: | \n")
             for q in qs:
                 f.write(f"    - {q}\n")

I hope this will help you :wink:

1 Like

@nik202 Thanks a lot!