Python增量

时间:2014-01-10 03:59:01

标签: python nltk addition subtraction sentiment-analysis

我正在编写一个接受文本作为输入的程序。

程序的值“音调”从0开始。 当它看到该文本中的单词也在单词“posfeats”列表中时,音调递增+1。 当它看到该文本中的单词也在单词“negfeats”列表中时,音调递增-1。

然而,无论我给出什么输入文本,我的代码都会返回值为“tone”的值为0。我觉得这是由于我错误的Python编程而不是我的算法。

以下是代码:

import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews #importing two corpora, movie_reviews and stopwords
from nltk.corpus import stopwords

def word_feats(words):
stops = dict([(word, True) for word in stopwords.words('english')]) #English stopwords
features = dict([(word, True) for word in words if word not in stops])#features minus stopwords
return features

def compare(words, negfeats, posfeats):
sentiment=0
for word in words:
    if word in negfeats:
        sentiment -= 1
    if word in posfeats:
        sentiment += 1
return sentiment


negReviews = reviews.fileids('neg')
posReviews = reviews.fileids('pos')

negfeats = [(word_feats(reviews.words(fileids=[f])), 'neg') for f in negReviews]
posfeats = [(word_feats(reviews.words(fileids=[f])), 'pos') for f in posReviews]

opinion = raw_input("Why don't you tell me about a movie you watched recently?\n\n")
tone = compare(opinion.split(), negfeats, posfeats)
print(str(tone)) #THIS KEEPS RETURNING 0

1 个答案:

答案 0 :(得分:1)

negfeats = [(word_feats(reviews.words(fileids=[f])), 'neg') for f in negReviews]
posfeats = [(word_feats(reviews.words(fileids=[f])), 'pos') for f in posReviews]

您的意思是dict来电吗? negfeatsposfeats(word, 'neg')(word, 'pos')元组的列表。 compare会在这些列表中搜索单词而不会找到任何单词,因为这些单词嵌套在元组中。当然,对于没有重复的无序集合,最好使用set