简体   繁体   中英

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)

尝试看看Porter Stemmer( http://tartarus.org/martin/PorterStemmer/ ),Python也有一个。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM