-2
我試圖做一個乾淨的文檔操作刪除停用詞,詞性標註及以下詞幹是我的代碼Python的AttributeError的:「元組」對象有沒有屬性「低」
def cleanDoc(doc):
stopset = set(stopwords.words('english'))
stemmer = nltk.PorterStemmer()
#Remove punctuation,convert lower case and split into seperate words
tokens = re.findall(r"<a.*?/a>|<[^\>]*>|[\w'@#]+", doc.lower() ,flags = re.UNICODE | re.LOCALE)
#Remove stopwords and words < 2
clean = [token for token in tokens if token not in stopset and len(token) > 2]
#POS Tagging
pos = nltk.pos_tag(clean)
#Stemming
final = [stemmer.stem(word) for word in pos]
return final
我得到這個錯誤:
Traceback (most recent call last):
File "C:\Users\USer\Desktop\tutorial\main.py", line 38, in <module>
final = cleanDoc(doc)
File "C:\Users\USer\Desktop\tutorial\main.py", line 30, in cleanDoc
final = [stemmer.stem(word) for word in pos]
File "C:\Python27\lib\site-packages\nltk\stem\porter.py", line 556, in stem
stem = self.stem_word(word.lower(), 0, len(word) - 1)
AttributeError: 'tuple' object has no attribute 'lower'
你試過任何調試,以找出爲什麼''字''是'元組'而不是字符串?或者查找'ntlk.pos_tag()'的文檔來查看它返回的內容而不是一串字符串? – millimoose