import pandas as pd import yaml import re # Read the data from the CSV file df = pd.read_csv(r"D:\livecode\chatbot\AI Support Questionaire - Sheet1 (2).csv", encoding='latin') def remove_special_characters(df): for column in df.columns: df[column] = df[column].apply(lambda x: re.sub(r'[^a-zA-Z0-9->]', ' ', str(x))) return df df_cleaned = remove_special_characters(df) uniqueid = df['UniqueId'].tolist() # Answers = df['Answers'].tolist() question_cols = df.filter(regex='Question').columns answer_cols = df.filter(regex='Answers').columns #appending intents import ruamel.yaml as yaml # Load the domain YAML file with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'r') as f: domain = yaml.safe_load(f) for i in uniqueid: domain['intents'].append(i) with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'w') as f: yaml.dump(domain, f, default_flow_style=False, allow_unicode=True) question_cols = df.filter(regex='Question').columns answer_cols = df.filter(regex='Answer').columns # for i in question_cols: # df[i] = df[i].replace('\n', '$$', regex=True) # # df[i] = df[i].str.strip() # for i in answer_cols: # df[i] = df[i].replace('\n', '$$', regex=True) # # df[i] = df[i].str.strip() 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'D:\\livecode\\chatbot\\traildata\\domain.yml', 'r', encoding='utf-8') 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'D:\\livecode\\chatbot\\traildata\\domain.yml', 'w',encoding='utf-8') as file: yaml.dump(domain, file) #appending multipal action for each rule with open(r"D:\\livecode\\chatbot\\traildata\\rules.yml", 'a') as f: for index, row in df.iterrows(): intent_name = row['UniqueId'] examples = [row[col] for col in answer_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} - action: action_service """) with open(r'D:\\livecode\\chatbot\\traildata\\nlu.yml', "a",encoding='utf-8') 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)