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))
当我运行上面的代码行时,我收到以下错误。
返回lambda doc:stemmer.stem(analyzer(doc))
文件“/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py”,第654行,干
stem = word.lower()
AttributeError:'list'对象没有属性'lower'
如何解决这个错误?
答案 0 :(得分:1)
您正在stemmer.stem()
中传递列表对象。该方法将字符串对象作为输入。
来自 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.