Big unit reorganisation

* Renamed pynum2word to num2word
* Renamed num2word_* to lang_*
* Re-organised all unit headers
* Made imports relative
This commit is contained in:
Virgil Dupras
2013-05-28 11:50:48 -04:00
parent 5479aa59c6
commit 6e2b0eeda2
15 changed files with 232 additions and 356 deletions

46
num2words/__init__.py Normal file
View File

@@ -0,0 +1,46 @@
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. 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 __future__ import unicode_literals
from . import lang_EN
from . import lang_EN_GB
from . import lang_FR
from . import lang_DE
from . import lang_ES
from . import lang_LT
CONVERTER_CLASSES = {
'en': lang_EN.Num2Word_EN(),
'en_GB': lang_EN_GB.Num2Word_EN_GB(),
'fr': lang_FR.Num2Word_FR(),
'de': lang_DE.Num2Word_DE(),
'es': lang_ES.Num2Word_ES(),
'lt': lang_LT.Num2Word_LT(),
}
def num2words(number, ordinal=False, lang='en'):
# We try the full language first
if lang not in CONVERTER_CLASSES:
# ... and then try only the first 2 letters
lang = lang[:2]
if lang not in CONVERTER_CLASSES:
raise NotImplementedError()
converter = CONVERTER_CLASSES[lang]
if ordinal:
return converter.to_ordinal(number)
else:
return converter.to_cardinal(number)

View File

@@ -1,25 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_base.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Version: 1.0
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Licence: # Lesser General Public License for more details.
This module is distributed under the Lesser General Public Licence. # You should have received a copy of the GNU Lesser General Public
http://www.opensource.org/licenses/lgpl-license.php # License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
History: # MA 02110-1301 USA
1.1: add to_splitnum() and inflect()
add to_year() and to_currency() stubs
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from orderedmapping import OrderedMapping from .orderedmapping import OrderedMapping
class Num2Word_Base(object): class Num2Word_Base(object):

View File

@@ -1,36 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_DE.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_base.py
Version: 0.4
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # 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
Copyright:
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
- http://german4u2know.tripod.com/nouns/10.html
- http://www.uni-bonn.de/~manfear/large.php
Usage:
from num2word_DE import to_card, to_ord, to_ordnum
to_card(1234567890)
to_ord(1234567890)
to_ordnum(12)
History
0.4: Use high ascii characters instead of low ascii approximations
add to_currency() and to_year()
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_EU import Num2Word_EU from .lang_EU import Num2Word_EU
#//TODO: Use German error messages #//TODO: Use German error messages
class Num2Word_DE(Num2Word_EU): class Num2Word_DE(Num2Word_EU):

View File

@@ -1,46 +1,23 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_EN.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_EU.py
Version: 1.2
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
http://www.uni-bonn.de/~manfear/large.php
Usage:
from num2word_EN import n2w, to_card, to_ord, to_ordnum
to_card(1234567890)
n2w.is_title = True
to_card(1234567890)
to_ord(1234567890)
to_ordnum(1234567890)
to_year(1976)
to_currency(dollars*100 + cents, longval=False)
to_currency((dollars, cents))
History:
1.2: to_ordinal_num() made shorter and simpler (but slower)
strings in merge() now interpolated
to_year() and to_currency() added
1.1: to_ordinal_num() fixed for 11,12,13
'''
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import num2word_EU from . import lang_EU
class Num2Word_EN(lang_EU.Num2Word_EU):
class Num2Word_EN(num2word_EU.Num2Word_EU):
def set_high_numwords(self, high): def set_high_numwords(self, high):
max = 3 + 3*len(high) max = 3 + 3*len(high)
for word, n in zip(high, range(max, 3, -3)): for word, n in zip(high, range(max, 3, -3)):

View File

@@ -1,38 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_EN_EUR.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_EN.py
Version: 1.0
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
http://www.uni-bonn.de/~manfear/large.php
Usage:
from num2word_EN import n2w, to_card, to_ord, to_ordnum
to_card(1234567890)
n2w.is_title = True
to_card(1234567890)
to_ord(1234567890)
to_ordnum(1234567890)
to_year(1976)
to_currency(euros*100 + cents)
to_currency((euros,cents))
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_EN import Num2Word_EN from .lang_EN import Num2Word_EN
class Num2Word_EN_EUR(Num2Word_EN): class Num2Word_EN_EUR(Num2Word_EN):
def to_currency(self, val, longval=True, cents=True, jointxt="and"): def to_currency(self, val, longval=True, cents=True, jointxt="and"):

View File

@@ -1,39 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_EN_GB.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_EN.py
Version: 1.0
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
http://www.uni-bonn.de/~manfear/large.php
Usage:
from num2word_EN import n2w, to_card, to_ord, to_ordnum
to_card(1234567890)
n2w.is_title = True
to_card(1234567890)
to_ord(1234567890)
to_ordnum(1234567890)
to_year(1976)
to_currency(pounds*100 + pence)
to_currency((pounds,pence))
History:
1.0: Split from num2word_EN with the addition of to_currency()
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_EN import Num2Word_EN from .lang_EN import Num2Word_EN
class Num2Word_EN_GB(Num2Word_EN): class Num2Word_EN_GB(Num2Word_EN):

View File

@@ -1,33 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_EN_GB_old.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_EN_GB_old.py
Version: 0.3
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Usage:
from num2word_EN_old import to_card, to_ord, to_ordnum
to_card(1234567890)
to_ord(1234567890)
to_ordnum(12)
History:
0.3: Rename from num2word_EN_old
Todo:
Currency (pounds/shillings/pence)
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_EN_GB import Num2Word_EN_GB from .lang_EN_GB import Num2Word_EN_GB
class Num2Word_EN_GB_old(Num2Word_EN_GB): class Num2Word_EN_GB_old(Num2Word_EN_GB):
def base_setup(self): def base_setup(self):

View File

@@ -1,35 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_ES.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_EU.py
Version: 0.3
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
http://www.smartphrase.com/Spanish/sp_numbers_voc.shtml
Usage:
from num2word_ES import to_card, to_ord, to_ordnum
to_card(1234567890)
to_ord(1234567890)
to_ordnum(12)
History:
0.3: Use high ascii characters instead of low ascii approximations
String interpolation where it makes things clearer
add to_currency()
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_EU import Num2Word_EU from .lang_EU import Num2Word_EU
#//TODO: correct orthographics #//TODO: correct orthographics
#//TODO: error messages #//TODO: error messages

View File

@@ -1,27 +1,21 @@
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_EU.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_base.py
Version: 1.1
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
http://www.uni-bonn.de/~manfear/large.php
History:
1.1: add to_currency()
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_base import Num2Word_Base from .base import Num2Word_Base
class Num2Word_EU(Num2Word_Base): class Num2Word_EU(Num2Word_Base):
def set_high_numwords(self, high): def set_high_numwords(self, high):

View File

@@ -1,38 +1,22 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
''' # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Module: num2word_FR.py # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Requires: num2word_EU.py
Version: 0.5
Author: # This library is free software; you can redistribute it and/or
Taro Ogawa (tso@users.sourceforge.org) # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
Copyright: # version 2.1 of the License, or (at your option) any later version.
Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # This library is distributed in the hope that it will be useful,
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # 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
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Data from:
http://www.ouc.bc.ca/mola/fr/handouts/numbers.doc
http://www.realfrench.net/units/Interunit_63.html
http://www.sover.net/~daxtell/france/Euro/euro.htm
Usage:
from num2word_FR import to_card, to_ord, to_ordnum
to_card(1234567890)
to_ord(1234567890)
to_ordnum(12)
History:
0.5: Use high ascii characters instead of low ascii approximations
String interpolation where it makes things clearer
to_currency() added [to_year works by default]
'''
from __future__ import unicode_literals from __future__ import unicode_literals
from num2word_EU import Num2Word_EU from .lang_EU import Num2Word_EU
#//TODO: error messages in French #//TODO: error messages in French
#//TODO: ords #//TODO: ords

View File

@@ -1,4 +1,19 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. 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
u""" u"""
>>> from textwrap import fill >>> from textwrap import fill

View File

@@ -0,0 +1,35 @@
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. 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
class OrderedMapping(dict):
def __init__(self, *pairs):
self.order = []
for key, val in pairs:
self[key] = val
def __setitem__(self, key, val):
if key not in self:
self.order.append(key)
super(OrderedMapping, self).__setitem__(key, val)
def __iter__(self):
for item in self.order:
yield item
def __repr__(self):
out = ["%s: %s"%(repr(item), repr(self[item])) for item in self]
out = ", ".join(out)
return "{%s}"%out

View File

View File

@@ -1,60 +0,0 @@
'''
Module: num2word.py
Requires: num2word_*.py
Version: 0.2
Author:
Taro Ogawa (tso@users.sourceforge.org)
Copyright:
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Usage:
from num2word import to_card, to_ord, to_ordnum
to_card(1234567890)
to_ord(1234567890)
to_ordnum(12)
Notes:
The module is a wrapper for language-specific modules. It imports the
appropriate modules as defined by locale settings. If unable to
load an appropriate module, an ImportError is raised.
History:
0.2: n2w, to_card, to_ord, to_ordnum now imported correctly
'''
from __future__ import unicode_literals
import num2word_EN
import num2word_EN_GB
import num2word_FR
import num2word_DE
import num2word_ES
import num2word_LT
CONVERTER_CLASSES = {
'en': num2word_EN.Num2Word_EN(),
'en_GB': num2word_EN_GB.Num2Word_EN_GB(),
'fr': num2word_FR.Num2Word_FR(),
'de': num2word_DE.Num2Word_DE(),
'es': num2word_ES.Num2Word_ES(),
'lt': num2word_LT.Num2Word_LT(),
}
def num2words(number, ordinal=False, lang='en'):
# We try the full language first
if lang not in CONVERTER_CLASSES:
# ... and then try only the first 2 letters
lang = lang[:2]
if lang not in CONVERTER_CLASSES:
raise NotImplementedError()
converter = CONVERTER_CLASSES[lang]
if ordinal:
return converter.to_ordinal(number)
else:
return converter.to_cardinal(number)

View File

@@ -1,35 +0,0 @@
'''
Module: orderedmapping.py
Version: 1.0
Author:
Taro Ogawa (tso@users.sourceforge.org)
Copyright:
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
'''
from __future__ import generators
class OrderedMapping(dict):
def __init__(self, *pairs):
self.order = []
for key, val in pairs:
self[key] = val
def __setitem__(self, key, val):
if key not in self:
self.order.append(key)
super(OrderedMapping, self).__setitem__(key, val)
def __iter__(self):
for item in self.order:
yield item
def __repr__(self):
out = ["%s: %s"%(repr(item), repr(self[item])) for item in self]
out = ", ".join(out)
return "{%s}"%out