Update 'chatbot/multipal_col.py'

Этот коммит содержится в:
2023-08-30 04:45:08 +00:00
родитель c1d0c08140
Коммит 91328b4b38
+46 -27
Просмотреть файл
@@ -1,37 +1,51 @@
import pandas as pd import pandas as pd
import yaml import yaml
import re
# Read the data from the CSV file # 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) def remove_special_characters(df):
# df['Answers'] = df['Answers'].str.strip() 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 df_cleaned = remove_special_characters(df)
Question = df['Question'].tolist() uniqueid = df['UniqueId'].tolist()
Example1 = df['Example1'].tolist() # Answers = df['Answers'].tolist()
Example2 = df['Example2'].tolist()
Example3 = df['Example3'].tolist()
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 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'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) domain = yaml.safe_load(f)
for i in uniqueid: for i in uniqueid:
domain['intents'].append(i) 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) 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 from ruamel.yaml import YAML
# Create a YAML object that preserves the formatting of the original YAML file # 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) yaml.indent(mapping=2, sequence=4, offset=2)
# Read in the existing domain file # 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) domain = yaml.load(file)
# Generate the new YAML code for each row of data # Generate the new YAML code for each row of data
new_content = {} new_content = {}
for index, row in df.iterrows(): for index, row in df.iterrows():
intent_name = row['uniqueid'] intent_name = row['UniqueId']
examples = [row[col] for col in answer_cols ] examples = [row[col] for col in answer_cols ]
separator = "_" separator = "_"
for counter, example in enumerate(examples, start=1): for counter, example in enumerate(examples, start=1):
#example=example.replace(':','') #example=example.replace(':','')
response_name = f"utter_{intent_name}{separator}{counter}" response_name = f"utter_{intent_name}{separator}{counter}"
response_text = f"- text: {example}" response_text =f"- text: {example}"
if 'responses' not in new_content: if 'responses' not in new_content:
new_content['responses'] = {} new_content['responses'] = {}
new_content['responses'][response_name] = yaml.load(response_text) 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']) domain['responses'].update(new_content['responses'])
# Write the updated domain file back to disk, preserving the formatting of the original file # 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) yaml.dump(domain, file)
#appending multipal action for each rule #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(): for index, row in df.iterrows():
intent_name = row['uniqueid'] intent_name = row['UniqueId']
examples = [row[col] for col in question_cols] examples = [row[col] for col in answer_cols]
separator = "_" separator = "_"
steps = {"intent": intent_name} steps = {"intent": intent_name}
gg=[] gg=[]
@@ -83,25 +99,28 @@ with open(r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml",
steps: steps:
- intent: {steps['intent']} - intent: {steps['intent']}
{output_str} {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(): for index, row in df.iterrows():
intent_name = row['uniqueid'] intent_name = row['UniqueId']
examples = [row[col] for col in question_cols] examples = [row[col] for col in question_cols]
file.write(f""" file.write(f"""
- intent: {intent_name} - intent: {intent_name}
examples: | examples: |
""") """)
for example in examples: for example in examples:
file.write(f" - \"{example}\"\n") file.write(f" - \"{example}\"\n")
file.write("\n") file.write("\n")
# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml" # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml"
# string_to_remove = ' - "nan"' # string_to_remove = ' - "nan"'