Pārlūkot izejas kodu

Update 'chatbot/multipal_col.py'

SadhulaSaiKumar 1 gadu atpakaļ
vecāks
revīzija
91328b4b38
1 mainītis faili ar 154 papildinājumiem un 135 dzēšanām
  1. 154
    135
      chatbot/multipal_col.py

+ 154
- 135
chatbot/multipal_col.py Parādīt failu

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

Notiek ielāde…
Atcelt
Saglabāt