| | |
Plural of Words
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 149
Reputation:
Solved Threads: 40
•
•
•
•
Is there a reliable way to 'pluralize' english words with Python?
here.
they may not be wat you want, but at least will give you a head start
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)
# 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.
May 'the Google' be with you!
![]() |
Similar Threads
- Plural form of keywords (Pay-Per-Click Advertising)
- Simple Banned Words Filter (PHP)
- Getting words (PHP)
Other Threads in the Python Forum
- Previous Thread: Need guidance.
- Next Thread: Help undestanding popen3 function
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm assignment avogadro beginner bluetooth character cmd code copy customdialog cx-freeze data decimals dictionary directory dynamic error examples excel exe file float format ftp function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop module mouse number numbers output parsing path port prime program programming projects push py2exe pygame pyglet pyqt python random recursion recursive schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable ventrilo verify vigenere webservice wikipedia windows wxpython xlib






