mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 14:52:25 +00:00
Many PEP8 fixes
Signed-off-by: William Moreno Reyes <williamjmorenor@gmail.com>
This commit is contained in:
@@ -70,6 +70,7 @@ CONVERTER_CLASSES = {
|
||||
'uk': lang_UK.Num2Word_UK()
|
||||
}
|
||||
|
||||
|
||||
def num2words(number, ordinal=False, lang='en'):
|
||||
# We try the full language first
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
|
||||
@@ -41,29 +41,24 @@ class Num2Word_Base(object):
|
||||
|
||||
self.MAXVAL = 1000 * self.cards.order[0]
|
||||
|
||||
|
||||
def set_numwords(self):
|
||||
self.set_high_numwords(self.high_numwords)
|
||||
self.set_mid_numwords(self.mid_numwords)
|
||||
self.set_low_numwords(self.low_numwords)
|
||||
|
||||
|
||||
def gen_high_numwords(self, units, tens, lows):
|
||||
out = [u + t for t in tens for u in units]
|
||||
out.reverse()
|
||||
return out + lows
|
||||
|
||||
|
||||
def set_mid_numwords(self, mid):
|
||||
for key, val in mid:
|
||||
self.cards[key] = val
|
||||
|
||||
|
||||
def set_low_numwords(self, numwords):
|
||||
for word, n in zip(numwords, range(len(numwords) - 1, -1, -1)):
|
||||
self.cards[n] = word
|
||||
|
||||
|
||||
def splitnum(self, value):
|
||||
for elem in self.cards:
|
||||
if elem > value:
|
||||
@@ -89,7 +84,6 @@ class Num2Word_Base(object):
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def to_cardinal(self, value):
|
||||
try:
|
||||
assert int(value) == value
|
||||
@@ -110,21 +104,20 @@ class Num2Word_Base(object):
|
||||
words, num = self.clean(val)
|
||||
return self.title(out + words)
|
||||
|
||||
|
||||
def float2tuple(self, value):
|
||||
pre = int(value)
|
||||
post = abs(value - pre) * 10**self.precision
|
||||
if abs(round(post) - post) < 0.01:
|
||||
# We generally floor all values beyond our precision (rather than rounding), but in
|
||||
# cases where we have something like 1.239999999, which is probably due to python's
|
||||
# handling of floats, we actually want to consider it as 1.24 instead of 1.23
|
||||
# We generally floor all values beyond our precision (rather than
|
||||
# rounding), but in cases where we have something like 1.239999999,
|
||||
# which is probably due to python's handling of floats, we actually
|
||||
# want to consider it as 1.24 instead of 1.23
|
||||
post = int(round(post))
|
||||
else:
|
||||
post = int(math.floor(post))
|
||||
|
||||
return pre, post
|
||||
|
||||
|
||||
def to_cardinal_float(self, value):
|
||||
try:
|
||||
float(value) == value
|
||||
@@ -146,11 +139,9 @@ class Num2Word_Base(object):
|
||||
|
||||
return " ".join(out)
|
||||
|
||||
|
||||
def merge(self, curr, next):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def clean(self, val):
|
||||
out = val
|
||||
while len(val) != 1:
|
||||
@@ -172,7 +163,6 @@ class Num2Word_Base(object):
|
||||
val = out
|
||||
return out[0]
|
||||
|
||||
|
||||
def title(self, value):
|
||||
if self.is_title:
|
||||
out = []
|
||||
@@ -185,30 +175,24 @@ class Num2Word_Base(object):
|
||||
value = " ".join(out)
|
||||
return value
|
||||
|
||||
|
||||
def verify_ordinal(self, value):
|
||||
if not value == int(value):
|
||||
raise TypeError(self.errmsg_floatord % value)
|
||||
if not abs(value) == value:
|
||||
raise TypeError(self.errmsg_negord % value)
|
||||
|
||||
|
||||
def verify_num(self, value):
|
||||
return 1
|
||||
|
||||
|
||||
def set_wordnums(self):
|
||||
pass
|
||||
|
||||
|
||||
def to_ordinal(self, value):
|
||||
return self.to_cardinal(value)
|
||||
|
||||
|
||||
def to_ordinal_num(self, value):
|
||||
return value
|
||||
|
||||
|
||||
# Trivial version
|
||||
def inflect(self, value, text):
|
||||
text = text.split("/")
|
||||
@@ -216,8 +200,7 @@ class Num2Word_Base(object):
|
||||
return text[0]
|
||||
return "".join(text)
|
||||
|
||||
|
||||
#//CHECK: generalise? Any others like pounds/shillings/pence?
|
||||
# //CHECK: generalise? Any others like pounds/shillings/pence?
|
||||
def to_splitnum(self, val, hightxt="", lowtxt="", jointxt="",
|
||||
divisor=100, longval=True, cents=True):
|
||||
out = []
|
||||
@@ -252,23 +235,18 @@ class Num2Word_Base(object):
|
||||
|
||||
return " ".join(out)
|
||||
|
||||
|
||||
def to_year(self, value, **kwargs):
|
||||
return self.to_cardinal(value)
|
||||
|
||||
|
||||
def to_currency(self, value, **kwargs):
|
||||
return self.to_cardinal(value)
|
||||
|
||||
|
||||
def base_setup(self):
|
||||
pass
|
||||
|
||||
|
||||
def setup(self):
|
||||
pass
|
||||
|
||||
|
||||
def test(self, value):
|
||||
try:
|
||||
_card = self.to_cardinal(value)
|
||||
@@ -285,5 +263,5 @@ class Num2Word_Base(object):
|
||||
except:
|
||||
_ordnum = "invalid"
|
||||
|
||||
print ("For %s, card is %s;\n\tord is %s; and\n\tordnum is %s." %
|
||||
(value, _card, _ord, _ordnum))
|
||||
print("For %s, card is %s;\n\tord is %s; and\n\tordnum is %s."
|
||||
% (value, _card, _ord, _ordnum))
|
||||
|
||||
@@ -18,9 +18,9 @@ import sys
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
|
||||
def to_s(val):
|
||||
if PY3:
|
||||
return str(val)
|
||||
else:
|
||||
return unicode(val)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
from __future__ import division, unicode_literals, print_function
|
||||
from . import lang_EU
|
||||
|
||||
|
||||
class Num2Word_AR(lang_EU.Num2Word_EU):
|
||||
def set_high_numwords(self, high):
|
||||
max = 3 + 3*len(high)
|
||||
@@ -30,7 +31,7 @@ class Num2Word_AR(lang_EU.Num2Word_EU):
|
||||
self.errmsg_nornum = "Only numbers may be converted to words."
|
||||
self.exclude_title = ["و", "فاصلة", "سالب"]
|
||||
|
||||
self.mid_numwords = [(1000000, "مليون"),(1000, "ألف"), (100, "مئة"),
|
||||
self.mid_numwords = [(1000000, "مليون"), (1000, "ألف"), (100, "مئة"),
|
||||
(90, "تسعين"), (80, "ثمانين"), (70, "سبعين"),
|
||||
(60, "ستين"), (50, "خمسين"), (40, "أربعين"),
|
||||
(30, "ثلاثين")]
|
||||
@@ -39,15 +40,14 @@ class Num2Word_AR(lang_EU.Num2Word_EU):
|
||||
"اثناعشر", "أحد عشر", "عشرة", "تسعة", "ثمانية",
|
||||
"سبعة", "ستة", "خمسة", "أربعة", "ثلاثة", "اثنين",
|
||||
"واحد", "صفر"]
|
||||
self.ords = { "واحد" : "أول",
|
||||
"اثنين" : "ثاني",
|
||||
"ثلاثة" : "ثالث",
|
||||
self.ords = {"واحد": "أول",
|
||||
"اثنين": "ثاني",
|
||||
"ثلاثة": "ثالث",
|
||||
"أربعة": "رابع",
|
||||
"خمسة" : "خامس",
|
||||
"ثمانية" : "ثامن",
|
||||
"تسعة" : "تاسع",
|
||||
"اثناعشر" : "ثاني عشر" }
|
||||
|
||||
"خمسة": "خامس",
|
||||
"ثمانية": "ثامن",
|
||||
"تسعة": "تاسع",
|
||||
"اثناعشر": "ثاني عشر" }
|
||||
|
||||
def merge(self, lpair, rpair):
|
||||
ltext, lnum = lpair
|
||||
@@ -68,7 +68,6 @@ class Num2Word_AR(lang_EU.Num2Word_EU):
|
||||
return ("%s %s"%(ltext, rtext), lnum * rnum)
|
||||
return ("%s، %s"%(ltext, rtext), lnum + rnum)
|
||||
|
||||
|
||||
def to_ordinal(self, value):
|
||||
self.verify_ordinal(value)
|
||||
outwords = self.to_cardinal(value).split(" ")
|
||||
@@ -82,12 +81,10 @@ class Num2Word_AR(lang_EU.Num2Word_EU):
|
||||
outwords[-1] = "،".join(lastwords)
|
||||
return " ".join(outwords)
|
||||
|
||||
|
||||
def to_ordinal_num(self, value):
|
||||
self.verify_ordinal(value)
|
||||
return "%s%s"%(value, self.to_ordinal(value)[-2:])
|
||||
|
||||
|
||||
def to_year(self, val, longval=True):
|
||||
if not (val//100)%10:
|
||||
return self.to_cardinal(val)
|
||||
@@ -105,6 +102,7 @@ to_ord = n2w.to_ordinal
|
||||
to_ordnum = n2w.to_ordinal_num
|
||||
to_year = n2w.to_year
|
||||
|
||||
|
||||
def main():
|
||||
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||
@@ -112,7 +110,7 @@ def main():
|
||||
-21212121211221211111, -2.121212, -1.0000100]:
|
||||
n2w.test(val)
|
||||
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
||||
for val in [1, 120, 1000, 1120, 1800, 1976, 2000, 2010, 2099, 2171]:
|
||||
print(val, "is", n2w.to_currency(val))
|
||||
print(val, "is", n2w.to_year(val))
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
from __future__ import unicode_literals, print_function
|
||||
from .lang_EU import Num2Word_EU
|
||||
|
||||
|
||||
class Num2Word_DE(Num2Word_EU):
|
||||
def set_high_numwords(self, high):
|
||||
max = 3 + 6*len(high)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
from __future__ import division, unicode_literals, print_function
|
||||
from num2words import lang_EU
|
||||
|
||||
|
||||
class Num2Word_DK(lang_EU.Num2Word_EU):
|
||||
def set_high_numwords(self, high):
|
||||
max = 3 + 6*len(high)
|
||||
@@ -138,6 +139,7 @@ to_ord = n2w.to_ordinal
|
||||
to_ordnum = n2w.to_ordinal_num
|
||||
to_year = n2w.to_year
|
||||
|
||||
|
||||
def main():
|
||||
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
from __future__ import division, unicode_literals, print_function
|
||||
from . import lang_EU
|
||||
|
||||
|
||||
class Num2Word_EN(lang_EU.Num2Word_EU):
|
||||
def set_high_numwords(self, high):
|
||||
max = 3 + 3*len(high)
|
||||
@@ -99,6 +100,7 @@ to_ord = n2w.to_ordinal
|
||||
to_ordnum = n2w.to_ordinal_num
|
||||
to_year = n2w.to_year
|
||||
|
||||
|
||||
def main():
|
||||
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||
|
||||
@@ -95,7 +95,7 @@ def pluralize(n, forms):
|
||||
|
||||
|
||||
def int2word(n):
|
||||
if n > 9999: #doesn't yet work for numbers this big
|
||||
if n > 9999: # doesn't yet work for numbers this big
|
||||
raise NotImplementedError()
|
||||
|
||||
if n == 0:
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
from __future__ import unicode_literals, print_function
|
||||
from .lang_EU import Num2Word_EU
|
||||
|
||||
|
||||
class Num2Word_NL(Num2Word_EU):
|
||||
def set_high_numwords(self, high):
|
||||
max = 3 + 6*len(high)
|
||||
@@ -92,10 +93,10 @@ class Num2Word_NL(Num2Word_EU):
|
||||
ntext = "een"
|
||||
|
||||
if ntext.endswith("e"):
|
||||
ntext += "ën"#"n"
|
||||
ntext += "ën" # "n"
|
||||
else:
|
||||
ntext += "en"
|
||||
ntext, ctext = ctext, ntext #+ "en"
|
||||
ntext, ctext = ctext, ntext # + "en"
|
||||
elif cnum >= 10**6:
|
||||
ctext += " "
|
||||
val = cnum + nnum
|
||||
|
||||
@@ -163,7 +163,7 @@ THOUSANDS = {
|
||||
6: (u'trylion', u'tryliony', u'trylionów'), # 10^18
|
||||
7: (u'tryliard', u'tryliardy', u'tryliardów'), # 10^21
|
||||
8: (u'kwadrylion', u'kwadryliony', u'kwadrylionów'), # 10^24
|
||||
9: (u'kwaryliard', u'kwadryliardy', u'kwadryliardów'), #10^27
|
||||
9: (u'kwaryliard', u'kwadryliardy', u'kwadryliardów'), # 10^27
|
||||
10: (u'kwintylion', u'kwintyliony', u'kwintylionów'), # 10^30
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ THOUSANDS = {
|
||||
6: (u'квинтиллион', u'квинтиллиона', u'квинтиллионов'), # 10^18
|
||||
7: (u'секстиллион', u'секстиллиона', u'секстиллионов'), # 10^21
|
||||
8: (u'септиллион', u'септиллиона', u'септиллионов'), # 10^24
|
||||
9: (u'октиллион', u'октиллиона', u'октиллионов'), #10^27
|
||||
9: (u'октиллион', u'октиллиона', u'октиллионов'), # 10^27
|
||||
10: (u'нониллион', u'нониллиона', u'нониллионов'), # 10^30
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ THOUSANDS = {
|
||||
6: (u'квiнтильйон', u'квiнтильйони', u'квiнтильйонiв'), # 10^18
|
||||
7: (u'секстильйон', u'секстильйони', u'секстильйонiв'), # 10^21
|
||||
8: (u'септильйон', u'септильйони', u'септильйонiв'), # 10^24
|
||||
9: (u'октильйон', u'октильйони', u'октильйонiв'), #10^27
|
||||
9: (u'октильйон', u'октильйони', u'октильйонiв'), # 10^27
|
||||
10: (u'нонiльйон', u'нонiльйони', u'нонiльйонiв'), # 10^30
|
||||
}
|
||||
|
||||
@@ -205,7 +205,8 @@ def get_digits(n):
|
||||
|
||||
|
||||
def pluralize(n, forms):
|
||||
#form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2
|
||||
# form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or
|
||||
# n % 100 > 20)) else 2
|
||||
if (n % 100 < 10 or n % 100 > 20):
|
||||
if n % 10 == 1:
|
||||
form = 0
|
||||
@@ -241,7 +242,7 @@ def int2word(n, feminine=True):
|
||||
|
||||
if n2 == 1:
|
||||
words.append(TENS[n1][0])
|
||||
#elif n1 > 0 and not (i > 0 and x == 1):
|
||||
# elif n1 > 0 and not (i > 0 and x == 1):
|
||||
elif n1 > 0:
|
||||
ones = ONES_FEMININE if i == 1 or feminine and i == 0 else ONES
|
||||
words.append(ones[n1][0])
|
||||
|
||||
Reference in New Issue
Block a user