我正在运行一些NLP代码,试图在一项调查中找到最有影响力(正面或负面)的单词。我的问题是,虽然我成功地在NLTK的秒表文件中添加了一些额外的停止词,但它们在后面一直作为有影响力的单词出现。
所以,我有一个数据,第一列包含分数,第二列包含评论。
我再加一句:
stopwords = stopwords.words('english')
extra = ['Cat', 'Dog']
stopwords.extend(extra)我在前面和后面使用len方法检查它们是否被添加。
我创建这个函数是为了删除我评论中的标点符号和句号:
def text_process(comment):
nopunc = [char for char in comment if char not in string.punctuation]
nopunc = ''.join(nopunc)
return [word for word in nopunc.split() if word.lower() not in stopwords]我运行模型(不包括整个代码,因为它没有什么区别):
corpus = df['Comment']
y = df['Label']
vectorizer = CountVectorizer(analyzer=text_process)
x = vectorizer.fit_transform(corpus)..。
然后得到最有影响力的词:
feature_to_coef = {word: coef for word, coef in zip(vectorizer.get_feature_names(), nb.coef_[0])}
for best_positive in sorted(
feature_to_coef.items(),
key=lambda x: x[1],
reverse=True)[:20]:
print (best_positive)但是,猫和狗在结果中。
我做错什么了,有什么想法吗?
非常感谢!
发布于 2019-05-30 16:44:21
看起来是因为你用“猫”和“狗”大写
在您的text_process函数中,只有当停止词是小写的情况下,才有if word.lower() not in stopwords。
https://stackoverflow.com/questions/56381970
复制相似问题