Search code examples
pythonnlpstemmingnormalizestem

How to modify word in a for loop in python


Im trying to stem some text in python with SnowballStemmer, but it wont work. Here is the code:

import nltk
from nltk import SnowballStemmer

stem = SnowballStemmer("spanish")

def limpiar (texto):
  texto = texto.split()
  stemm = SnowballStemmer('spanish')
  for palabra in texto:
    palabra = stem.stem(palabra.lower())
    
  return texto

It returns the text in lower capitals, but without stemming


Solution

  • The will work :

    import nltk
    from nltk.stem.snowball import SnowballStemmer
    
    def limpiar(texto):
        words=texto.split()
        stem_words=[]
        for w in words: 
            x = snow_stemmer.stem(w) 
            stem_words.append(x) 
    
        #print stemming results 
        for e1,e2 in zip(words,stem_words): 
            print(e1+' ----> '+e2.lower())
    
    snow_stemmer = SnowballStemmer(language='spanish')
    texto="..."
    limpiar(texto)