Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,364 Bytes
19c8b95 |
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 |
# Transpiled from here: https://github.com/virgil-av/numbers-to-words-romanian
import re
import math
TEN = 10
ONE_HUNDRED = 100
ONE_THOUSAND = 1000
ONE_MILLION = 1000000
ONE_BILLION = 1000000000
ONE_TRILLION = 1000000000000
LESS_THAN_TWENTY = [
'zero',
'unu',
'doi',
'trei',
'patru',
'cinci',
'șase',
'șapte',
'opt',
'nouă',
'zece',
'unsprezece',
'doisprezece',
'treisprezece',
'paisprezece',
'cincisprezece',
'șaisprezece',
'șaptesprezece',
'optsprezece',
'nouăsprezece',
]
TENTHS_LESS_THAN_HUNDRED = [
'zero',
'zece',
'douăzeci',
'treizeci',
'patruzeci',
'cincizeci',
'șaizeci',
'șaptezeci',
'optzeci',
'nouăzeci',
]
def parseDecimals (nr):
decimals = int(str(int(nr*100)/100).split(".")[1])
word = ""
if decimals>0:
word += " virgulă"
if decimals < 10:
word += "zero "
word += generateWords(decimals, [])
return word
def match (nr, numberUnitsSingular, numberUnitsPlural):
string = ""
if nr==1:
string = numberUnitsSingular
elif nr==2:
string = "două " + numberUnitsPlural
elif nr<20 or (nr > 100 and nr % 100 < 20):
string = generateWords(nr, []) + " " + numberUnitsPlural
else:
words = generateWords(nr, [])
if nr % 10 == 2:
words = re.sub("doi$", "două", words)
string = words + " de " + numberUnitsPlural
return string
def generateWords (nr, words = []):
remainder = 0
word = ""
if math.isnan(nr):
return "nan"
if nr > ONE_TRILLION - 1:
return nr
if nr==0:
if len(words):
words = " ".join(words)
words = re.sub(",$", "", words)
words = re.sub("\s{2,}", " ", words)
return words
else:
return "zero"
if nr<0:
words.append("minus")
nr = abs(nr)
if nr<20:
remainder = 0
word = LESS_THAN_TWENTY[int(nr)]
word += parseDecimals(nr)
elif nr < ONE_HUNDRED:
remainder = int(nr % TEN)
word = TENTHS_LESS_THAN_HUNDRED[int(nr / TEN)]
if remainder:
word += " și "
elif nr < ONE_THOUSAND:
remainder = nr % ONE_HUNDRED
hundreds = int(nr / ONE_HUNDRED)
if hundreds==1:
word = "o sută"
elif hundreds==2:
word = "două sute"
else:
word = generateWords(hundreds, []) + " sute"
elif nr<ONE_MILLION:
remainder = nr % ONE_THOUSAND
thousands = int(nr / ONE_THOUSAND)
word = match(thousands, "o mie", "mii")
elif nr<ONE_BILLION:
remainder = nr % ONE_MILLION
millions = int(nr / ONE_MILLION)
word = match(millions, "un milion", "milioane")
elif nr<ONE_TRILLION:
remainder = nr % ONE_BILLION
billions = int(nr/ONE_BILLION)
word = match(billions, "un miliard", "miliarde")
words.append(word)
return generateWords(remainder, words)
if __name__ == '__main__':
# Test
print(generateWords(100))
print(generateWords(125))
print(generateWords(118931))
print(generateWords(1259631))
print(generateWords(101230465))
print(generateWords(5101230465))
print(generateWords(999999999999))
|