Script to combine RASA domain file

(rasa_env) C:\chat_support>python scripts\combine_domain.py

python scripts\combine_domain.py
the script code itself
import yaml
import os
from collections import defaultdict

def merge_dicts(dict_list):
    """Merge dictionaries by extending lists under the same key."""
    merged = defaultdict(list)
    for dictionary in dict_list:
        for key, value in dictionary.items():
            if isinstance(value, list):
                merged[key].extend(value)
            else:
                merged[key] = value
    return dict(merged)

# Specify the directory containing your domain parts
domain_dir = 'domain/'
# Automatically find all .yml files in the domain directory
domain_files = [f for f in os.listdir(domain_dir) if f.endswith('.yml')]

combined_domain = defaultdict(list)

for file_name in domain_files:
    file_path = os.path.join(domain_dir, file_name)
    if os.path.exists(file_path):
        with open(file_path, 'r') as file:
            part_data = yaml.safe_load(file)
            # Merge the current file's data into the combined domain
            combined_domain = merge_dicts([combined_domain, part_data])

# Add the version at the top of the combined domain
combined_domain_final = {'version': '3.1'}
combined_domain_final.update(combined_domain)

# Save the combined domain to a file
with open('domain.yml', 'w') as file:
    yaml.dump(combined_domain_final, file, allow_unicode=True)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *