我试图运行一个在堆栈溢出中提供的示例,即here.
我在这里再次复制了代码:
from sklearn.feature_extraction.text import TfidfVectorizer
text_files = ['file1.txt', 'file2.txt']
documents = [open(f) for f in text_files]
tfidf = TfidfVectorizer().fit_transform(documents)
# no need to normalize, since Vectorizer will return normalized tf-idf
pairwise_similarity = tfidf * tfidf.T
我添加的唯一内容就是这一行:
text_files = ['file1.txt', 'file2.txt']
当我运行代码时,我收到此错误:
File "C:\Python33\lib\site-packages\sklearn\feature_extraction\text.py", line 195, in <lambda>
return lambda x: strip_accents(x.lower())
AttributeError: '_io.TextIOWrapper' object has no attribute 'lower'
file1.txt和file2.txt是输入文本文件.我是否使用了错误的text_files格式?这个错误的原因是什么,我该如何解决?我真的很感激任何帮助.
解决方法:
open(f)是一个_io.TextIOWrapper对象,这就是它失败的原因.
尝试改变
documents = [open(f) for f in text_files]
至
documents = [open(f).read() for f in text_files]