gist: 3018 Download_button fork
public
Public Clone URL: git://gist.github.com/3018.git
singularize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def singularize(word):
    """Return the singular form of a word
>>> singularize('rabbits')
'rabbit'
>>> singularize('potatoes')
'potato'
>>> singularize('leaves')
'leaf'
>>> singularize('knives')
'knife'
>>> singularize('spies')
'spy'
"""
    sing_rules = [lambda w: w[-3:] == 'ies' and w[:-3] + 'y',
                  lambda w: w[-4:] == 'ives' and w[:-4] + 'ife',
                  lambda w: w[-3:] == 'ves' and w[:-3] + 'f',
                  lambda w: w[-2:] == 'es' and w[:-2],
                  lambda w: w[-1:] == 's' and w[:-1],
                  lambda w: w,
                  ]
    word = word.strip()
    singleword = [f(word) for f in sing_rules if f(word) is not False][0]
    return singleword
 
def _test():
    import doctest
    doctest.testmod()
 
if __name__ == '__main__':
    _test()

Owner

cnu

Revisions