import pandas as pd import yaml # Read the data from the CSV file df = pd.read_csv(r"D:\livecode\Book1.csv") # df['Question'] = df['Question'].replace('\n', ' ', regex=True) # df['Question'] = df['Question'].str.strip() # df['Answers'] = df['Answers'].replace('\n', ' ', regex=True) # df['Answers'] = df['Answers'].str.strip() # # Extract the columns from the dataframe Question = df['Question'].tolist() Example1 = df['Example1'].tolist() Example2 = df['Example2'].tolist() Example3 = df['Example3'].tolist() uniqueid = df['uniqueid'].tolist() Answers = df['Answers'].tolist() question_cols = df.filter(regex='Example').columns answer_cols = df.filter(regex='Answers').columns #appending intents import ruamel.yaml as yaml # Load the domain YAML file with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as f: domain = yaml.safe_load(f) for i in uniqueid: domain['intents'].append(i) with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'w') as f: yaml.dump(domain, f, default_flow_style=False, allow_unicode=True) #appending muitipal answers from ruamel.yaml import YAML # Create a YAML object that preserves the formatting of the original YAML file yaml = YAML() yaml.preserve_quotes = True yaml.indent(mapping=2, sequence=4, offset=2) # Read in the existing domain file with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as file: domain = yaml.load(file) # Generate the new YAML code for each row of data new_content = {} for index, row in df.iterrows(): intent_name = row['uniqueid'] examples = [row[col] for col in answer_cols ] separator = "_" for counter, example in enumerate(examples, start=1): #example=example.replace(':','') response_name = f"utter_{intent_name}{separator}{counter}" response_text = f"- text: {example}" if 'responses' not in new_content: new_content['responses'] = {} new_content['responses'][response_name] = yaml.load(response_text) # Update the `responses` section of the domain dictionary with the new content domain['responses'].update(new_content['responses']) # Write the updated domain file back to disk, preserving the formatting of the original file with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'w') as file: yaml.dump(domain, file) #appending multipal action for each rule with open(r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml", 'a') as f: for index, row in df.iterrows(): intent_name = row['uniqueid'] examples = [row[col] for col in question_cols] separator = "_" steps = {"intent": intent_name} gg=[] for counter, example in enumerate(examples, start=1): unique_id = f"{intent_name}{separator}{counter}" gg.append({"action": f"utter_{unique_id}"}) output_str = " \n ".join([f"- action: {question['action'][:-1]}{i}" for i, question in enumerate(gg, start=1)]) f.write(f""" - rule: Ticket{intent_name} steps: - intent: {steps['intent']} {output_str} """) # appending multipal question with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml', "a") as file: for index, row in df.iterrows(): intent_name = row['uniqueid'] examples = [row[col] for col in question_cols] file.write(f""" - intent: {intent_name} examples: | """) for example in examples: file.write(f" - \"{example}\"\n") file.write("\n") # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml" # string_to_remove = ' - "nan"' # with open(filename, "r") as f: # text = f.read() # modified_text = text.replace(string_to_remove, "") # with open(filename, "w") as f: # f.write(modified_text) # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\domain.yml" # string_to_remove = ' - "nan"' # with open(filename, "r") as f: # text = f.read() # modified_text = text.replace(string_to_remove, "") # with open(filename, "w") as f: # f.write(modified_text) # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml" # string_to_remove = ' - "nan"' # with open(filename, "r") as f: # text = f.read() # modified_text = text.replace(string_to_remove, "") # with open(filename, "w") as f: # f.write(modified_text)