Plural of Words

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 2,288
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Plural of Words

 
0
  #1
Feb 21st, 2007
Is there a reliable way to 'pluralize' english words with Python?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Plural of Words

 
0
  #2
Feb 21st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 149
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Plural of Words

 
0
  #3
Feb 21st, 2007
Originally Posted by sneekula View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Plural of Words

 
0
  #4
Feb 22nd, 2007
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 ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC