Home > Enterprise >  Stemming words in a list (Python NLTK)
Stemming words in a list (Python NLTK)

Time:12-13

I feel like I'm doing something really stupid here, I am trying to stem words I have in a list but it is not giving me the intended outcome, my code is:

from nltk.stem.snowball import SnowballStemmer
snowball = SnowballStemmer(language="english")
my_words = ['works', 'shooting', 'runs']
for w in my_words:
    w=snowball.stem(w)
print(my_words)

and the ouput I get is

['works', 'shooting', 'runs]

as opposed to

['work','shoot','run']

I feel like I'm doing something very silly with my list but could anyone enlighten me what I'm doing wrong?

CodePudding user response:

You can use list comprehension :

[snowball.stem(word) for word in my_words]

CodePudding user response:

Silly me,

I just created a new list inside and append to it to give the intended outcome:

from nltk.stem.snowball import SnowballStemmer
snowball = SnowballStemmer(language="english")
new_list=[]
my_words = ['works', 'shooting']
for w in my_words:
    w=snowball.stem(w)
    new_list.append(w)
print(new_list)
  • Related