import os
ENGLISH_OPINION_LEXICON_LOCATION = os.path.join('opinion-lexicon-English')
POS_WORDS_FILE = os.path.join(ENGLISH_OPINION_LEXICON_LOCATION,'positive-words.txt') NEG_WORDS_FILE = os.path.join(ENGLISH_OPINION_LEXICON_LOCATION,'negative-words.txt')
pos_words = []
neg_words = []
打开pos_word(POS_WORDS_FILE,'r')。readlines()[35:]: pos_words.append(({pos_word.rstrip():True},'positive'))
for neg_word in open(NEG_WORDS_FILE, 'r').readlines()[35:]:
neg_words.append(({neg_word.rstrip(): True}, 'negative'))
print ("First 5 positive words %s " % pos_words[:5])
print ("First 5 negative words %s" % neg_words[:5])
print ("Number of positive words %d" % len(pos_words))
print ("Number of negative words %d" % len(neg_words))
all_words_with_sentiment = pos_words + neg_words
print ("Total number of words %d" % len(all_words_with_sentiment))
from nltk.classify import NaiveBayesClassifier
classifier = NaiveBayesClassifier.train(all_words_with_sentiment)
def to_dictionary(words):
return dict([(word, True) for word in words])
test_data = []
def predict_sentiment(text, expected_sentiment=None):
text_to_classify = to_dictionary(text.split())
result = classifier.classify(text_to_classify)
test_data.append([text_to_classify, expected_sentiment])
return result
POLARITY_DATA_DIR = os.path.join('polarity-data', 'rt-polaritydata')
POSITIVE_REVIEWS_FILE = os.path.join(POLARITY_DATA_DIR, 'rt-polarity-pos.txt')
NEGATIVE_REVIEWS_FILE = os.path.join(POLARITY_DATA_DIR, 'rt-polarity-neg.txt')
import collections
import nltk.classify
import nltk.metrics
#import nltk.metrics.scores
from nltk.util import LazyConcatenation, LazyMap
from nltk.util import LazyConcatenation, LazyMap
from decimal import *
from nltk.metrics.scores import (precision, recall)
from nltk.metrics import precision, recall
#from sklearn.metrics import precision_score
def run_sentiment_analysis_on_rt():
rt_positive_reviewers = open(POSITIVE_REVIEWS_FILE, 'r')
expected_pos_set = collections.defaultdict(set)
actual_pos_set = collections.defaultdict(set)
for index, review in enumerate(rt_positive_reviewers.readlines()):
expected_pos_set['positive'].add(index)
actual_sentiment = predict_sentiment(review, 'positive')
actual_pos_set[actual_sentiment].add(index)
print ("Total Negative found in positive reviews %s" %
len(actual_pos_set['negative']))
rt_negative_reviews = open(NEGATIVE_REVIEWS_FILE, 'r')
expected_neg_set = collections.defaultdict(set)
actual_neg_set = collections.defaultdict(set)
for index, review in enumerate(rt_negative_reviews.readlines()):
expected_neg_set['negative'].add(index)
actual_sentiment = predict_sentiment(review, 'negative')
actual_neg_set[actual_sentiment].add(index)
print ("Total Positive found in negative reviews %s" %
len(actual_neg_set['positive']))
print ('accuracy: %.2f' % nltk.classify.util.accuracy(classifier, test_data))
#print ('pos precision:', nltk.metrics.precision(refsets['pos'], testsets['pos']))
print ('pos precision: %.2f' %
nltk.metrics.scores.precision(expected_pos_set['positive'], actual_pos_set['positive']))
print ('pos recall: %.2f' %
nltk.metrics.scores.recall(expected_pos_set['positive'], actual_pos_set['positive']))
print ('neg precision: %.2f' %
nltk.metrics.scores.precision(expected_neg_set['negative'], actual_neg_set['negative']))
print ('neg recall: %.2f' %
nltk.metrics.scores.recall(expected_neg_set['negative'], actual_neg_set['negative']))
run_sentiment_analysis_on_rt()
我得到的错误如下: AttributeError:module'nltk.translate.metrics'没有属性'scores'
答案 0 :(得分:1)
你可以尝试
from nltk import precision
并直接调用precision方法。它会工作!
答案 1 :(得分:0)
我认为你错误地调用了精确功能。
从docs来看,你似乎应该使用它
nltk.metrics.scores.precision
代替nltk.metrics.precision
。回忆也是如此。
答案 2 :(得分:0)
只需导入以下内容。
from nltk import precision
并按如下所示直接调用precision方法。
nltk.precision(expected_neg_set['negative'], actual_neg_set['negative']))
肯定会起作用!