对负面评论进行'pos'测试

时间:2017-03-01 05:18:37

标签: python nlp nltk document-classification nltk-trainer

好的,所以我训练了一个NaiveBayes电影评论分类器...但是当我针对负面评论(从我复制并粘贴到txt文件的网站)运行时,我得到'pos'...我在做什么有问题?以下是代码:

import nltk, random
from nltk.corpus import movie_reviews
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
random.shuffle(documents)
all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = list(all_words)[:2000]

def document_features(document): 
    document_words = set(document) 
    features = {}
    for word in word_features:
        features['contains({})'.format(word)] = (word in document_words)
    return features

featuresets = [(document_features(d), c) for (d,c) in documents]
train_set, test_set = featuresets[100:], featuresets[:100]
classifier = nltk.NaiveBayesClassifier.train(train_set)

print(nltk.classify.accuracy(classifier, test_set)) 
classifier.show_most_informative_features(5)
>>>0.67
>>>Most Informative Features
      contains(thematic) = True              pos : neg    =      8.9 : 1.0
        contains(annual) = True              pos : neg    =      8.9 : 1.0
       contains(miscast) = True              neg : pos    =      8.7 : 1.0
      contains(supports) = True              pos : neg    =      6.9 : 1.0
    contains(unbearable) = True              neg : pos    =      6.7 : 1.0

f = open('negative_review.txt','rU')
fraw = f.read()
review_tokens =nltk.word_tokenize(fraw)
docfts = document_features(review_tokens)

classifier.classify(docfts)
>>>    'pos'

更新多次重新运行程序后,它现在可以准确地将我的负面评论归类为负面...有人可以帮我理解原因吗?或者这是一般的巫术?

1 个答案:

答案 0 :(得分:1)

分类器不是100%准确。更好的测试是查看分类器在多个电影评论中的行为方式。我看到分类器的准确率为67%,这意味着1/3的评论将被错误分类。您可以尝试使用不同的分类器或不同的功能来改进模型(尝试n-gram和word2vec)。