How can I get the root word in Python? -
i want root of word. not using stemmer because want replacements. here code; gives me correct results, except not replace "ies" "y" when token ends in "ies":
import string; contents = ["shoping", "balls", "babies"] token in contents: if token.endswith("ies"): string.replace(token,'ies','y',1) print token elif token.endswith('s'): print token[0:-1] elif token.endswith("ed"): print token[0:-2] elif token.endswith("ing"): print token[0:-3]
to add bit more gobusto's answer, use of string library redundant (as semi colon after import string).
you can instead:
contents = ["shoping", "balls", "babies"] token in contents: if token.endswith("ies"): token = token.replace('ies','y',1) print token elif token.endswith('s'): print token[0:-1] elif token.endswith("ed"): print token[0:-2] elif token.endswith("ing"): print token[0:-3]
Comments
Post a Comment