Skip to content

Commit 48c4e77

Browse files
authored
Add files via upload
1 parent d4ad792 commit 48c4e77

File tree

14 files changed

+663
-0
lines changed

14 files changed

+663
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
NUMBER_CN2AN = {
2+
"零": 0,
3+
"〇": 0,
4+
"一": 1,
5+
"壹": 1,
6+
"幺": 1,
7+
"二": 2,
8+
"贰": 2,
9+
"两": 2,
10+
"三": 3,
11+
"叁": 3,
12+
"四": 4,
13+
"肆": 4,
14+
"五": 5,
15+
"伍": 5,
16+
"六": 6,
17+
"陆": 6,
18+
"七": 7,
19+
"柒": 7,
20+
"八": 8,
21+
"捌": 8,
22+
"九": 9,
23+
"玖": 9,
24+
}
25+
UNIT_CN2AN = {
26+
"十": 10,
27+
"拾": 10,
28+
"百": 100,
29+
"佰": 100,
30+
"千": 1000,
31+
"仟": 1000,
32+
"万": 10000,
33+
"亿": 100000000,
34+
}
35+
UNIT_LOW_AN2CN = {
36+
10: "十",
37+
100: "百",
38+
1000: "千",
39+
10000: "万",
40+
100000000: "亿",
41+
}
42+
NUMBER_LOW_AN2CN = {
43+
0: "零",
44+
1: "一",
45+
2: "二",
46+
3: "三",
47+
4: "四",
48+
5: "五",
49+
6: "六",
50+
7: "七",
51+
8: "八",
52+
9: "九",
53+
}
54+
NUMBER_UP_AN2CN = {
55+
0: "零",
56+
1: "壹",
57+
2: "贰",
58+
3: "叁",
59+
4: "肆",
60+
5: "伍",
61+
6: "陆",
62+
7: "柒",
63+
8: "捌",
64+
9: "玖",
65+
}
66+
UNIT_LOW_ORDER_AN2CN = [
67+
"",
68+
"十",
69+
"百",
70+
"千",
71+
"万",
72+
"十",
73+
"百",
74+
"千",
75+
"亿",
76+
"十",
77+
"百",
78+
"千",
79+
"万",
80+
"十",
81+
"百",
82+
"千",
83+
]
84+
UNIT_UP_ORDER_AN2CN = [
85+
"",
86+
"拾",
87+
"佰",
88+
"仟",
89+
"万",
90+
"拾",
91+
"佰",
92+
"仟",
93+
"亿",
94+
"拾",
95+
"佰",
96+
"仟",
97+
"万",
98+
"拾",
99+
"佰",
100+
"仟",
101+
]
102+
STRICT_CN_NUMBER = {
103+
"零": "零",
104+
"一": "一壹",
105+
"二": "二贰",
106+
"三": "三叁",
107+
"四": "四肆",
108+
"五": "五伍",
109+
"六": "六陆",
110+
"七": "七柒",
111+
"八": "八捌",
112+
"九": "九玖",
113+
"十": "十拾",
114+
"百": "百佰",
115+
"千": "千仟",
116+
"万": "万",
117+
"亿": "亿",
118+
}
119+
NORMAL_CN_NUMBER = {
120+
"零": "零〇",
121+
"一": "一壹幺",
122+
"二": "二贰两",
123+
"三": "三叁仨",
124+
"四": "四肆",
125+
"五": "五伍",
126+
"六": "六陆",
127+
"七": "七柒",
128+
"八": "八捌",
129+
"九": "九玖",
130+
"十": "十拾",
131+
"百": "百佰",
132+
"千": "千仟",
133+
"万": "万",
134+
"亿": "亿",
135+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import re
2+
from chinese_num_conf import UNIT_CN2AN
3+
all_num = "零一二三四五六七八九"
4+
all_unit = "".join(list(UNIT_CN2AN.keys()))
5+
cn_pattern = f"负?([{all_num}{all_unit}]+点)?[{all_num}{all_unit}]+"
6+
smart_cn_pattern = f"-?([0-9]+.)?[0-9]+[{all_unit}]+"
7+
def chinese_to_arabic(chinese_num):
8+
chinese_char_dict = {
9+
'〇' : 0, '一' : 1, '二' : 2, '三' : 3, '四' : 4, '五' : 5, '六' : 6, '七' : 7, '八' : 8, '九' : 9, '零' : 0,
10+
'壹' : 1, '贰' : 2, '叁' : 3, '肆' : 4, '伍' : 5, '陆' : 6, '柒' : 7, '捌' : 8, '玖' : 9, '貮' : 2, '兩' : 2,
11+
}
12+
chinese_char_unit = {
13+
'十' : 10,
14+
'拾' : 10,
15+
'百' : 100,
16+
'佰' : 100,
17+
'千' : 1000,
18+
'仟' : 1000,
19+
'万' : 10000,
20+
'萬' : 10000,
21+
'亿' : 100000000,
22+
'億' : 100000000,
23+
'兆' : 1000000000000,
24+
}
25+
unit = 0
26+
ldig = []
27+
# print(chinese_num)
28+
for char_digit in reversed(chinese_num):
29+
# print(char_digit)
30+
if chinese_char_unit.get(char_digit) != None:
31+
unit = chinese_char_unit.get(char_digit)
32+
# print("unit:"+str(unit))
33+
if unit == 10000 or unit == 100000000:
34+
ldig.append(unit)
35+
unit = 1
36+
else:
37+
dig = chinese_char_dict.get(char_digit)
38+
# print("num:"+str(dig))
39+
if unit:
40+
dig *= unit
41+
unit = 0
42+
ldig.append(dig)
43+
if unit == 10:
44+
ldig.append(10)
45+
val, tmp = 0, 0
46+
for x in reversed(ldig):
47+
if x == 10000 or x == 100000000:
48+
val += tmp * x
49+
tmp = 0
50+
else:
51+
tmp += x
52+
val += tmp
53+
return val
54+
def __sub_util(inputs, sub_mode: str = "number") -> str:
55+
try:
56+
if inputs:
57+
if sub_mode == "date":
58+
return re.sub(fr"(({smart_cn_pattern})|({cn_pattern}))",
59+
lambda x: str(chinese_to_arabic(x.group())), inputs)
60+
elif sub_mode == "fraction":
61+
if inputs[0] != "百":
62+
frac_result = re.sub(cn_pattern,
63+
lambda x: str(chinese_to_arabic(x.group())), inputs)
64+
numerator, denominator = frac_result.split("分之")
65+
return f"{denominator}/{numerator}"
66+
else:
67+
return inputs
68+
elif sub_mode == "percent":
69+
return re.sub(f"(?<=百分之){cn_pattern}",
70+
lambda x: str(chinese_to_arabic(x.group())), inputs).replace("百分之", "") + "%"
71+
elif sub_mode == "celsius":
72+
return re.sub(f"{cn_pattern}(?=攝氏度)",
73+
lambda x: str(chinese_to_arabic(x.group())), inputs).replace("攝氏度", "℃")
74+
elif sub_mode == "number":
75+
return str(chinese_to_arabic(inputs))
76+
else:
77+
raise Exception(f"error sub_mode: {sub_mode} !")
78+
except Exception as e:
79+
print(f"WARN: {e}")
80+
return inputs
81+
def chinese_in_string_transform(inputs: str) -> str:
82+
inputs = inputs.replace("廿", "二十").replace("半", "0.5").replace("两", "2")
83+
# date
84+
inputs = re.sub(
85+
fr"((({smart_cn_pattern})|({cn_pattern}))年)?([{all_num}十]+月)?([{all_num}十]+日)?",
86+
lambda x: __sub_util(x.group(),"date"), inputs)
87+
# fraction
88+
inputs = re.sub(fr"{cn_pattern}分之{cn_pattern}",
89+
lambda x: __sub_util(x.group(),"fraction"), inputs)
90+
# percent
91+
inputs = re.sub(fr"百分之{cn_pattern}",
92+
lambda x: __sub_util(x.group(),"percent"), inputs)
93+
# celsius
94+
inputs = re.sub(fr"{cn_pattern}攝氏度",
95+
lambda x: __sub_util(x.group(),"celsius"), inputs)
96+
# number
97+
output = re.sub(cn_pattern,
98+
lambda x: __sub_util(x.group(),"number"), inputs)
99+
return output
100+
101+
if __name__ == '__main__':
102+
# num = '一千七百七十一個蛋糕'
103+
# print(chinese_in_string_transform(num))
104+
nume = '十七億三千萬零八'
105+
print(chinese_to_arabic(nume))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def fake_rate(self):
2+
self.line_speaker('請描述你所要計算的機率問題。講完後請說我說完了')
3+
while(not '完' in order_line):
4+
order_line = self.listener()
5+
self.line_speaker('請問所求為會發生還是不會發生的機率?')
6+
order_line = self.listener()
7+
while(not '會' in order_line):
8+
order_line = self.listener()
9+
if '不會' in order_line:
10+
self.line_speaker('二分之一')
11+
else:
12+
self.line_speaker('二分之一')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def hello(self):
2+
self.line_speaker('你好。')
3+
if self.master!=None:
4+
self.line_speaker('我的'+self.master)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def my_name(self):
2+
self.master=self.last_order.split('是')[-1]
3+
self.line_speaker('你就是我的Master嗎?'+self.master)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from order_process.process_order import load_xlsx, process_data_to_menu, process_price_with_order
2+
menu_xlsx_path = 'script\order_bot_program_module\order_process\menu.xlsx'
3+
data_dict = load_xlsx(file_name=menu_xlsx_path)
4+
menu_dict = process_data_to_menu(data_dict)
5+
def order_meal(self):
6+
total_order = ''
7+
self.line_speaker('請問要點些什麼呢?')
8+
while(1):
9+
order_menu_line = self.listener()
10+
if '和' in order_menu_line or '個' in order_menu_line:
11+
total_order+=order_menu_line+'和'
12+
elif '餐' in order_menu_line or '點完' in order_menu_line:
13+
# 點完餐
14+
break
15+
else:
16+
self.line_speaker('不好意思,請再說一次。')
17+
self.line_speaker(process_price_with_order(menu_dict, total_order))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def what_eat(self):
2+
eat_count = 0
3+
self.line_speaker('想吃飯還是麵?')
4+
while(eat_count<2):
5+
order_line = self.listener()
6+
if '飯' in order_line:
7+
self.line_speaker('我們沒有飯')
8+
eat_count+=1
9+
elif '麵' in order_line:
10+
self.line_speaker('我們沒有麵')
11+
eat_count+=1
12+
else:
13+
self.line_speaker('我們沒有這個')
14+
self.line_speaker('我們有水餃')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from datetime import datetime
2+
def what_time(self):
3+
now = datetime.now()
4+
res_text = '現在時間是 %d 點 %d 分 %d 秒' % (now.hour, now.minute, now.second)
5+
self.line_speaker(res_text)

0 commit comments

Comments
 (0)