944,164 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6482
  • Python RSS
Feb 21st, 2007
0

Plural of Words

Expand Post »
Is there a reliable way to 'pluralize' english words with Python?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Feb 21st, 2007
0

Re: Plural of Words

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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 21st, 2007
0

Re: Plural of Words

Click to Expand / Collapse  Quote originally posted by sneekula ...
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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Feb 22nd, 2007
0

Re: Plural of Words

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 ...
python Syntax (Toggle Plain Text)
  1. # using module re to pluralize most common english words
  2. # (rule_tuple used as function default, so establish it first)
  3.  
  4. import re
  5.  
  6. # (pattern, search, replace) regex english plural rules tuple
  7. rule_tuple = (
  8. ('[ml]ouse$', '([ml])ouse$', '\\1ice'),
  9. ('child$', 'child$', 'children'),
  10. ('booth$', 'booth$', 'booths'),
  11. ('foot$', 'foot$', 'feet'),
  12. ('ooth$', 'ooth$', 'eeth'),
  13. ('l[eo]af$', 'l([eo])af$', 'l\\1aves'),
  14. ('sis$', 'sis$', 'ses'),
  15. ('man$', 'man$', 'men'),
  16. ('ife$', 'ife$', 'ives'),
  17. ('eau$', 'eau$', 'eaux'),
  18. ('lf$', 'lf$', 'lves'),
  19. ('[sxz]$', '$', 'es'),
  20. ('[^aeioudgkprt]h$', '$', 'es'),
  21. ('(qu|[^aeiou])y$', 'y$', 'ies'),
  22. ('$', '$', 's')
  23. )
  24.  
  25. def regex_rules(rules=rule_tuple):
  26. for line in rules:
  27. pattern, search, replace = line
  28. yield lambda word: re.search(pattern, word) and re.sub(search, replace, word)
  29.  
  30. def plural(noun):
  31. for rule in regex_rules():
  32. result = rule(noun)
  33. if result:
  34. return result
  35.  
  36. # testing ...
  37. print plural("man") # men
  38. print plural("woman") # women
  39. print plural("lady") # ladies
  40. print plural("wife") # wives
  41. print plural("leaf") # leaves
  42. # okay according to Webster ...
  43. print plural("index") # indexes
  44. print plural("fungus") # funguses
  45. # etc.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 9th, 2010
3
Re: Plural of Words
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
Last edited by pwdyson; Jul 9th, 2010 at 7:05 pm.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
pwdyson is offline Offline
1 posts
since Jul 2010
Jul 10th, 2010
0
Re: Plural of Words
For simple idea how to deal with singular plural after you have them by program or by literals see my snippet:
Kbyte(s) eliminator
Featured Poster
Reputation Points: 687
Solved Threads: 749
Industrious Poster
pyTony is offline Offline
4,209 posts
since Apr 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: line number in wxpython
Next Thread in Python Forum Timeline: Kbyte(s) eliminator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC