From 949ea7aa2bb300c90137024c5871b61851c622a4 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 2 Dec 2003 18:58:57 +0000 Subject: [PATCH] Made utils.pluralize a little smarter. --- src/utils.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/utils.py b/src/utils.py index 376788c71..fe4f204b8 100755 --- a/src/utils.py +++ b/src/utils.py @@ -48,6 +48,7 @@ import sgmllib import compiler import textwrap import htmlentitydefs +from itertools import imap, ifilter from structures import TwoWayDictionary @@ -304,16 +305,17 @@ def ellipsisify(s, n): else: return (textwrap.wrap(s, n-3)[0] + '...') -plurals = TwoWayDictionary({'match': 'matches', - 'patch': 'patches', - 'loss': 'losses'}) +plurals = TwoWayDictionary({}) def _matchCase(s1, s2): """Matches the case of s1 in s2""" - L = list(s2) - for (i, char) in enumerate(s1[:len(s2)]): - if char.isupper(): - L[i] = char.upper() - return ''.join(L) + if s1.isupper(): + return s2.upper() + else: + L = list(s2) + for (i, char) in enumerate(s1[:len(s2)]): + if char.isupper(): + L[i] = char.upper() + return ''.join(L) def pluralize(i, s): """Returns the plural of s based on its number i. Put any exceptions to @@ -325,17 +327,18 @@ def pluralize(i, s): lowered = s.lower() if lowered in plurals: return _matchCase(s, plurals[lowered]) + elif any(lowered.endswith, ['ch', 'ss']): + return _matchCase(s, s+'es') else: - if s.isupper(): - return s + 'S' - else: - return s + 's' + return _matchCase(s, s+'s') def depluralize(s): """Returns the singular of s.""" lowered = s.lower() if lowered in plurals: return _matchCase(s, plurals[lowered]) + elif any(lowered.endswith, ['ches', 'sses']): + return s[:-2] else: if s.endswith('s') or s.endswith('S'): return s[:-1] # Chop off 's'.