Search code examples
pythonsliceprefixes

Slicing the prefix from the stem in Python


I want to slice the affixes from stem. I tried the suffixes with the following command and it was OK for 'dishes'. However when I want to do it with a prefix (for example, 'undo'), how can I define the prefix in Python to get the result of un-do?

>>> def stem(word):
    for suffix in ['ing', 'lity', 'es']:
        if word.endswith(suffix):
            return word[:-len(suffix)]
        return word
>>> re.findall(r'^(.*)(ing|lity|es)$', 'dishes')
[('dish', 'es')]

Solution

  • Well, why not use regulare expressions the same way you did ?

    >>> re.findall(r'^(un|ir)(.*)$', 'undo')
    [('un', 'do')]