Update 'chatbot/multipal_col.py'

This commit is contained in:
2023-08-30 04:45:08 +00:00
parent c1d0c08140
commit 91328b4b38
+154 -135
View File
@@ -1,136 +1,155 @@
import pandas as pd import pandas as pd
import yaml import yaml
import re
# Read the data from the CSV file
df = pd.read_csv(r"D:\livecode\Book1.csv") # Read the data from the CSV file
df = pd.read_csv(r"D:\livecode\chatbot\AI Support Questionaire - Sheet1 (2).csv", encoding='latin')
# df['Question'] = df['Question'].replace('\n', ' ', regex=True)
# df['Question'] = df['Question'].str.strip()
def remove_special_characters(df):
# df['Answers'] = df['Answers'].replace('\n', ' ', regex=True) for column in df.columns:
# df['Answers'] = df['Answers'].str.strip() df[column] = df[column].apply(lambda x: re.sub(r'[^a-zA-Z0-9->]', ' ', str(x)))
return df
# # Extract the columns from the dataframe
Question = df['Question'].tolist() df_cleaned = remove_special_characters(df)
Example1 = df['Example1'].tolist() uniqueid = df['UniqueId'].tolist()
Example2 = df['Example2'].tolist() # Answers = df['Answers'].tolist()
Example3 = df['Example3'].tolist()
uniqueid = df['uniqueid'].tolist() question_cols = df.filter(regex='Question').columns
Answers = df['Answers'].tolist() answer_cols = df.filter(regex='Answers').columns
question_cols = df.filter(regex='Example').columns
answer_cols = df.filter(regex='Answers').columns #appending intents
#appending intents import ruamel.yaml as yaml
import ruamel.yaml as yaml
# Load the domain YAML file
# Load the domain YAML file with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'r') as f:
with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as f: domain = yaml.safe_load(f)
domain = yaml.safe_load(f) for i in uniqueid:
for i in uniqueid: domain['intents'].append(i)
domain['intents'].append(i) with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'w') as f:
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)
yaml.dump(domain, f, default_flow_style=False, allow_unicode=True)
#appending muitipal answers
from ruamel.yaml import YAML question_cols = df.filter(regex='Question').columns
answer_cols = df.filter(regex='Answer').columns
# Create a YAML object that preserves the formatting of the original YAML file
yaml = YAML() # for i in question_cols:
yaml.preserve_quotes = True # df[i] = df[i].replace('\n', '$$', regex=True)
yaml.indent(mapping=2, sequence=4, offset=2) # # df[i] = df[i].str.strip()
# Read in the existing domain file
with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as file: # for i in answer_cols:
domain = yaml.load(file) # df[i] = df[i].replace('\n', '$$', regex=True)
# # df[i] = df[i].str.strip()
# Generate the new YAML code for each row of data
new_content = {}
for index, row in df.iterrows():
intent_name = row['uniqueid'] from ruamel.yaml import YAML
examples = [row[col] for col in answer_cols ]
separator = "_" # Create a YAML object that preserves the formatting of the original YAML file
for counter, example in enumerate(examples, start=1): yaml = YAML()
#example=example.replace(':','') yaml.preserve_quotes = True
response_name = f"utter_{intent_name}{separator}{counter}" yaml.indent(mapping=2, sequence=4, offset=2)
response_text = f"- text: {example}"
if 'responses' not in new_content: # Read in the existing domain file
new_content['responses'] = {} with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'r', encoding='utf-8') as file:
new_content['responses'][response_name] = yaml.load(response_text) domain = yaml.load(file)
# Update the `responses` section of the domain dictionary with the new content # Generate the new YAML code for each row of data
domain['responses'].update(new_content['responses']) new_content = {}
for index, row in df.iterrows():
# Write the updated domain file back to disk, preserving the formatting of the original file intent_name = row['UniqueId']
with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'w') as file: examples = [row[col] for col in answer_cols ]
yaml.dump(domain, file) separator = "_"
for counter, example in enumerate(examples, start=1):
#appending multipal action for each rule #example=example.replace(':','')
with open(r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml", 'a') as f: response_name = f"utter_{intent_name}{separator}{counter}"
for index, row in df.iterrows(): response_text =f"- text: {example}"
intent_name = row['uniqueid'] if 'responses' not in new_content:
examples = [row[col] for col in question_cols] new_content['responses'] = {}
separator = "_" new_content['responses'][response_name] = yaml.load(response_text)
steps = {"intent": intent_name}
gg=[] # Update the `responses` section of the domain dictionary with the new content
for counter, example in enumerate(examples, start=1): domain['responses'].update(new_content['responses'])
unique_id = f"{intent_name}{separator}{counter}"
gg.append({"action": f"utter_{unique_id}"}) # 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:
output_str = " \n ".join([f"- action: {question['action'][:-1]}{i}" for i, question in enumerate(gg, start=1)]) yaml.dump(domain, file)
f.write(f"""
- rule: Ticket{intent_name}
steps: #appending multipal action for each rule
- intent: {steps['intent']} with open(r"D:\\livecode\\chatbot\\traildata\\rules.yml", 'a') as f:
{output_str} for index, row in df.iterrows():
intent_name = row['UniqueId']
""") examples = [row[col] for col in answer_cols]
separator = "_"
steps = {"intent": intent_name}
# appending multipal question gg=[]
for counter, example in enumerate(examples, start=1):
with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml', "a") as file: unique_id = f"{intent_name}{separator}{counter}"
for index, row in df.iterrows(): gg.append({"action": f"utter_{unique_id}"})
intent_name = row['uniqueid']
examples = [row[col] for col in question_cols] output_str = " \n ".join([f"- action: {question['action'][:-1]}{i}" for i, question in enumerate(gg, start=1)])
file.write(f"""
- intent: {intent_name} f.write(f"""
examples: | - rule: Ticket{intent_name}
""") steps:
for example in examples: - intent: {steps['intent']}
file.write(f" - \"{example}\"\n") {output_str}
file.write("\n") - action: action_service
""")
# 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()
with open(r'D:\\livecode\\chatbot\\traildata\\nlu.yml', "a",encoding='utf-8') as file:
# modified_text = text.replace(string_to_remove, "") for index, row in df.iterrows():
intent_name = row['UniqueId']
# with open(filename, "w") as f: examples = [row[col] for col in question_cols]
# f.write(modified_text) file.write(f"""
- intent: {intent_name}
# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\domain.yml" examples: |
# string_to_remove = ' - "nan"' """)
for example in examples:
# with open(filename, "r") as f: file.write(f" - \"{example}\"\n")
# text = f.read() file.write("\n")
# modified_text = text.replace(string_to_remove, "")
# with open(filename, "w") as f: # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml"
# f.write(modified_text) # string_to_remove = ' - "nan"'
# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml" # with open(filename, "r") as f:
# string_to_remove = ' - "nan"' # text = f.read()
# with open(filename, "r") as f: # modified_text = text.replace(string_to_remove, "")
# text = f.read()
# with open(filename, "w") as f:
# modified_text = text.replace(string_to_remove, "") # f.write(modified_text)
# with open(filename, "w") as f: # 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) # f.write(modified_text)