1
votes
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
class StemmedTfidfVectorizer(TfidfVectorizer):
   def build_analyzer(self):
      analyzer = super(TfidfVectorizer, self).build_analyzer()
      return lambda doc: stemmer.stem(analyzer(doc))

When I run the above lines of code, I get the following error.

return lambda doc: stemmer.stem(analyzer(doc))

File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 654, in stem

stem = word.lower()

AttributeError: 'list' object has no attribute 'lower'

How can this error be solved?

1
error is not in the code that you've given , its in line 654...please add that also - Vikas P
try to print the stack error with traceback.print_exe.. - Frank AK
Why don't just just pass stemmer.stem to preprocessor in the constructor? - juanpa.arrivillaga

1 Answers

3
votes

You are passing a list object in stemmer.stem() .The method takes a string object as input.

From the DOCS:

from nltk.stem.porter import *
stemmer = PorterStemmer()
plurals = ['caresses', 'flies', 'dies', 'mules', 'denied']
singles = [stemmer.stem(plural) for plural in plurals]  # ---> loop through the list and process each element.