|
| 1 | +# -*- coding: utf-8 -*-Num2Word_TET |
| 2 | +# Copyright (c) 2003, Taro Ogawa. All Rights Reserved. |
| 3 | +# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. |
| 4 | + |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 2.1 of the License, or (at your option) any later version. |
| 9 | +# This library is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# Lesser General Public License for more details. |
| 13 | +# You should have received a copy of the GNU Lesser General Public |
| 14 | +# License along with this library; if not, write to the Free Software |
| 15 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 16 | +# MA 02110-1301 USA |
| 17 | + |
| 18 | + |
| 19 | +from num2words.currency import parse_currency_parts |
| 20 | + |
| 21 | +from .lang_EU import Num2Word_EU |
| 22 | + |
| 23 | +DOLLAR = ('dolar', 'dolar') |
| 24 | +CENTS = ('sentavu', 'sentavu') |
| 25 | + |
| 26 | + |
| 27 | +class Num2Word_TET(Num2Word_EU): |
| 28 | + |
| 29 | + CURRENCY_FORMS = { |
| 30 | + 'AUD': (DOLLAR, CENTS), |
| 31 | + 'CAD': (DOLLAR, CENTS), |
| 32 | + 'EUR': (('euro', 'euros'), CENTS), |
| 33 | + 'GBP': (('pound sterling', 'pound sterling'), ('pence', 'pence')), |
| 34 | + 'USD': (DOLLAR, CENTS), |
| 35 | + } |
| 36 | + |
| 37 | + GIGA_SUFFIX = None |
| 38 | + MEGA_SUFFIX = "iliaun" |
| 39 | + |
| 40 | + def setup(self): |
| 41 | + super().setup() |
| 42 | + lows = ["kuatr", "tr", "b", "m"] |
| 43 | + self.high_numwords = self.gen_high_numwords([], [], lows) |
| 44 | + self.negword = "menus " |
| 45 | + self.pointword = "vírgula" |
| 46 | + self.exclude_title = ["resin", "vírgula", "menus"] |
| 47 | + self.count = 0 |
| 48 | + |
| 49 | + self.mid_numwords = [ |
| 50 | + (1000, "rihun"), (100, "atus"), (90, "sianulu"), |
| 51 | + (80, "ualunulu"), (70, "hitunulu"), (60, "neenulu"), |
| 52 | + (50, "limanulu"), (40, "haatnulu"), (30, "tolunulu"), |
| 53 | + (20, "ruanulu") |
| 54 | + ] |
| 55 | + self.low_numwords = [ |
| 56 | + "sanulu", |
| 57 | + "sia", "ualu", "hitu", "neen", "lima", "haat", "tolu", "rua", |
| 58 | + "ida", "mamuk" |
| 59 | + ] |
| 60 | + self.hundreds = { |
| 61 | + 1: "atus", |
| 62 | + 2: "atus rua", |
| 63 | + 3: "atus tolu", |
| 64 | + 4: "atus haat", |
| 65 | + 5: "atus lima", |
| 66 | + 6: "atus neen", |
| 67 | + 7: "atus hitu", |
| 68 | + 8: "atus ualu", |
| 69 | + 9: "atus sia", |
| 70 | + } |
| 71 | + |
| 72 | + def merge(self, curr, next): |
| 73 | + ctext, cnum, ntext, nnum = curr + next |
| 74 | + |
| 75 | + if cnum == 1 and nnum < 100: |
| 76 | + return next |
| 77 | + |
| 78 | + if nnum < cnum: |
| 79 | + if nnum < 10: |
| 80 | + value_str = str(cnum + nnum) |
| 81 | + if int(value_str) > 100: |
| 82 | + zero_list = value_str[1:-1] |
| 83 | + all_zero = all(element == '0' for element in zero_list) |
| 84 | + if all_zero: |
| 85 | + if self.count >= 1: |
| 86 | + self.count += 0 |
| 87 | + return ( |
| 88 | + "ho %s %s" % (ctext, ntext), |
| 89 | + cnum + nnum |
| 90 | + ) |
| 91 | + self.count += 1 |
| 92 | + return ("%s %s" % (ctext, ntext), cnum + nnum) |
| 93 | + |
| 94 | + return ("%s resin %s" % (ctext, ntext), cnum + nnum) |
| 95 | + else: |
| 96 | + return ("%s %s" % (ctext, ntext), cnum + nnum) |
| 97 | + |
| 98 | + return (ntext + " " + ctext, cnum * nnum) |
| 99 | + |
| 100 | + def ho_result(self, result, value): |
| 101 | + index = result.find('ho') |
| 102 | + count_ho = result.count('ho') |
| 103 | + |
| 104 | + if index != -1 and count_ho >= 1: |
| 105 | + index_rihun = result.find('rihun') |
| 106 | + value_str = len(str(value)) |
| 107 | + if index_rihun != -1 and value_str > 7: |
| 108 | + result = result.replace("rihun ho", "ho rihun") |
| 109 | + lows = ["kuatr", "tr", "b", "m"] |
| 110 | + MEGA_SUFFIX = "iliaun" |
| 111 | + for low in lows: |
| 112 | + result = result.replace( |
| 113 | + low + MEGA_SUFFIX + " ho", "ho " + low + MEGA_SUFFIX) |
| 114 | + remove_first_ho = result.startswith('ho') |
| 115 | + if remove_first_ho: |
| 116 | + result = result[3:] |
| 117 | + return result |
| 118 | + |
| 119 | + def remove_ho(self, result, value): |
| 120 | + value_str = str(value) |
| 121 | + result = self.ho_result(result, value) |
| 122 | + end_value = value_str[:-4] |
| 123 | + end_true = end_value.endswith('0') |
| 124 | + if end_true is False: |
| 125 | + if value > 100: |
| 126 | + if value_str[-1] != '0' and value_str[-2] == '0': |
| 127 | + result = result.replace("ho", "") |
| 128 | + result = result.replace(" ", " ") |
| 129 | + |
| 130 | + return result |
| 131 | + |
| 132 | + def to_cardinal(self, value): |
| 133 | + result = super().to_cardinal(value) |
| 134 | + |
| 135 | + results = self.remove_ho(result, value) |
| 136 | + return results |
| 137 | + |
| 138 | + def to_ordinal(self, value): |
| 139 | + self.verify_ordinal(value) |
| 140 | + out = "" |
| 141 | + val = self.splitnum(value) |
| 142 | + outs = val |
| 143 | + while len(val) != 1: |
| 144 | + outs = [] |
| 145 | + left, right = val[:2] |
| 146 | + if isinstance(left, tuple) and isinstance(right, tuple): |
| 147 | + outs.append(self.merge(left, right)) |
| 148 | + if val[2:]: |
| 149 | + outs.append(val[2:]) |
| 150 | + else: |
| 151 | + for elem in val: |
| 152 | + if isinstance(elem, list): |
| 153 | + if len(elem) == 1: |
| 154 | + outs.append(elem[0]) |
| 155 | + else: |
| 156 | + outs.append(self.clean(elem)) |
| 157 | + else: |
| 158 | + outs.append(elem) |
| 159 | + val = outs |
| 160 | + |
| 161 | + words, num = outs[0] |
| 162 | + |
| 163 | + words = self.remove_ho(words, value) |
| 164 | + |
| 165 | + if num in [90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 5, 3, 2]: |
| 166 | + words = 'da'+words+'k' |
| 167 | + if num in [6, 4]: |
| 168 | + words = 'da'+words |
| 169 | + if num == 1: |
| 170 | + words = 'dahuluk' |
| 171 | + if num in [900, 800, 700, 500, 300, 200, 100]: |
| 172 | + words = 'dah'+words+'k' |
| 173 | + if num in [600, 400]: |
| 174 | + words = 'dah'+words |
| 175 | + |
| 176 | + words_split = words.split() |
| 177 | + if len(words_split) >= 3 and num < 100: |
| 178 | + first_word = 'da'+words_split[0] |
| 179 | + second_word = " ".join(words_split[1:]) |
| 180 | + if 'haat' in second_word or 'neen' in second_word: |
| 181 | + words = first_word+" "+second_word |
| 182 | + else: |
| 183 | + words = first_word+" "+second_word+'k' |
| 184 | + |
| 185 | + word_first = 'dah'+words_split[0] |
| 186 | + if word_first == 'dahatus' and len(words_split) >= 3: |
| 187 | + word_second = " ".join(words_split[1:]) |
| 188 | + if 'haat' in word_second or 'neen' in word_second: |
| 189 | + words = word_first+" "+word_second |
| 190 | + else: |
| 191 | + words = word_first+" "+word_second+'k' |
| 192 | + |
| 193 | + if len(str(num)) > 3: |
| 194 | + if 'haat' in words_split[-1:] or 'neen' in words_split[-1:]: |
| 195 | + words = 'da'+words |
| 196 | + else: |
| 197 | + words = 'da'+words+'k' |
| 198 | + |
| 199 | + result = self.title(out + words) |
| 200 | + |
| 201 | + return result |
| 202 | + |
| 203 | + def to_ordinal_num(self, value): |
| 204 | + self.verify_ordinal(value) |
| 205 | + return "%sº" % (value) |
| 206 | + |
| 207 | + def to_year(self, val, longval=True): |
| 208 | + if val < 0: |
| 209 | + return self.to_cardinal(abs(val)) + ' antes Kristu' |
| 210 | + return self.to_cardinal(val) |
| 211 | + |
| 212 | + def to_currency(self, val, currency='USD', cents=True): |
| 213 | + """ |
| 214 | + Args: |
| 215 | + val: Numeric value |
| 216 | + currency (str): Currency code |
| 217 | + cents (bool): Verbose cents |
| 218 | + adjective (bool): Prefix currency name with adjective |
| 219 | + Returns: |
| 220 | + str: Formatted string |
| 221 | +
|
| 222 | + """ |
| 223 | + left, right, is_negative = parse_currency_parts(val) |
| 224 | + |
| 225 | + try: |
| 226 | + cr1, cr2 = self.CURRENCY_FORMS[currency] |
| 227 | + |
| 228 | + except KeyError: |
| 229 | + raise NotImplementedError( |
| 230 | + 'Currency code "%s" not implemented for "%s"' % |
| 231 | + (currency, self.__class__.__name__)) |
| 232 | + |
| 233 | + minus_str = "%s " % self.negword.strip() if is_negative else "" |
| 234 | + money_str = self._money_verbose(left, currency) |
| 235 | + cents_str = self._cents_verbose(right, currency) \ |
| 236 | + if cents else self._cents_terse(right, currency) |
| 237 | + |
| 238 | + if right == 0: |
| 239 | + return u'%s%s %s' % ( |
| 240 | + minus_str, |
| 241 | + self.pluralize(left, cr1), |
| 242 | + money_str |
| 243 | + ) |
| 244 | + else: |
| 245 | + return u'%s%s %s %s %s' % ( |
| 246 | + minus_str, |
| 247 | + self.pluralize(left, cr1), |
| 248 | + money_str, |
| 249 | + self.pluralize(right, cr2), |
| 250 | + cents_str |
| 251 | + ) |
0 commit comments