Skip to content

Commit

Permalink
change numwords to number_to_words; plequal to compare; plnounequal t…
Browse files Browse the repository at this point in the history
…o compare_nouns; plverbequal to compare_verbs; pladjequal to compare_adjs
  • Loading branch information
pwdyson committed Aug 5, 2010
1 parent 24c3939 commit 00f0b1e
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 129 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ ver 0.2.0
pladj -> plural_adjective
sinoun -> singular_noun
prespart -> present_participle
numwords -> number_to_words
plequal -> compare
plnounequal -> compare_nouns
plverbequal -> compare_verbs
pladjequal -> compare_adjs


* change classical() to only accept keyword args: only one way to do it

Expand Down
114 changes: 69 additions & 45 deletions inflect.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@
methods:
classical inflect
plural plural_noun plural_verb plural_adj singular_noun no num a an
plequal plnounequal plverbequal pladjequal
compare compare_nouns compare_verbs compare_adjs
prespart
ordinal
numwords
number_to_words
wordlist
defnoun defverb defadj defa defan
INFLECTIONS: classical inflect
plural plural_noun plural_verb plural_adj singular_noun plequal
plural plural_noun plural_verb plural_adj singular_noun compare
no num a an prespart
PLURALS: classical inflect
plural plural_noun plural_verb plural_adj singular_noun no num
plequal plnounequal plverbequal pladjequal
compare compare_nouns compare_verbs compare_adjs
COMPARISONS: classical
plequal plnounequal plverbequal pladjequal
compare compare_nouns compare_verbs compare_adjs
ARTICLES: classical inflect num a an
NUMERICAL: ordinal numwords
NUMERICAL: ordinal number_to_words
USER_DEFINED: defnoun defverb defadj defa defan
Expand All @@ -67,6 +67,7 @@
from re import sub as resub
from os.path import dirname, isfile, expanduser
from os.path import join as pathjoin
from gnome_sudoku.pausable import MethodWrapper

class UnknownClassicalModeError(Exception): pass
class BadNumValueError(Exception): pass
Expand Down Expand Up @@ -1191,6 +1192,25 @@ def __init__(self):
self.si_sb_user_defined = []
self.A_a_user_defined = []
self.thegender = 'neuter'

deprecated_methods = dict(pl='plural',
plnoun='plural_noun',
plverb='plural_verb',
pladj='plural_adj',
sinoun='single_noun',
prespart='present_participle',
numwords='number_to_words',
plequal='compare',
plnounequal='compare_nouns',
plverbequal='compare_verbs',
pladjequal='compare_adjs',
)
def __getattr__(self, meth):
if meth in self.deprecated_methods:
print3('%s() deprecated, use %s()' % (meth, self.deprecated_methods[meth]))
raise DeprecationWarning
raise AttributeError


def defnoun(self, singular, plural):
'''
Expand Down Expand Up @@ -1422,10 +1442,10 @@ def ordinalmo(self, matchobject):

def numwordsmo(self, matchobject):
'''
numwords but take a matchobject
number_to_words but take a matchobject
use group 1
'''
return self.numwords(matchobject.group(1))
return self.number_to_words(matchobject.group(1))

def prespartmo(self, matchobject):
'''
Expand All @@ -1445,7 +1465,7 @@ def inflect(self, text):
'The plural of cat is cats'
can use plural, plural_noun, plural_verb, plural_adj, singular_noun, a, an, no, ordinal,
numwords and prespart
number_to_words and prespart
'''
save_persistent_count = self.persistent_count
Expand Down Expand Up @@ -1526,37 +1546,41 @@ def partition_word(self, text):
except AttributeError: # empty string
return '', '', ''


def pl(self, *args, **kwds):
print 'pl() deprecated, use plural()'
#raise DeprecationWarning
return self.plural(*args, **kwds)

def plnoun(self, *args, **kwds):
print 'plnoun() deprecated, use plural_noun()'
raise DeprecationWarning
return self.plural_noun(*args, **kwds)

def plverb(self, *args, **kwds):
print 'plverb() deprecated, use plural_verb()'
raise DeprecationWarning
return self.plural_verb(*args, **kwds)

def pladj(self, *args, **kwds):
print 'pladj() deprecated, use plural_adj()'
raise DeprecationWarning
return self.plural_adj(*args, **kwds)

def sinoun(self, *args, **kwds):
print 'sinoun() deprecated, use singular_noun()'
raise DeprecationWarning
return self.singular_noun(*args, **kwds)

def prespart(self, *args, **kwds):
print 'prespart() deprecated, use present_paarticiple()'
raise DeprecationWarning
return self.present_participle(*args, **kwds)


# def pl(self, *args, **kwds):
# print 'pl() deprecated, use plural()'
# raise DeprecationWarning
# return self.plural(*args, **kwds)
#
# def plnoun(self, *args, **kwds):
# print 'plnoun() deprecated, use plural_noun()'
# raise DeprecationWarning
# return self.plural_noun(*args, **kwds)
#
# def plverb(self, *args, **kwds):
# print 'plverb() deprecated, use plural_verb()'
# raise DeprecationWarning
# return self.plural_verb(*args, **kwds)
#
# def pladj(self, *args, **kwds):
# print 'pladj() deprecated, use plural_adj()'
# raise DeprecationWarning
# return self.plural_adj(*args, **kwds)
#
# def sinoun(self, *args, **kwds):
# print 'sinoun() deprecated, use singular_noun()'
# raise DeprecationWarning
# return self.singular_noun(*args, **kwds)
#
# def prespart(self, *args, **kwds):
# print 'prespart() deprecated, use present_participle()'
# raise DeprecationWarning
# return self.present_participle(*args, **kwds)
#
# def numwords(self, *args, **kwds):
# print 'numwords() deprecated, use number_to_words()'
# raise DeprecationWarning
# return self.number_to_words(*args, **kwds)

def plural(self, text, count=None):
'''
Expand Down Expand Up @@ -1632,7 +1656,7 @@ def plural_adj(self, text, count=None):
or word)
return "%s%s%s" % (pre, plural, post)

def plequal(self, word1, word2):
def compare(self, word1, word2):
'''
compare word1 and word2 for equality regardless of plurality
Expand All @@ -1649,7 +1673,7 @@ def plequal(self, word1, word2):
self._plequal(word1, word2, self.plural_verb) or
self._plequal(word1, word2, self.plural_adj))

def plnounequal(self, word1, word2):
def compare_nouns(self, word1, word2):
'''
compare word1 and word2 for equality regardless of plurality
word1 and word2 are to be treated as nouns
Expand All @@ -1664,7 +1688,7 @@ def plnounequal(self, word1, word2):
'''
return self._plequal(word1, word2, self.plural_noun)

def plverbequal(self, word1, word2):
def compare_verbs(self, word1, word2):
'''
compare word1 and word2 for equality regardless of plurality
word1 and word2 are to be treated as verbs
Expand All @@ -1679,7 +1703,7 @@ def plverbequal(self, word1, word2):
'''
return self._plequal(word1, word2, self.plural_verb)

def pladjequal(self, word1, word2):
def compare_adjs(self, word1, word2):
'''
compare word1 and word2 for equality regardless of plurality
word1 and word2 are to be treated as adjectives
Expand Down Expand Up @@ -2980,7 +3004,7 @@ def spacefn(self, mo):
'''
return ' '

def numwords(self, num, wantlist=False,
def number_to_words(self, num, wantlist=False,
group=0, comma=',', andword='and',
zero='zero', one='one', decimal='point',
threshold=None):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inflections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .. import inflect

def is_eq(p, a, b):
return (p.plequal(a, b) or
return (p.compare(a, b) or
p.plnounequal(a, b) or
p.plverbequal(a, b) or
p.pladjequal(a, b) )
Expand Down
66 changes: 33 additions & 33 deletions tests/test_numwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@ def test_loop():

for thresh in range(21):
for n in range(21):
threshed = p.numwords(n, threshold=thresh)
numwords = p.numwords(n)
threshed = p.number_to_words(n, threshold=thresh)
numwords = p.number_to_words(n)

if (n <= thresh):
eq_ ( numwords, threshed, msg="Wordified %s (<= %s)" % (n, thresh))
else:
#$threshed =~ s/\D//gxms;
eq_ ( threshed, str(n), msg="p.numwords(%s, thresold=%s) == %s != %s" % (
eq_ ( threshed, str(n), msg="p.number_to_words(%s, thresold=%s) == %s != %s" % (
n, thresh, threshed, str(n)))

def test_lines():
p = inflect.engine()
eq_( p.numwords( 999, threshold=500) , '999', msg= ' 999 -> 999')
eq_( p.numwords( 1000, threshold=500) , '1,000', msg= '1000 -> 1,000')
eq_( p.numwords( 10000, threshold=500) , '10,000', msg= '10000 -> 10,000')
eq_( p.numwords( 100000, threshold=500) , '100,000', msg= '100000 -> 100,000')
eq_( p.numwords(1000000, threshold=500) , '1,000,000', msg= '1000000 -> 1,000,000')
eq_( p.number_to_words( 999, threshold=500) , '999', msg= ' 999 -> 999')
eq_( p.number_to_words( 1000, threshold=500) , '1,000', msg= '1000 -> 1,000')
eq_( p.number_to_words( 10000, threshold=500) , '10,000', msg= '10000 -> 10,000')
eq_( p.number_to_words( 100000, threshold=500) , '100,000', msg= '100000 -> 100,000')
eq_( p.number_to_words(1000000, threshold=500) , '1,000,000', msg= '1000000 -> 1,000,000')

eq_( p.numwords( 999.3, threshold=500) , '999.3', msg= ' 999.3 -> 999.3')
eq_( p.numwords( 1000.3, threshold=500) , '1,000.3', msg= '1000.3 -> 1,000.3')
eq_( p.numwords( 10000.3, threshold=500) , '10,000.3', msg= '10000.3 -> 10,000.3')
eq_( p.numwords( 100000.3, threshold=500) , '100,000.3', msg= '100000.3 -> 100,000.3')
eq_( p.numwords(1000000.3, threshold=500) , '1,000,000.3', msg= '1000000.3 -> 1,000,000.3')
eq_( p.number_to_words( 999.3, threshold=500) , '999.3', msg= ' 999.3 -> 999.3')
eq_( p.number_to_words( 1000.3, threshold=500) , '1,000.3', msg= '1000.3 -> 1,000.3')
eq_( p.number_to_words( 10000.3, threshold=500) , '10,000.3', msg= '10000.3 -> 10,000.3')
eq_( p.number_to_words( 100000.3, threshold=500) , '100,000.3', msg= '100000.3 -> 100,000.3')
eq_( p.number_to_words(1000000.3, threshold=500) , '1,000,000.3', msg= '1000000.3 -> 1,000,000.3')

eq_( p.numwords( 999, threshold=500, comma=0) , '999', msg= ' 999 -> 999')
eq_( p.numwords( 1000, threshold=500, comma=0) , '1000', msg= '1000 -> 1000')
eq_( p.numwords( 10000, threshold=500, comma=0) , '10000', msg= '10000 -> 10000')
eq_( p.numwords( 100000, threshold=500, comma=0) , '100000', msg= '100000 -> 100000')
eq_( p.numwords(1000000, threshold=500, comma=0) , '1000000', msg= '1000000 -> 1000000')
eq_( p.number_to_words( 999, threshold=500, comma=0) , '999', msg= ' 999 -> 999')
eq_( p.number_to_words( 1000, threshold=500, comma=0) , '1000', msg= '1000 -> 1000')
eq_( p.number_to_words( 10000, threshold=500, comma=0) , '10000', msg= '10000 -> 10000')
eq_( p.number_to_words( 100000, threshold=500, comma=0) , '100000', msg= '100000 -> 100000')
eq_( p.number_to_words(1000000, threshold=500, comma=0) , '1000000', msg= '1000000 -> 1000000')

eq_( p.numwords( 999.3, threshold=500, comma=0) , '999.3', msg= ' 999.3 -> 999.3')
eq_( p.numwords( 1000.3, threshold=500, comma=0) , '1000.3', msg= '1000.3 -> 1000.3')
eq_( p.numwords( 10000.3, threshold=500, comma=0) , '10000.3', msg= '10000.3 -> 10000.3')
eq_( p.numwords( 100000.3, threshold=500, comma=0) , '100000.3', msg= '100000.3 -> 100000.3')
eq_( p.numwords(1000000.3, threshold=500, comma=0) , '1000000.3', msg= '1000000.3 -> 1000000.3')
eq_( p.number_to_words( 999.3, threshold=500, comma=0) , '999.3', msg= ' 999.3 -> 999.3')
eq_( p.number_to_words( 1000.3, threshold=500, comma=0) , '1000.3', msg= '1000.3 -> 1000.3')
eq_( p.number_to_words( 10000.3, threshold=500, comma=0) , '10000.3', msg= '10000.3 -> 10000.3')
eq_( p.number_to_words( 100000.3, threshold=500, comma=0) , '100000.3', msg= '100000.3 -> 100000.3')
eq_( p.number_to_words(1000000.3, threshold=500, comma=0) , '1000000.3', msg= '1000000.3 -> 1000000.3')


def test_array():
Expand Down Expand Up @@ -454,22 +454,22 @@ def test_array():
yield go, p, i

def go(p, i):
eq_ ( p.numwords(i[0]) , i[1], msg="numwords(%s) == %s != %s" % (
eq_ ( p.number_to_words(i[0]) , i[1], msg="number_to_words(%s) == %s != %s" % (
i[0],
p.numwords(i[0]),
p.number_to_words(i[0]),
i[1]) )
eq_ ( p.numwords(i[0], group=1) , i[2] )
eq_ ( p.numwords(i[0], group=2) , i[3] )
eq_ ( p.numwords(i[0], group=3) , i[4] )
eq_ ( p.number_to_words(i[0], group=1) , i[2] )
eq_ ( p.number_to_words(i[0], group=2) , i[3] )
eq_ ( p.number_to_words(i[0], group=3) , i[4] )
if len(i) > 5:
eq_ ( p.numwords(p.ordinal(i[0])) , i[5], msg="numwords(ordinal(%s)) == %s != %s" % (
i[0], p.numwords(p.ordinal(i[0])),
eq_ ( p.number_to_words(p.ordinal(i[0])) , i[5], msg="number_to_words(ordinal(%s)) == %s != %s" % (
i[0], p.number_to_words(p.ordinal(i[0])),
i[5]))
if len(i) > 6:
eq_ ( p.ordinal(p.numwords(i[0])) , i[6] )
eq_ ( p.ordinal(p.number_to_words(i[0])) , i[6] )
else:
if len(i) > 5:
eq_ ( p.ordinal(p.numwords(i[0])) , i[5] )
eq_ ( p.ordinal(p.number_to_words(i[0])) , i[5] )

#eq_ !eval { p.numwords(42, and=>); 1; };
#eq_ !eval { p.number_to_words(42, and=>); 1; };
#eq_ $@ =~ 'odd number of';
Loading

0 comments on commit 00f0b1e

Please sign in to comment.