/
actions.py
307 lines (234 loc) · 9.95 KB
/
actions.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# This files contains your custom actions which can be used to run
# custom Python code.
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/core/actions/#custom-actions/
# This is a simple example for a custom action which uttecity "Hello World!"
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_core_sdk.events import SlotSet
from difflib import SequenceMatcher
from Food import Food
import json
def Menu():
mylist = []
f1 = Food("Lẩu dầu cay không vụn", 200, "hotpot",0)
f2 = Food("Lẩu cay mala", 250, "hotpot",0)
f3 = Food("Lẩu thảo dược", 230, "hotpot",0)
f4 = Food("Thịt bò bông tuyết Mỹ", 140, "beef",0)
f5 = Food("Thịt bò Nhật", 75, "beef",0)
f6 = Food("Cá basa phi lê", 70, "food",0)
f7 = Food("Múa mì", 20, "special",0)
f8 = Food("Coca", 230, "drink",0)
f9 = Food("Lavie", 230, "drink",0)
mylist.append(f1)
mylist.append(f2)
mylist.append(f3)
mylist.append(f4)
mylist.append(f5)
mylist.append(f6)
mylist.append(f7)
mylist.append(f8)
mylist.append(f9)
return mylist
class ActionAnswerPriceFood(Action):
def name(self) -> Text:
return "answer_price_food"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("answer_price_food")
food = str(tracker.get_slot('food'))
if food is None:
dispatcher.utter_message("Nhà hàng mình không có món kia bạn ạ")
Max = SequenceMatcher(a=food, b="Lẩu dầu cay không vụn").ratio()
for item in Menu():
temp = SequenceMatcher(a=food, b=item.name).ratio()
if temp >= Max:
Max = temp
max_food = Food(item.name, item.price, item.type,0)
if max_food.type == "beef":
dispatcher.utter_message(
"Món "+max_food.name+" bên em đang bán có giá " + str(max_food.price)+"k/100g ạ")
elif max_food.type == "special":
dispatcher.utter_message("Tiết mục "+max_food.name+" bên em đang bán có giá " + str(
max_food.price)+"k bao gồm cả phần mì và nhân viên ra múa mì luôn ạ")
elif max_food.type == "drink":
dispatcher.utter_message(
"Dạ "+max_food.name+" bên em đang bán có giá " + str(max_food.price)+"k/chai ạ")
else:
dispatcher.utter_message(
"Món "+max_food.name+" bên em đang bán có giá " + str(max_food.price)+"k/suất ạ")
return [SlotSet("food",None)]
def ConvertNumber(number):
convert = {
"một": 1,
"hai": 2,
"ba": 3,
"bốn": 4,
"năm": 5,
"sáu": 6,
"bảy": 7,
"tám": 8,
"chín": 9,
"mười": 10,
}
return convert[number]
class OrderFood(Action):
def name(self) -> Text:
return "order_food"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("order_food")
food = tracker.get_slot('food') # Chứa các món trong cầu người dùng
quanity = tracker.get_slot('quanity')
jsonOrder = tracker.get_slot('listOrder') # Chứa các món trong order người dùng
tOrder = tracker.get_slot('totalOrder')
## Nếu kiểm tra món ăn không tồn tại trong thực đơn
if food is None :
dispatcher.utter_message("Dạ bên mình không có món này bạn ạ")
return []
rs =""
for item in food:
rs+=item + ","
if quanity is None:
## Kiểm tra không có số lượng
dispatcher.utter_message("Bạn lấy "+rs+" số lượng thế nào ạ ?")
return []
print(food)
print(quanity)
if len(food) > len(quanity):
## Kiểm trả có số lượng nhưng thiếu
dispatcher.utter_message("Bạn kiểm tra lại các món ăn đã kèm số lượng tương ứng chưa ạ ?")
return []
## Đổi chứ sáng số
nQuanity = []
for item in quanity:
if item.isnumeric() is False:
nQuanity.append(ConvertNumber(item.lower()))
else:
nQuanity.append(int(item))
Max = SequenceMatcher(a=food[0], b="Lẩu dầu cay không vụn").ratio()
list_max_food = []
for element in food:
for item in Menu():
temp = SequenceMatcher(a=element, b=item.name).ratio()
if temp >= Max:
Max = temp
max_food= Food(item.name, item.price, item.type,item.quanity)
Max = SequenceMatcher(a="a", b="b").ratio()
list_max_food.append(max_food)
lOrder = []
if tOrder is None or jsonOrder is None or lOrder is None:
tOrder = 0
lOrder = []
jsonOrder = '[]'
lOrder = json.loads(jsonOrder)
count =0
for element in list_max_food:
tOrder+= (element.price*nQuanity[count])
element.quanity = nQuanity[count]
count+=1
lOrder.append(element.__dict__)
jsonOrder = json.dumps([item for item in lOrder])
dispatcher.utter_message("Mình đã xác nhận đặt món cho bạn rồi nhé. Bạn muốn đặt gì thêm không ?")
return [SlotSet("listOrder",jsonOrder),SlotSet("totalOrder", tOrder),SlotSet("food",None),SlotSet("quanity",None)]
class AnswerOrderFood(Action):
def name(self) -> Text:
return "answer_food_in_order"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("answer_food_in_order")
jsonOrder = tracker.get_slot('listOrder')
if jsonOrder is None:
dispatcher.utter_message("Bạn chưa đặt món nào cả !")
return []
lOrder = json.loads(jsonOrder)
result = ""
for item in lOrder:
result+= "\n"+"(*) "+item["name"]+" - Số lượng: "+str(item["quanity"])
dispatcher.utter_message("Danh sách món bạn đã đặt :")
dispatcher.utter_message(result)
return []
class AnswerOrderFoodv2(Action):
def name(self) -> Text:
return "answer_total_order"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("answer_total_order")
totalOrder = tracker.get_slot('totalOrder')
if totalOrder is None:
dispatcher.utter_message("Bạn chưa đặt món mà :D")
else:
dispatcher.utter_message("Dạ tổng hóa đơn hiện tại của bạn hết: "+str(totalOrder)+ "k ạ !")
return []
class AnswerOrderFoodv3(Action):
def name(self) -> Text:
return "action_remove_food_in_order"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("action_remove_food_in_order")
food = str(tracker.get_slot('food'))
jsonOrder = tracker.get_slot('listOrder')
tOrder = tracker.get_slot('totalOrder')
if jsonOrder is None or tOrder is None:
dispatcher.utter_message("Bạn chưa đặt món nào cả :D")
return
if food is None:
dispatcher.utter_message("Nhà hàng mình làm gì có món kia bạn ơi")
return
Max = SequenceMatcher(a=food, b="Lẩu dầu cay không vụn").ratio()
for item in Menu():
temp = SequenceMatcher(a=food, b=item.name).ratio()
if temp >= Max:
Max = temp
max_food = Food(item.name, item.price, item.type,item.quanity)
lOrder = json.loads(jsonOrder)
print(len(lOrder))
print(max_food.name)
for element in lOrder:
tOrder-=(element["price"]*element["quanity"])
## Thuật toán xóa
i=0
for item in lOrder:
if item["name"]==max_food.name:
i+=1
k = len(lOrder)-1
for x in range(i,k+1):
if lOrder[x]["name"]==max_food.name:
for y in range(0,i):
if lOrder[y]["name"]!=max_food.name:
lOrder[x],lOrder[y]=lOrder[y],lOrder[x]
print(lOrder)
for x in range(i):
lOrder.pop(0)
jsonOrder = json.dumps([item for item in lOrder])
dispatcher.utter_message("Mình đã bỏ "+max_food.name+" cho bạn rồi nhé !")
return [SlotSet("listOrder",jsonOrder),SlotSet("totalOrder", tOrder),SlotSet("food",None)]
class SearchFood(Action):
def name(self) -> Text:
return "answer_search_food"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("answer_search_food")
food = tracker.get_slot('food')
print(food)
if food is None:
dispatcher.utter_message("Dạ bên mình không có bạn ạ")
return []
Max = SequenceMatcher(a=food[0], b="Lẩu dầu cay không vụn").ratio()
for item in Menu():
temp = SequenceMatcher(a=food[0], b=item.name).ratio()
if temp >= Max:
Max = temp
print(Max)
if Max < 0.7:
dispatcher.utter_message("Dạ bên mình không có bạn ạ")
else:
dispatcher.utter_message("Bên mình có nhé !")
return [SlotSet("food",None)]