feat: function correctly encodes arabic number to chinese
This commit is contained in:
parent
ada5130c9b
commit
1a5073c2a7
@ -1,4 +1,4 @@
|
||||
C = {
|
||||
numerals = {
|
||||
"-": "负",
|
||||
".": "点",
|
||||
0: "零",
|
||||
@ -15,28 +15,13 @@ C = {
|
||||
100: "百",
|
||||
1000: "千",
|
||||
10000: "万",
|
||||
11: "十一",
|
||||
12: "十二",
|
||||
13: "十三",
|
||||
14: "十四",
|
||||
15: "十五",
|
||||
16: "十六",
|
||||
17: "十七",
|
||||
18: "十八",
|
||||
19: "十九",
|
||||
}
|
||||
|
||||
# "Ten^x" dictonary
|
||||
T = {2: 10, 3: 100, 4: 1000, 5: 10000}
|
||||
T = {1: 0, 2: 10, 3: 100, 4: 1000, 5: 10000}
|
||||
|
||||
|
||||
def to_chinese_numeral(n):
|
||||
# Check if n in C dictonary
|
||||
try:
|
||||
return C[n]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
# Check if negative, turn positive and remeber state
|
||||
if n < 0:
|
||||
n = abs(n)
|
||||
@ -44,6 +29,16 @@ def to_chinese_numeral(n):
|
||||
else:
|
||||
is_negative = False
|
||||
|
||||
# return C[n] if n in C dictonary
|
||||
if n % 100 != 0:
|
||||
try:
|
||||
if is_negative:
|
||||
return "".join([numerals["-"], numerals[n]])
|
||||
else:
|
||||
return numerals[n]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
# Split number in iteger list and fractional (None if not exists)
|
||||
if type(n) is not int:
|
||||
n = str(n).split(".")
|
||||
@ -71,7 +66,7 @@ def to_chinese_numeral(n):
|
||||
integer.pop(0)
|
||||
|
||||
# Pop last digit in blueprint if it's a 0
|
||||
for i in range(len(blueprint) - 1, 1, -1):
|
||||
for i in range(len(blueprint) - 1, 0, -1):
|
||||
if blueprint[i] == 0:
|
||||
blueprint.pop(i)
|
||||
else:
|
||||
@ -79,19 +74,36 @@ def to_chinese_numeral(n):
|
||||
|
||||
# Remove grouped zeros
|
||||
was_zero = False
|
||||
index_to_remove = []
|
||||
for j, i in enumerate(blueprint):
|
||||
if i == 0 and not was_zero:
|
||||
was_zero = True
|
||||
elif i == 0 and was_zero:
|
||||
blueprint.pop(j)
|
||||
index_to_remove.append(j)
|
||||
elif i != 0 and was_zero:
|
||||
was_zero = False
|
||||
|
||||
print(blueprint)
|
||||
print(fractional)
|
||||
index_to_remove.sort(
|
||||
reverse=True
|
||||
) # Reverse indices to avoid index errors when applying pop()
|
||||
[blueprint.pop(x) for x in index_to_remove]
|
||||
|
||||
return is_negative
|
||||
# remove first item in blueprint if second item in blueprint is between 10 and 19. Skip step if index error
|
||||
try:
|
||||
if sum(blueprint[0:2]) == 11:
|
||||
blueprint.pop(0)
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
# Add symbols - if n was negative and . if it was a fraction
|
||||
if is_negative:
|
||||
blueprint.insert(0, "-")
|
||||
|
||||
to_chinese_numeral(-90090.358)
|
||||
to_chinese_numeral(90000)
|
||||
if fractional is not None:
|
||||
blueprint.append(".")
|
||||
[blueprint.append(x) for x in fractional]
|
||||
|
||||
# Apply chinese encoding to blueprint and fractional
|
||||
encoding = [numerals[x] for x in blueprint]
|
||||
|
||||
return "".join(encoding)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user