def predict(testSet,PP,PN,positive_probabilities,negative_probabilities,unseen_pos_prob,unseen_neg_prob):
predicted_class = []
for review in testSet:
negative_probab = math.log10(PN)
positive_probab = math.log10(PP)
review_words = word_tokenize(review)
for w in review_words:
if w in negative_probabilities:
negative_probab = negative_probab + math.log10(negative_probabilities[w])
else:
negative_probab = negative_probab + math.log10(unseen_neg_prob)
if w in positive_probabilities:
positive_probab = positive_probab + math.log10(positive_probabilities[w])
else:
positive_probab = positive_probab + math.log10(unseen_pos_prob)
if(negative_probab > positive_probab):
result = '-'
else:
result = '+'
predicted_class.append(result)
return predicted_class
评论列表
文章目录