2013-05-28 11:50:48 -04:00
|
|
|
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
|
|
|
|
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
2003-07-17 01:37:11 +00:00
|
|
|
|
2013-05-28 11:50:48 -04:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
# License along with this library; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
|
# MA 02110-1301 USA
|
2003-07-17 01:37:11 +00:00
|
|
|
|
2017-10-30 14:06:46 -04:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
|
|
2013-05-28 11:50:48 -04:00
|
|
|
from . import lang_EU
|
2003-07-17 01:37:11 +00:00
|
|
|
|
2017-10-23 15:58:00 -06:00
|
|
|
|
2013-05-28 11:50:48 -04:00
|
|
|
class Num2Word_EN(lang_EU.Num2Word_EU):
|
2003-07-17 01:37:11 +00:00
|
|
|
def set_high_numwords(self, high):
|
2017-10-23 18:12:36 -06:00
|
|
|
max = 3 + 3 * len(high)
|
2003-07-17 01:37:11 +00:00
|
|
|
for word, n in zip(high, range(max, 3, -3)):
|
2017-10-23 18:12:36 -06:00
|
|
|
self.cards[10 ** n] = word + "illion"
|
2003-07-17 01:37:11 +00:00
|
|
|
|
|
|
|
|
def setup(self):
|
2017-11-09 17:13:01 +02:00
|
|
|
super(Num2Word_EN, self).setup()
|
|
|
|
|
|
2003-07-17 01:37:11 +00:00
|
|
|
self.negword = "minus "
|
|
|
|
|
self.pointword = "point"
|
2005-06-18 16:20:11 +00:00
|
|
|
self.errmsg_nornum = "Only numbers may be converted to words."
|
2003-07-17 01:37:11 +00:00
|
|
|
self.exclude_title = ["and", "point", "minus"]
|
|
|
|
|
|
|
|
|
|
self.mid_numwords = [(1000, "thousand"), (100, "hundred"),
|
|
|
|
|
(90, "ninety"), (80, "eighty"), (70, "seventy"),
|
|
|
|
|
(60, "sixty"), (50, "fifty"), (40, "forty"),
|
|
|
|
|
(30, "thirty")]
|
|
|
|
|
self.low_numwords = ["twenty", "nineteen", "eighteen", "seventeen",
|
|
|
|
|
"sixteen", "fifteen", "fourteen", "thirteen",
|
|
|
|
|
"twelve", "eleven", "ten", "nine", "eight",
|
|
|
|
|
"seven", "six", "five", "four", "three", "two",
|
|
|
|
|
"one", "zero"]
|
2017-10-23 18:12:36 -06:00
|
|
|
self.ords = {"one": "first",
|
|
|
|
|
"two": "second",
|
|
|
|
|
"three": "third",
|
|
|
|
|
"five": "fifth",
|
|
|
|
|
"eight": "eighth",
|
|
|
|
|
"nine": "ninth",
|
|
|
|
|
"twelve": "twelfth"}
|
2003-07-17 01:37:11 +00:00
|
|
|
|
2016-11-22 09:13:18 -05:00
|
|
|
def merge(self, lpair, rpair):
|
|
|
|
|
ltext, lnum = lpair
|
|
|
|
|
rtext, rnum = rpair
|
2005-06-18 16:20:11 +00:00
|
|
|
if lnum == 1 and rnum < 100:
|
2014-03-14 10:25:25 -04:00
|
|
|
return (rtext, rnum)
|
2017-10-23 18:12:36 -06:00
|
|
|
elif 100 > lnum > rnum:
|
|
|
|
|
return ("%s-%s" % (ltext, rtext), lnum + rnum)
|
2005-06-18 16:20:11 +00:00
|
|
|
elif lnum >= 100 > rnum:
|
2017-10-23 18:12:36 -06:00
|
|
|
return ("%s and %s" % (ltext, rtext), lnum + rnum)
|
2005-06-18 16:20:11 +00:00
|
|
|
elif rnum > lnum:
|
2017-10-23 18:12:36 -06:00
|
|
|
return ("%s %s" % (ltext, rtext), lnum * rnum)
|
|
|
|
|
return ("%s, %s" % (ltext, rtext), lnum + rnum)
|
2003-07-17 01:37:11 +00:00
|
|
|
|
|
|
|
|
def to_ordinal(self, value):
|
|
|
|
|
self.verify_ordinal(value)
|
|
|
|
|
outwords = self.to_cardinal(value).split(" ")
|
|
|
|
|
lastwords = outwords[-1].split("-")
|
|
|
|
|
lastword = lastwords[-1].lower()
|
|
|
|
|
try:
|
|
|
|
|
lastword = self.ords[lastword]
|
|
|
|
|
except KeyError:
|
|
|
|
|
if lastword[-1] == "y":
|
2016-11-22 09:13:18 -05:00
|
|
|
lastword = lastword[:-1] + "ie"
|
2003-07-17 01:37:11 +00:00
|
|
|
lastword += "th"
|
2016-11-22 09:13:18 -05:00
|
|
|
lastwords[-1] = self.title(lastword)
|
2003-07-17 01:37:11 +00:00
|
|
|
outwords[-1] = "-".join(lastwords)
|
|
|
|
|
return " ".join(outwords)
|
|
|
|
|
|
|
|
|
|
def to_ordinal_num(self, value):
|
|
|
|
|
self.verify_ordinal(value)
|
2017-10-23 18:12:36 -06:00
|
|
|
return "%s%s" % (value, self.to_ordinal(value)[-2:])
|
2005-06-18 16:20:11 +00:00
|
|
|
|
|
|
|
|
def to_year(self, val, longval=True):
|
2017-10-23 18:12:36 -06:00
|
|
|
if not (val // 100) % 10:
|
2005-06-18 16:20:11 +00:00
|
|
|
return self.to_cardinal(val)
|
|
|
|
|
return self.to_splitnum(val, hightxt="hundred", jointxt="and",
|
|
|
|
|
longval=longval)
|