ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Smart pluralisation (English)
Submitter: Robin Parmar (other recipes)
Last Updated: 2001/10/16
Version no: 1.0
Category: Text

 

4 stars 1 vote(s)


Description:

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

Source: Text Source

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)

Discussion:

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.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Treat the Win32 Registry ...

5. a friendly mkdir()

6. Wrapping template engine ...

7. Assignment in expression

8. Changing return value ...

9. Implementation of sets ...

10. bag collection class




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.