License Python Software Foundation License (Python 2.x)
Lines 29
Keywords
pluralize (1)
Included in this Library
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Siafoo – the intersection of pastebin, help desk, version control, and social networking Join Siafoo Now or Learn More

Pluralize Nouns Atom Feed 0

In Brief Easily and correctly pluralize most English nouns.... more
# 's
 1"""Pluralize English nouns (stage 6)
2
3This program is part of "Dive Into Python", a free Python book for
4experienced programmers. Visit http://diveintopython.org/ for the
5latest version.
6
7Command line usage:
8$ python plural6.py noun
9nouns
10"""
11
12__author__ = "Mark Pilgrim (mark@diveintopython.org)"
13__version__ = "$Revision: 1.7 $"
14__date__ = "$Date: 2004/05/03 19:40:42 $"
15__copyright__ = "Copyright (c) 2004 Mark Pilgrim"
16__license__ = "Python"
17
18import re
19
20def rules(language):
21 for line in file('plural-rules.%s' % language):
22 pattern, search, replace = line.split()
23 yield lambda word: re.search(pattern, word) and re.sub(search, replace, word)
24
25def plural(noun, language='en'):
26 """returns the plural form of a noun"""
27 for applyRule in rules(language):
28 result = applyRule(noun)
29 if result: return result
30
31if __name__ == '__main__':
32 import sys
33 if sys.argv[1:]:
34 print plural(sys.argv[1])
35 else:
36 print __doc__

Easily and correctly pluralize most English nouns.

You'll also need pluralization rules for your chosen language. English ones are here, save them as 'plural-rules.en':

 1^(sheep|deer|fish|moose|aircraft|series|haiku)$     ($)     \1
2[ml]ouse$ ouse$ ice
3child$ $ ren
4booth$ $ s
5foot$ oot$ eet
6ooth$ ooth$ eeth
7l[eo]af$ af$ aves
8sis$ sis$ ses
9^(hu|ro)man$ $ s
10man$ man$ men
11^lowlife$ $ s
12ife$ ife$ ives
13eau$ $ x
14^[dp]elf$ $ s
15lf$ lf$ lves
16[sxz]$ $ es
17[^aeioudgkprt]h$ $ es
18(qu|[^aeiou])y$ y$ ies
19$ $ s

Here's a unittest to make sure everything works:

 1"""Unit test for plural.py
2
3This program is part of "Dive Into Python", a free Python book for
4experienced programmers. Visit http://diveintopython.org/ for the
5latest version.
6"""
7
8__author__ = "Mark Pilgrim (mark@diveintopython.org)"
9__version__ = "$Revision: 1.2 $"
10__date__ = "$Date: 2004/03/17 14:34:40 $"
11__copyright__ = "Copyright (c) 2004 Mark Pilgrim"
12__license__ = "Python"
13
14from plural import plural
15import unittest, new
16
17class KnownValues(unittest.TestCase):
18 nouns = {'bass': 'basses',
19 'bus': 'buses',
20 'walrus': 'walruses',
21 'box': 'boxes',
22 'fax': 'faxes',
23 'suffix': 'suffixes',
24 'mailbox': 'mailboxes',
25 'buzz': 'buzzes',
26 'waltz': 'waltzes',
27 'coach': 'coaches',
28 'glitch': 'glitches',
29 'rash': 'rashes',
30 'watch': 'watches',
31 'cheetah': 'cheetahs',
32 'cough': 'coughs',
33 'utility': 'utilities',
34 'vacancy': 'vacancies',
35 'soliloquy': 'soliloquies',
36 'boy': 'boys',
37 'day': 'days',
38 'computer': 'computers',
39 'rock': 'rocks',
40 'paper': 'papers',
41
42 'mouse': 'mice',
43 'louse': 'lice',
44 'child': 'children',
45 'foot': 'feet',
46 'booth': 'booths',
47 'tooth': 'teeth',
48 'leaf': 'leaves',
49 'loaf': 'loaves',
50 'thesis': 'theses',
51 'man': 'men',
52 'mailman': 'mailmen',
53 'knife': 'knives',
54 'wife': 'wives',
55 'tableau': 'tableaux',
56 'elf': 'elves',
57 'shelf': 'shelves',
58 'sheep': 'sheep',
59 'deer': 'deer',
60 'fish': 'fish',
61 'moose': 'moose',
62 'aircraft': 'aircraft',
63 'series': 'series',
64 'haiku': 'haiku',
65 'delf': 'delfs',
66 'pelf': 'pelfs',
67 'human': 'humans',
68 'roman': 'romans',
69 'lowlife': 'lowlifes',
70 }
71
72for noun, pluralnoun in KnownValues.nouns.items():
73 func = lambda self, noun=noun, pluralnoun=pluralnoun: \
74 KnownValues.failUnlessEqual(self, plural(noun), pluralnoun)
75 func.__doc__ = "%s --> %s" % (noun, pluralnoun)
76 instanceMethod = new.instancemethod(func, None, KnownValues)
77 setattr(KnownValues, "test_%s" % noun, instanceMethod)
78
79if __name__ == "__main__":
80 unittest.main()