forked from KikeVen/simplebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplebot.py
87 lines (69 loc) · 2.65 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
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
""" Simple rule base chatbot with emotional monitoring """
from textblob import TextBlob
import twily_classifier as cl
import stop_words as stopwords
import json
with open('twilybot.json', 'r') as f:
array = json.load(f)
CONVERSATION = array["conversations"]
BOT_NAME = 'Twily'
STOP_WORDS = stopwords.sw_list
neg_distribution = []
def sentiment(u_input):
"""Utilitarian function: Appends 'neg_distribution'
with negative probability, returns Negative Probability"""
blob_it = cl.trainer().prob_classify(u_input)
npd = round(blob_it.prob("neg"), 2)
neg_distribution.append(npd)
return npd
def simplebot(user):
"""Rule base bot, takes an argument, user input in form of a string.
In sequence will pre-process the string. Lower case, tokenize and remove
stop words. iterates through CONVERSATION, if filtered_input intersects
response_set is updated. if the set is empty, it returns a message,
else it returns the longest string in the set"""
user_input = user
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 am sorry, I don't have an answer, ask again"
else:
return max(response_set, key=len)
def escalation(uinput):
"""Monitors user sentiment index, takes an argument,
user_input, in form of a string. If the emotional index,
set by sentiment() and taken from neg_distribution,
increases above a set threshold and it is sustained
an automatic respose/action is triggered.
simultaneously sending user_input to simplebot() for a
response"""
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(uinput)
list_len = len(neg_distribution)
bot_response = simplebot(uinput)
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