IVB_CODING
IVB_CODING

Reputation: 23

Stemming words in a list (Python NLTK)

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?

Upvotes: 0

Views: 947

Answers (2)

AfterFray
AfterFray

Reputation: 1851

You can use list comprehension :

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

Upvotes: 1

IVB_CODING
IVB_CODING

Reputation: 23

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)

Upvotes: 0

Related Questions