Update 'chatbot/multipal_col.py'

This commit is contained in:
2023-08-30 04:45:08 +00:00
parent c1d0c08140
commit 91328b4b38
+46 -27
Vedi File
@@ -1,37 +1,51 @@
import pandas as pd
import yaml
import re
# Read the data from the CSV file
df = pd.read_csv(r"D:\livecode\Book1.csv")
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()
# df['Answers'] = df['Answers'].replace('\n', ' ', regex=True)
# df['Answers'] = df['Answers'].str.strip()
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
# # 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()
df_cleaned = remove_special_characters(df)
uniqueid = df['UniqueId'].tolist()
# Answers = df['Answers'].tolist()
question_cols = df.filter(regex='Example').columns
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'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as f:
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'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'w') as f:
with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'w') as f:
yaml.dump(domain, f, default_flow_style=False, allow_unicode=True)
#appending muitipal answers
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
@@ -40,19 +54,19 @@ 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:
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']
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}"
response_text =f"- text: {example}"
if 'responses' not in new_content:
new_content['responses'] = {}
new_content['responses'][response_name] = yaml.load(response_text)
@@ -61,14 +75,16 @@ for index, row in df.iterrows():
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:
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"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml", 'a') as f:
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 question_cols]
intent_name = row['UniqueId']
examples = [row[col] for col in answer_cols]
separator = "_"
steps = {"intent": intent_name}
gg=[]
@@ -83,25 +99,28 @@ with open(r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml",
steps:
- intent: {steps['intent']}
{output_str}
- action: action_service
""")
# appending multipal question
with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml', "a") as file:
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']
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(f" - \"{example}\"\n")
file.write("\n")
# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml"
# string_to_remove = ' - "nan"'