Skip to content

Commit 755d528

Browse files
authored
Add files via upload
1 parent e1a4846 commit 755d528

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
9.44 KB
Binary file not shown.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import pandas as pd
2+
import re
3+
from chinese_num_transform import chinese_to_arabic
4+
chinese_char_dict = {
5+
'〇' : 0, '一' : 1, '二' : 2, '三' : 3, '四' : 4, '五' : 5, '六' : 6, '七' : 7, '八' : 8, '九' : 9, '零' : 0,
6+
'壹' : 1, '贰' : 2, '叁' : 3, '肆' : 4, '伍' : 5, '陆' : 6, '柒' : 7, '捌' : 8, '玖' : 9, '貮' : 2, '兩' : 2,
7+
}
8+
def load_xlsx(file_name = 'menu.xlsx'):
9+
xls = pd.ExcelFile(file_name)
10+
df = xls.parse(xls.sheet_names[0])
11+
df = df.T
12+
# print(df)
13+
# print(df.to_dict())
14+
return df.to_dict()
15+
16+
def write_xlsx(file_name = 'menu.xlsx'):
17+
menu = {
18+
"item":["滷肉飯","豆花", "燒肉", "雞腿"],
19+
"price":[1, 2, 3, 4],
20+
}
21+
df = pd.DataFrame(data=menu)
22+
# df = (df.T)
23+
# print (df)
24+
df.to_excel(file_name)
25+
26+
def process_data_to_menu(data_dict:dict):
27+
menu_dict = dict()
28+
for key in data_dict:
29+
menu_dict[data_dict[key]['item']]=data_dict[key]['price']
30+
# print(menu_dict)
31+
return menu_dict
32+
33+
def process_price_with_order(menu_dict, order):
34+
# format is 多少個什麼和多少個什麼
35+
order = re.split('和|個', order)
36+
37+
# using stack to process item and amount
38+
price_dict = dict()
39+
top = None
40+
for item in order:
41+
if item.isdigit():
42+
if top == None:
43+
top = item
44+
else:
45+
if item in chinese_char_dict:
46+
item = chinese_to_arabic(item)
47+
price_dict[top] = {'amount':item, 'price':None}
48+
top = None
49+
else:
50+
if top == None:
51+
top = item
52+
else:
53+
if top.isnumeric():
54+
top = chinese_to_arabic(top)
55+
top = int(top)
56+
price_dict[item] = {'amount':top, 'price':None}
57+
top = None
58+
59+
total = 0
60+
for item in price_dict:
61+
if item in menu_dict:
62+
price_dict[item]['price'] = price_dict[item]['amount'] * menu_dict[item]
63+
total += price_dict[item]['price']
64+
none_list = []
65+
for item in price_dict:
66+
if price_dict[item]['price']==None:
67+
none_list.append(item)
68+
69+
print(menu_dict)
70+
print(price_dict)
71+
# return total, none_list
72+
return sum_up_total_line(total, none_list)
73+
74+
def sum_up_total_line(total, none_list):
75+
total = format(total, ",")
76+
line = '總共是' + str(total) + '元。'
77+
if len(none_list)>0:
78+
line+='不過,我們沒有:'
79+
for item in none_list:
80+
line+=item + ', '
81+
return line
82+
83+
# write_xlsx()
84+
# data_dict = load_xlsx('script/menu.xlsx')
85+
# menu_dict = process_data_to_menu(data_dict)
86+
# print(process_price_with_order(menu_dict, '100個火腿和7000個蛋糕'))
87+
# print(process_price_with_order(menu_dict, '兩千萬個雞腿和兩個蛋糕'))

0 commit comments

Comments
 (0)