mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 06:42:25 +00:00
add basic farsi support
This commit is contained in:
committed by
Willem Van Onsem
parent
b01c44e33c
commit
9f59c5c894
@@ -86,6 +86,7 @@ Besides the numerical argument, there are two main optional arguments.
|
||||
* ``es_CO`` (Spanish - Colombia)
|
||||
* ``es_VE`` (Spanish - Venezuela)
|
||||
* ``eu`` (EURO)
|
||||
* ``fa`` (Farsi)
|
||||
* ``fi`` (Finnish)
|
||||
* ``fr`` (French)
|
||||
* ``fr_CH`` (French - Switzerland)
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from . import (lang_AR, lang_CZ, lang_DE, lang_DK, lang_EN, lang_EN_IN,
|
||||
lang_ES, lang_ES_CO, lang_ES_NI, lang_ES_VE, lang_FI, lang_FR,
|
||||
lang_ES, lang_ES_CO, lang_ES_NI, lang_ES_VE, lang_FA, lang_FI, lang_FR,
|
||||
lang_FR_BE, lang_FR_CH, lang_FR_DZ, lang_HE, lang_HU, lang_ID,
|
||||
lang_IT, lang_JA, lang_KN, lang_KO, lang_KZ, lang_LT, lang_LV,
|
||||
lang_NL, lang_NO, lang_PL, lang_PT, lang_PT_BR, lang_RO,
|
||||
@@ -30,6 +30,7 @@ CONVERTER_CLASSES = {
|
||||
'cz': lang_CZ.Num2Word_CZ(),
|
||||
'en': lang_EN.Num2Word_EN(),
|
||||
'en_IN': lang_EN_IN.Num2Word_EN_IN(),
|
||||
'fa': lang_FA.Num2Word_FA(),
|
||||
'fr': lang_FR.Num2Word_FR(),
|
||||
'fr_CH': lang_FR_CH.Num2Word_FR_CH(),
|
||||
'fr_BE': lang_FR_BE.Num2Word_FR_BE(),
|
||||
|
||||
165
num2words/lang_FA.py
Normal file
165
num2words/lang_FA.py
Normal file
@@ -0,0 +1,165 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
# Copyright (c) 2018, Abdullah Alhazmy, Alhazmy13. All Rights Reserved.
|
||||
# Copyright (c) 2020, Hamidreza Kalbasi. All Rights Reserved.
|
||||
|
||||
# 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
|
||||
|
||||
import re
|
||||
from decimal import Decimal
|
||||
from math import floor
|
||||
|
||||
farsiOnes = [
|
||||
"", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت",
|
||||
"نه",
|
||||
"ده",
|
||||
"یازده",
|
||||
"دوازده",
|
||||
"سیزده",
|
||||
"چهارده",
|
||||
"پونزده",
|
||||
"شونزده",
|
||||
"هیفده",
|
||||
"هیجده",
|
||||
"نوزده",
|
||||
]
|
||||
|
||||
farsiTens = [
|
||||
"",
|
||||
"ده",
|
||||
"بیست",
|
||||
"سی",
|
||||
"چهل",
|
||||
"پنجاه",
|
||||
"شصت",
|
||||
"هفتاد",
|
||||
"هشتاد",
|
||||
"نود",
|
||||
]
|
||||
|
||||
farsiHundreds = [
|
||||
"",
|
||||
"صد",
|
||||
"دویست",
|
||||
"سیصد",
|
||||
"چهارصد",
|
||||
"پانصد",
|
||||
"ششصد",
|
||||
"هفتصد",
|
||||
"هشتصد",
|
||||
"نهصد",
|
||||
]
|
||||
|
||||
farsiBig = [
|
||||
'',
|
||||
' هزار',
|
||||
' میلیون',
|
||||
" میلیارد",
|
||||
' تریلیون',
|
||||
" تریلیارد",
|
||||
]
|
||||
|
||||
farsiFrac = ["", "دهم", "صدم"]
|
||||
farsiFracBig = ["", "هزارم", "میلیونیم", "میلیاردیم"]
|
||||
|
||||
farsiSeperator = ' و '
|
||||
|
||||
class Num2Word_FA(object):
|
||||
errmsg_too_big = "Too large"
|
||||
max_num = 10 ** 36
|
||||
|
||||
def __init__(self):
|
||||
self.number = 0
|
||||
|
||||
def float2tuple(self, value):
|
||||
pre = int(value)
|
||||
|
||||
# Simple way of finding decimal places to update the precision
|
||||
self.precision = abs(Decimal(str(value)).as_tuple().exponent)
|
||||
|
||||
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
|
||||
post = int(round(post))
|
||||
else:
|
||||
post = int(math.floor(post))
|
||||
|
||||
return pre, post, self.precision
|
||||
|
||||
|
||||
def cardinal3(self, number):
|
||||
if (number < 19):
|
||||
return farsiOnes[number]
|
||||
if (number < 100):
|
||||
x, y = divmod(number, 10)
|
||||
if y == 0:
|
||||
return farsiTens[x]
|
||||
return farsiTens[x] + farsiSeperator + farsiOnes[y]
|
||||
x, y = divmod(number, 100)
|
||||
if y == 0:
|
||||
return farsiHundreds[x]
|
||||
return farsiHundreds[x] + farsiSeperator + self.cardinal3(y)
|
||||
|
||||
def cardinalPos(self, number):
|
||||
x = number
|
||||
res = ''
|
||||
for b in farsiBig:
|
||||
x, y = divmod(x, 1000)
|
||||
if (y == 0):
|
||||
continue
|
||||
yx = self.cardinal3(y) + b
|
||||
if (res == ''):
|
||||
res = yx
|
||||
else:
|
||||
res = yx + farsiSeperator
|
||||
return res
|
||||
|
||||
def fractional(self, number, l):
|
||||
if (number == 5):
|
||||
return "نیم"
|
||||
x = self.cardinalPos(number)
|
||||
ld3, lm3 = divmod(l, 3)
|
||||
ltext = (farsiFrac[lm3] + " " + farsiFracBig[ld3]).strip()
|
||||
return x + " " + ltext
|
||||
|
||||
def to_currency(self, value):
|
||||
return self.to_cardinal(value) + " تومان"
|
||||
|
||||
def to_ordinal(self, number):
|
||||
r = self.to_cardinal(number)
|
||||
if (r[-1] == 'ه' and r[-2] == 'س'):
|
||||
return r[:-1] + 'وم'
|
||||
return r + 'م'
|
||||
|
||||
def to_year(self, value):
|
||||
return self.to_cardinal(value)
|
||||
|
||||
def to_ordinal_num(self, value):
|
||||
return str(value)+"م"
|
||||
|
||||
def to_cardinal(self, number):
|
||||
if number < 0:
|
||||
return "منفی " + self.to_cardinal(-number)
|
||||
if (number == 0):
|
||||
return "صفر"
|
||||
x, y, l = self.float2tuple(number)
|
||||
if y == 0:
|
||||
return self.cardinalPos(x)
|
||||
if x == 0:
|
||||
return self.fractional(y, l)
|
||||
return self.cardinalPos(x) + farsiSeperator + self.fractional(y, l)
|
||||
85
tests/test_fa.py
Normal file
85
tests/test_fa.py
Normal file
@@ -0,0 +1,85 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
# Copyright (c) 2020, Hamidreza Kalbasi. All Rights Reserved.
|
||||
|
||||
# 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
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from num2words import num2words
|
||||
|
||||
|
||||
class Num2WordsFATest(TestCase):
|
||||
def test_and_join_199(self):
|
||||
self.assertEqual(num2words(199, lang='fa'), "صد و نود و نه")
|
||||
|
||||
def test_ordinal(self):
|
||||
self.assertEqual(
|
||||
num2words(0, lang='fa', to='ordinal'),
|
||||
'صفرم'
|
||||
)
|
||||
self.assertEqual(
|
||||
num2words(1, lang='fa', to='ordinal'),
|
||||
'یکم'
|
||||
)
|
||||
self.assertEqual(
|
||||
num2words(13, lang='fa', to='ordinal'),
|
||||
'سیزدهم'
|
||||
)
|
||||
self.assertEqual(
|
||||
num2words(23, lang='fa', to='ordinal'),
|
||||
'بیست و سوم'
|
||||
)
|
||||
self.assertEqual(
|
||||
num2words(12, lang='fa', to='ordinal'),
|
||||
'دوازدهم'
|
||||
)
|
||||
self.assertEqual(
|
||||
num2words(113, lang='fa', to='ordinal'),
|
||||
'صد و سیزدهم'
|
||||
)
|
||||
self.assertEqual(
|
||||
num2words(103, lang='fa', to='ordinal'),
|
||||
'صد و سوم'
|
||||
)
|
||||
|
||||
def test_cardinal(self):
|
||||
self.assertEqual(num2words(130000, lang='fa'), "صد و سی هزار")
|
||||
self.assertEqual(num2words(242, lang='fa'), "دویست و چهل و دو")
|
||||
|
||||
|
||||
|
||||
def test_ordinal_num(self):
|
||||
self.assertEqual(num2words(10, lang='fa', to='ordinal_num'), '10م')
|
||||
self.assertEqual(num2words(21, lang='fa', to='ordinal_num'), '21م')
|
||||
self.assertEqual(num2words(102, lang='fa', to='ordinal_num'), '102م')
|
||||
self.assertEqual(num2words(73, lang='fa', to='ordinal_num'), '73م')
|
||||
|
||||
def test_cardinal_for_float_number(self):
|
||||
self.assertEqual(num2words(12.5, lang='fa'), "دوازده و نیم")
|
||||
self.assertEqual(num2words(0.75, lang='fa'), "هفتاد و پنج صدم")
|
||||
self.assertEqual(num2words(12.51, lang='fa'), "دوازده و پنجاه و یک صدم")
|
||||
self.assertEqual(num2words(12.53, lang='fa'), "دوازده و پنجاه و سه صدم")
|
||||
self.assertEqual(num2words(12.59, lang='fa'), "دوازده و پنجاه و نه صدم")
|
||||
self.assertEqual(num2words(0.000001, lang='fa'), "یک میلیونیم")
|
||||
|
||||
def test_overflow(self):
|
||||
with self.assertRaises(OverflowError):
|
||||
num2words("1000000000000000000000000000000000000000000000000000000"
|
||||
"0000000000000000000000000000000000000000000000000000000"
|
||||
"0000000000000000000000000000000000000000000000000000000"
|
||||
"0000000000000000000000000000000000000000000000000000000"
|
||||
"0000000000000000000000000000000000000000000000000000000"
|
||||
"00000000000000000000000000000000")
|
||||
Reference in New Issue
Block a user