Welcome, guest | Sign In | My Account | Store | Cart

Smart pluralisation function that provides more intelligence than simply adding an 's' to the end of a word.

Python, 58 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def Plural(num=1, text=''):
	if num == 1:
		# singular -- easy
		result = '%d %s' % (num, text)
	else:
		aberrant = {	'knife' 	: 'knives',
				'self'		: 'selves',
				'elf'		: 'elves',
				'life'		: 'lives',
				'hoof'		: 'hooves',
				'leaf'		: 'leaves',
				'echo'		: 'echoes',
				'embargo'	: 'embargoes',
				'hero'		: 'heroes',
				'potato'	: 'potatoes',
				'tomato'	: 'tomatoes',
				'torpedo'	: 'torpedoes',
				'veto'		: 'vetoes',
				'child'		: 'children',
				'woman'		: 'women',
				'man'		: 'men',
				'person'	: 'people',
				'goose'		: 'geese',
				'mouse'		: 'mice',
				'barracks'	: 'barracks',
				'deer'		: 'deer',
				'nucleus'	: 'nuclei',
				'syllabus'	: 'syllabi',
				'focus'		: 'foci',
				'fungus'	: 'fungi',
				'cactus'	: 'cacti',
				'phenomenon'	: 'phenomena',
				'index'		: 'indices',
				'appendix'	: 'appendices',
				'criterion'	: 'criteria'
				}

		if aberrant.has_key(text):
			result = '%d %s' % (num, aberrant[text])
		else:
			postfix = 's'
			if len(text) > 2:
				vowels = 'aeiou'
				if text[-2:] in ('ch', 'sh'):
					postfix = 'es'
				elif text[-1:] == 'y':
					if (text[-2:-1] in vowels) or (text[0] in string.uppercase):
						postfix = 's'
					else:
						postfix = 'ies'
						text = text[:-1]
				elif text[-2:] == 'is':
					postfix = 'es'
					text = text[:-2]
				elif text[-1:] in ('s', 'z', 'x'):
					postfix = 'es'
				
			result = '%d %s%s' % (num, text, postfix)

Often one wishes to easily produce output for singular or plural forms of English language nouns. For example "Thanks for ordering 1 shirt" or "Thanks for ordering 3 shirts". This can be accomplished simply by appending an "s" to the singular form of the noun when the number is not 1. However, this simple form will not produce the correct results in cases like "Thanks for ordering 4 cacti".

The function here will do the trick, by comparing the text to a known list of aberrations, and by using the grammar rules found here: http://owl.english.purdue.edu/handouts/grammar/g_spelnoun.html http://webster.commnet.edu/grammar/plurals.htm

Further aberrant cases can be added to the dictionary as they are found. These are forms that: * end in 'f' --> 'ves' * end in 'o' --> 'oes' * mutate, eg: 'child' --> 'children' * maintain Latin or Greek form in plural, eg: 'fungus' --> 'fungi'

The text passed to this function should be the correct singular form in correct capitalisation, since proper names do not follow the same rules.

1 comment

Paul Dyson 13 years, 8 months ago  # | flag

For a comprehensive plural generator see inflect.py http://pypi.python.org/pypi/inflect

$ easy_install inflect.py

>>> import inflect
>>> p = inflect.engine()
>>> p.pl('die')
'dice'
Created by Robin Parmar on Tue, 16 Oct 2001 (PSF)
Python recipes (4591)
Robin Parmar's recipes (9)

Required Modules

  • (none specified)

Other Information and Tasks