Search code examples
pythonnlpstemming

stemming words in python


I'm using this code to stem words, here is how it works, first there's a list of suffixes, the program checks if the word has the ending same as the one in the list if positive it removes the suffix, however, when I run the code I get this result:

suffixes = ['ing']
def stem(word):
for suff in suffixes:
    return word[:-len(suff)]

stem ('having')
print (stem)

Solution

  • For each suffix in the given list you can check if the given word ends with any of the given suffixes, if yes the remove the suffix, else return the word.

    suffixes = ['ing']
    def stem(word):
        for suff in suffixes:
            if word.endswith(suff):
                return word[:-len(suff)]
    
        return word
    
    print(stem ('having'))
    >>> hav