954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Plural of Words

Is there a reliable way to 'pluralize' english words with Python?

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Unfortunately, it won't always be accurate unless you go to great pains. This is due to there being multiple rules (and exceptions to those rules) for the English language.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
Is there a reliable way to 'pluralize' english words with Python?


there are some cookbook recipe in ASPN you can refer to. here .
they may not be wat you want, but at least will give you a head start

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

Python's regex module re allows you to establish english plural rules in a more condensed from. This one should take care of most common plurals, you may add more rules ...

# using module re to pluralize most common english words
# (rule_tuple used as function default, so establish it first)
 
import re
 
# (pattern, search, replace) regex english plural rules tuple
rule_tuple = (
('[ml]ouse$', '([ml])ouse$', '\\1ice'), 
('child$', 'child$', 'children'), 
('booth$', 'booth$', 'booths'), 
('foot$', 'foot$', 'feet'), 
('ooth$', 'ooth$', 'eeth'), 
('l[eo]af$', 'l([eo])af$', 'l\\1aves'), 
('sis$', 'sis$', 'ses'), 
('man$', 'man$', 'men'), 
('ife$', 'ife$', 'ives'), 
('eau$', 'eau$', 'eaux'), 
('lf$', 'lf$', 'lves'), 
('[sxz]$', '$', 'es'), 
('[^aeioudgkprt]h$', '$', 'es'), 
('(qu|[^aeiou])y$', 'y$', 'ies'), 
('$', '$', 's')
)
 
def regex_rules(rules=rule_tuple):
    for line in rules:
        pattern, search, replace = line
        yield lambda word: re.search(pattern, word) and re.sub(search, replace, word)
 
def plural(noun):
    for rule in regex_rules():
        result = rule(noun)
        if result: 
            return result
 
# testing ...
print plural("man")    # men
print plural("woman")  # women
print plural("lady")   # ladies
print plural("wife")   # wives
print plural("leaf")   # leaves
# okay according to Webster ...
print plural("index")  # indexes
print plural("fungus") # funguses    
# etc.
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I have gone to "great pains" to create a module that accurately gives the plural of English words: inflect.py

>>> import inflect
>>> p = inflect.engine()
>>> p.pl('man')
'men'
>>> p.pl('woman')
'women'
>>> p.pl('human')
'humans'

to use it just do:
easy_install inflect.py
or
pip install inflect

http://pypi.python.org/pypi/inflect

pwdyson
Newbie Poster
1 post since Jul 2010
Reputation Points: 13
Solved Threads: 0
 

For simple idea how to deal with singular plural after you have them by program or by literals see my snippet:
Kbyte(s) eliminator

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You