Fixed a bug

This commit is contained in:
Vincent Foley 2004-01-28 21:42:46 +00:00
parent 4e5957d8d2
commit 7bdf8210cb

View File

@ -319,6 +319,8 @@ def matchCase(s1, s2):
L[i] = L[i].upper() L[i] = L[i].upper()
return ''.join(L) return ''.join(L)
consonants = 'bcdfghjklmnpqrstvwxz'
_pluralizeRegex = re.compile('[%s]y$' % consonants)
def pluralize(s, i=2): def pluralize(s, i=2):
"""Returns the plural of s based on its number i. Put any exceptions to """Returns the plural of s based on its number i. Put any exceptions to
the general English rule of appending 's' in the plurals dictionary. the general English rule of appending 's' in the plurals dictionary.
@ -327,24 +329,31 @@ def pluralize(s, i=2):
return s return s
else: else:
lowered = s.lower() lowered = s.lower()
# Exception dictionary
if lowered in plurals: if lowered in plurals:
return matchCase(s, plurals[lowered]) return matchCase(s, plurals[lowered])
elif any(lowered.endswith, ['ch', 'ss']): # Words ending with 'ch', 'sh' or 'ss' such as 'punch(es)', 'fish(es)
return matchCase(s, s+'es') # and miss(es)
elif lowered.endswith('y'): elif any(lowered.endswith, ['ch', 'sh', 'ss']):
return matchCase(s, s[:-1]+'ies') return matchCase(s, s+'es')
# Words ending with a consonant followed by a 'y' such as
# 'try (tries)' or 'spy (spies)'
elif re.search(_pluralizeRegex, lowered):
return matchCase(s, s[:-1] + 'ies')
# In all other cases, we simply add an 's' to the base word
else: else:
return matchCase(s, s+'s') return matchCase(s, s+'s')
_depluralizeRegex = re.compile('[%s]ies' % consonants)
def depluralize(s): def depluralize(s):
"""Returns the singular of s.""" """Returns the singular of s."""
lowered = s.lower() lowered = s.lower()
if lowered in plurals: if lowered in plurals:
return matchCase(s, plurals[lowered]) return matchCase(s, plurals[lowered])
elif any(lowered.endswith, ['ches', 'sses']): elif any(lowered.endswith, ['ches', 'shes', 'sses']):
return s[:-2] return s[:-2]
elif lowered.endswith('ies'): elif re.search(_depluralizeRegex, lowered):
return matchCase(s, s[:-3]+'y') return s[:-3] + 'y'
else: else:
if lowered.endswith('s'): if lowered.endswith('s'):
return s[:-1] # Chop off 's'. return s[:-1] # Chop off 's'.