-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
126 lines (91 loc) · 2.95 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
116
117
118
119
120
121
122
123
124
125
126
from textblob.classifiers import NaiveBayesClassifier
import asyncio
import websockets
product = ""
bonus_product = ""
mba = ""
products = []
pairs = [["chips", "beer"],
["eggs", "bacon"],
["bread", "butter"],
["buns", "butter"],
["corn flakes", "milk"],
["coffee", "milk"],
["ketchup", "sausage"],
["garlic sauce", "sausage"],
["mustard", "sausage"]
]
def get_bonus_product():
for pair in pairs:
contains_product = pair.__contains__(product)
product_index = pair.index(product)
# lhs (product left-hand side)
# bonus product right-hand side - see on google association rules
if (contains_product and product_index == 0):
bonus_product_index = product_index + 1
bonus_product_rhs = pair[bonus_product_index]
return bonus_product_rhs
# rhs (product right-hand side)
# bonus product left-hand side
elif (contains_product and product_index == 1):
bonus_product_index = product_index - 1
bonus_product_lhs = pair[bonus_product_index]
return bonus_product_lhs
with open('tags.json') as f:
classifier = NaiveBayesClassifier(f, format="json")
def greet():
return "hi"
def add():
products.append(product)
def count():
total = str(len(products))
total_items = "total items: " + total
return total_items
def show():
if (product != None):
status = "<ol><b>groceries status</b> <br>"
for p in products:
status += "<li>" + p + '</li>'
status += '</ol>'
return status
def remove():
products.clear()
return "removed list"
def get_message(arg):
switcher = {
"greeting": greet,
"add": add,
"count": count,
"show": show,
"remove": remove
}
return switcher.get(arg, "sorry I don't understand")
def process_text(input_text):
global product, mba
contains_add = input_text.__contains__("add")
word = input_text.split()
# when product consists of many words
if (contains_add and len(word) > 2):
product = input_text[4:]
elif (contains_add):
product = word[1]
label = classifier.classify(input_text)
print('label ' + label)
user_msg = get_message(label)
return user_msg
async def time(websocket, path):
while True:
global bonus_product
input_text = await websocket.recv()
print('msg' + input_text)
user_msg = process_text(input_text)
if (input_text.__contains__("add")):
bonus_product = get_bonus_product()
mba = "Do you want to also add " + bonus_product + '?'
await websocket.send(mba)
else:
await websocket.send(user_msg())
start_server = websockets.serve(time, "127.0.0.1", 5678)
print('connected')
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()