-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplebot.py
61 lines (52 loc) · 1.81 KB
/
simplebot.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
from textblob import TextBlob
import twily_classifier as cl
import stop_words as stop_words
import json
with open('twilybot.json', 'r') as f:
array = json.load(f)
CONVERSATION = array["conversations"]
BOT_NAME = 'Cheems'
STOP_WORDS = stop_words.sw_list
neg_distribution = []
def sentiment(u_input):
blob_it = cl.trainer().prob_classify(u_input)
npd = round(blob_it.prob("neg"), 2)
neg_distribution.append(npd)
return npd
def simplebot(user_input):
user_blob = TextBlob(user_input)
lower_input = user_blob.lower()
token_input = lower_input.words
filtered_input = [w for w in token_input if w not in STOP_WORDS]
response_set = set()
for con_list in CONVERSATION:
for sentence in con_list:
sentence_split = sentence.split()
if set(filtered_input).intersection(sentence_split):
response_set.update(con_list)
if not response_set:
return "I'm sorry, ask again please!"
else:
return max(response_set, key=len)
def escalation(user_input):
# Takes an argument, user_input, in form of a string ...
live_rep = f'We apologize {BOT_NAME} is unable to assist you, we are getting a live representative for you, please stay with us'
sentiment(user_input)
list_len = len(neg_distribution)
bot_response = simplebot(user_input)
if list_len > 3:
last_3 = neg_distribution[-3:]
if last_3[0] > .40 and last_3[0] <= last_3[1] <= last_3[2]:
return live_rep
else:
return bot_response
else:
return bot_response
# if __name__ == '__main__':
# while True:
# try:
# user_input = input('You: ')
# print(escalation(user_input))
# print(neg_distribution)
# except (KeyboardInterrupt, EOFError, SystemExit):
# break