Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Bagofwords没有属性精确性和召回率_Python_Python 3.x_Attributes_Nltk_Precision - Fatal编程技术网

Python Bagofwords没有属性精确性和召回率

Python Bagofwords没有属性精确性和召回率,python,python-3.x,attributes,nltk,precision,Python,Python 3.x,Attributes,Nltk,Precision,POS\u WORDS\u FILE=os.path.join(英语意见词典位置,'positive WORDS.txt') NEG_WORDS_FILE=os.path.join(英语意见词典位置,'negative WORDS.txt') pos_words=[({'mazing':True},'positive'),({'great':True},'positive')] neg_words=[({'pathetic':True},'negative')] 对于打开的pos_单词(pos_

POS\u WORDS\u FILE=os.path.join(英语意见词典位置,'positive WORDS.txt') NEG_WORDS_FILE=os.path.join(英语意见词典位置,'negative WORDS.txt')

pos_words=[({'mazing':True},'positive'),({'great':True},'positive')] neg_words=[({'pathetic':True},'negative')] 对于打开的pos_单词(pos_单词文件'r')。readlines()[35:]: pos_words.append({pos_word.rstrip():True},'positive'))

我遇到了如下错误:
AttributeError:模块“nltk.translate.metrics”没有属性“scores”

我认为您错误地调用了精度函数。 从表面上看,似乎你应该使用
nltk.metrics.scores.precision
而不是
nltk.metrics.precision
。召回也是如此。

你可以试试

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()
并直接调用precision方法。它会成功的

只需导入以下内容。

并直接调用precision方法,如下所示

from nltk import precision

它肯定会有用的

但是,它仍然给出了另一个错误:AttributeError:module'nltk.translate.metrics'没有属性'scores'。您可以检查您是否没有按照错误提示包含单词'translate'?
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()
from nltk import precision
from nltk import precision
nltk.precision(expected_neg_set['negative'], actual_neg_set['negative']))