【发布时间】:2018-04-09 17:44:24
【问题描述】:
所以,我是使用 Python 和 NLTK 的新手。我有一个名为 reviews.csv 的文件,其中包含从亚马逊提取的 cmets。我已经标记了这个 csv 文件的内容并将其写入一个名为 csvfile.csv 的文件中。这是代码:
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem import PorterStemmer
import csv #CommaSpaceVariable
from nltk.corpus import stopwords
ps = PorterStemmer()
stop_words = set(stopwords.words("english"))
with open ('reviews.csv') as csvfile:
readCSV = csv.reader(csvfile,delimiter='.')
for lines in readCSV:
word1 = word_tokenize(str(lines))
print(word1)
with open('csvfile.csv','a') as file:
for word in word1:
file.write(word)
file.write('\n')
with open ('csvfile.csv') as csvfile:
readCSV1 = csv.reader(csvfile)
for w in readCSV1:
if w not in stopwords:
print(w)
我正在尝试对 csvfile.csv 执行词干提取。但我得到这个错误:
Traceback (most recent call last):<br>
File "/home/aarushi/test.py", line 25, in <module> <br>
if w not in stopwords: <br>
TypeError: argument of type 'WordListCorpusReader' is not iterable
【问题讨论】:
-
错字...使用
stop_words,而不是stopwords -
另外,真的有必要写一个文件,然后读回来打印出来吗?
-
1.我写了 stop_words 而不是停用词。现在我有另一个错误。 TypeError: unhashable type: 'list' 2. 我想要 word_tokenized 文件。这就是我这样做的原因。
-
1.每个 stackoverflow 问题都应该与一个问题有关。当你继续下一个问题时,问一个新问题。 2. 谁能猜出你的新错误来自哪里?你还没有发布代码。 (但不要编辑问题或将其发布在评论中:如果您仍然卡住,请提出新问题。)我的猜测是您正在尝试从错误类型的数据创建一个集合...
-
你还应该写一个minimal reproducible example... 你的代码错误在第一行没有从第三方库导入或声明一个类。换句话说,您似乎没有在每次添加功能时都在测试您的代码。您询问词干,但错误出现在停用词上。请参阅此处了解您在开始时尝试执行的操作stackoverflow.com/a/19133088/2308683