-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot.py
115 lines (97 loc) · 4.48 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import random
from youtubesearchpython import SearchVideos
from fuzzywuzzy import process
from db_parser import gen_dict
sorry_messages = ["So its related to {}, any more information?",
"Ah yes, {}, whats wrong with it?",
"{}... whats the problem?",
"Okay so {}, what about it",
"So its the {}, tell me more"]
##############################
class ChatBot:
# problems and solutions
"""
base_layer = {
'EDM': {'thing go brr': {'F': ["If F, then undie"]},
'no powa': ["Also F"]
},
"CNC": {}
}
"""
def __init__(self):
# best match minimum threshold
self.MATCH_CONFIDENCE_THRESHOLD = 50
self.RETRY_LIMIT = 2
self.db_dict = gen_dict('./db/')
self.base_layer = self.db_dict
self.layer = self.base_layer # which layer of the nested dict we are atm
self.topic = ""
self.troubleshooting = False
self.retry_counter = 0 # Count attempts to provide automatic help
self.current_advice_index = -1 # Index within deepest layer list
self.prevcat = ""
# this two to be substituted by discord bot
async def stub_input(self, msg):
if not self.troubleshooting:
confidence = 100
looping = False
while confidence >= self.MATCH_CONFIDENCE_THRESHOLD:
match = process.extract(msg, self.layer.keys())
subcat = match[0][0]
confidence = match[0][1]
confidence_difference = match[0][1] - match[1][1]
print("Not troubleshooting: {}".format(subcat))
if confidence < self.MATCH_CONFIDENCE_THRESHOLD:
print("Low confidence {}".format(confidence))
if not looping:
await self.stub_output("Sorry, didnt get that.")
self.retry_counter += 1
if self.retry_counter > self.RETRY_LIMIT:
print("Too many tries")
await self.print_help(self.layer)
return
print("{} Reset retries, confidence {}".format(subcat, confidence))
if confidence_difference < 5:
print("Confidence difference only {}, asking user".format(confidence_difference))
await self.stub_output(random.choice(sorry_messages).format(self.prevcat))
return
self.retry_counter = 0
self.layer = self.layer[subcat]
if not isinstance(self.layer, dict):
self.troubleshooting = True
print(subcat)
await self.stub_output("Hmm.. lets try a few things")
return
elif not looping:
await self.stub_output(random.choice(sorry_messages).format(subcat))
self.prevcat = subcat
looping = True
else: # provide troubleshooting help
print("Troubleshooting")
if msg == "yes":
await self.stub_output("", img_name="happy.gif")
await self.stub_output("Bye!")
await self.reset()
return
self.current_advice_index += 1
if self.current_advice_index < len(self.layer):
await self.stub_output(self.layer[self.current_advice_index])
await self.stub_output("Did that work?")
else:
await self.stub_output("", img_name="sad.gif")
await self.stub_output("Sorry I'm not too sure how to help :( You can connect to a technician if the "
"videos below don't help.")
search = SearchVideos(self.prevcat, offset=1, mode="dict", max_results=2)
search = search.result()
for i in range(2):
await self.stub_output(str(search['search_result'][i]['link']))
# discord bot should implement exit functionality and input validations
async def stub_output(self, msg, img_name=None):
print(msg)
async def print_help(self, layer):
await self.stub_output("", img_name="confused.gif")
problems = ', '.join(list(layer.keys()))
await self.stub_output("It's ok we all get confused sometimes!")
await self.stub_output("Is it related to this? {}...".format(problems))
async def reset(self):
return