Przeglądaj źródła

Upload files to 'chatbot'

SadhulaSaiKumar 2 lat temu
rodzic
commit
acb741f08b
1 zmienionych plików z 136 dodań i 0 usunięć
  1. 136
    0
      chatbot/multipal_col.py

+ 136
- 0
chatbot/multipal_col.py Wyświetl plik

@@ -0,0 +1,136 @@
1
+import pandas as pd
2
+import yaml
3
+
4
+# Read the data from the CSV file
5
+df = pd.read_csv(r"D:\livecode\Book1.csv")
6
+
7
+# df['Question'] = df['Question'].replace('\n', ' ', regex=True)
8
+# df['Question'] = df['Question'].str.strip()
9
+
10
+# df['Answers'] = df['Answers'].replace('\n', ' ', regex=True)
11
+# df['Answers'] = df['Answers'].str.strip()
12
+
13
+# # Extract the columns from the dataframe
14
+Question = df['Question'].tolist()
15
+Example1 = df['Example1'].tolist()
16
+Example2 = df['Example2'].tolist()
17
+Example3 = df['Example3'].tolist()
18
+uniqueid = df['uniqueid'].tolist()
19
+Answers = df['Answers'].tolist()
20
+
21
+question_cols = df.filter(regex='Example').columns
22
+answer_cols = df.filter(regex='Answers').columns
23
+#appending intents
24
+import ruamel.yaml as yaml
25
+
26
+# Load the domain YAML file
27
+with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as f:
28
+    domain = yaml.safe_load(f)
29
+    for i in uniqueid:
30
+        domain['intents'].append(i)
31
+        with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'w') as f:
32
+            yaml.dump(domain, f, default_flow_style=False, allow_unicode=True)
33
+
34
+#appending muitipal answers
35
+from ruamel.yaml import YAML
36
+
37
+# Create a YAML object that preserves the formatting of the original YAML file
38
+yaml = YAML()
39
+yaml.preserve_quotes = True
40
+yaml.indent(mapping=2, sequence=4, offset=2)
41
+
42
+# Read in the existing domain file
43
+with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'r') as file:
44
+    domain = yaml.load(file)
45
+
46
+# Generate the new YAML code for each row of data
47
+new_content = {}
48
+for index, row in df.iterrows():
49
+    intent_name = row['uniqueid']
50
+    examples = [row[col] for col in answer_cols ]
51
+    separator = "_"
52
+    for counter, example in enumerate(examples, start=1):
53
+        #example=example.replace(':','')
54
+        response_name = f"utter_{intent_name}{separator}{counter}"
55
+        response_text = f"- text: {example}"
56
+        if 'responses' not in new_content:
57
+            new_content['responses'] = {}
58
+        new_content['responses'][response_name] = yaml.load(response_text)
59
+
60
+# Update the `responses` section of the domain dictionary with the new content
61
+domain['responses'].update(new_content['responses'])
62
+
63
+# Write the updated domain file back to disk, preserving the formatting of the original file
64
+with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\domain.yml', 'w') as file:
65
+    yaml.dump(domain, file)
66
+
67
+#appending multipal action for each rule
68
+with open(r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml", 'a') as f:
69
+    for index, row in df.iterrows():
70
+        intent_name = row['uniqueid']
71
+        examples = [row[col] for col in question_cols]
72
+        separator = "_"
73
+        steps = {"intent": intent_name}
74
+        gg=[]
75
+        for counter, example in enumerate(examples, start=1):
76
+            unique_id = f"{intent_name}{separator}{counter}"
77
+            gg.append({"action": f"utter_{unique_id}"})
78
+
79
+        output_str = "  \n  ".join([f"- action: {question['action'][:-1]}{i}" for i, question in enumerate(gg, start=1)])
80
+
81
+        f.write(f""" 
82
+- rule: Ticket{intent_name}
83
+  steps:
84
+  - intent: {steps['intent']}
85
+  {output_str}
86
+  
87
+""")
88
+
89
+
90
+# appending multipal question
91
+
92
+with open(r'C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml', "a") as file:
93
+    for index, row in df.iterrows():
94
+        intent_name = row['uniqueid']
95
+        examples = [row[col] for col in question_cols]
96
+        file.write(f"""
97
+- intent: {intent_name}
98
+  examples: |
99
+""")
100
+        for example in examples:
101
+            file.write(f"   - \"{example}\"\n")
102
+        file.write("\n")
103
+
104
+
105
+# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml"
106
+# string_to_remove = '  - "nan"'
107
+
108
+# with open(filename, "r") as f:
109
+#     text = f.read()
110
+
111
+# modified_text = text.replace(string_to_remove, "")
112
+
113
+# with open(filename, "w") as f:
114
+#     f.write(modified_text)
115
+
116
+# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\domain.yml"
117
+# string_to_remove = '  - "nan"'
118
+
119
+# with open(filename, "r") as f:
120
+#     text = f.read()
121
+
122
+# modified_text = text.replace(string_to_remove, "")
123
+
124
+# with open(filename, "w") as f:
125
+#     f.write(modified_text)
126
+
127
+# filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml"
128
+# string_to_remove = '  - "nan"'
129
+
130
+# with open(filename, "r") as f:
131
+#     text = f.read()
132
+
133
+# modified_text = text.replace(string_to_remove, "")
134
+
135
+# with open(filename, "w") as f:
136
+#     f.write(modified_text)

Ładowanie…
Anuluj
Zapisz