-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot.py
104 lines (91 loc) · 2.82 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
from nltk.chat import Chat, util
from nltk.tokenize import word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tag import pos_tag
from nltk.corpus import stopwords
import string
lem = WordNetLemmatizer()
reflections = util.reflections
def prepare_data(sentence, stop_words=()):
tokens_list = word_tokenize(sentence)
tokens_list = [lem.lemmatize(token) for token in tokens_list]
tokens_list = pos_tag(tokens_list)
lemmatized_tokens = []
for (word, tag) in tokens_list:
if tag.startswith('NN'):
pos = 'n'
elif tag.startswith('VB'):
pos = 'v'
else:
pos = 'a'
lemmatized_tokens.append(lem.lemmatize(word, pos))
punc_less_list = []
for token in lemmatized_tokens:
if token not in string.punctuation and token.lower() not in stop_words:
punc_less_list.append(token.lower())
return punc_less_list
def get_bot_response(text):
pairs = return_pairs_list()
chat = Chat(pairs, reflections)
sentences = text.split('.')
bot_response = []
for sentence in sentences:
data = prepare_data(sentence, stopwords.words('english'))
res = ' '.join(data)
bot_response.append(chat.respond(res))
return bot_response
def coupons():
return 'No discount Coupons as of yet'
def order_stat():
return 'Order is out for delivery'
def return_pairs_list():
return [
[
r"hi|hey|hello",
["Hello, How are you today ?", ]
],
[
r"(work)(.*)",
["Well its a nice place!", ]
],
[
r"want|need (refund)",
["Redirecting to customer service", ]
],
[
r"wrong order|order wrong|wrong item|item wrong",
["Can you tell me the exact items that were wrong!", ]
],
[
r"replace order|order replace",
["Please keep the old order as a proof. The delivery person will recieve it", ]
],
[
r"food cold|cold food",
["Sorry for the inconvinence. This won't happen the next time.", ]
],
[
r"item present|missing item",
["Can you tell me the exact items that were missing!", ]
],
[
r"eta|time|wait|arrive",
["The food will arrive in some time.", ]
],
[
r"order|status|deivered|deliver|food|recieve|receive",
[order_stat()]
],
[
r"coupon work|discount work|coupon apply|discount apply",
["Sorry, can you please send me the coupon code!", ]
],
[
r"coupon|discount",
["Currently no coupon code is available!", ]
],
[
r"(.*)",
["Sorry, Did not get you!", ]
],
]