带有预定义文本的情感分析

时间:2019-03-14 04:23:10

标签: python nltk sentiment-analysis

我正在使用NLTK在Python中进行情感分析项目。项目的输出必须显示给定的陈述是肯定的还是负面的。我已经成功地做到了,但是如何获得中立声明的输出呢? 并可以百分比形式输出(即正百分比,负百分比或中性百分比)吗?

classifier.py

import random
import preprocess
import nltk

def get_classifier():
    data = preprocess.get_data()
    random.shuffle(data)

    split = int(0.8 * len(data))

    train_set = data[:split]
    test_set =  data[split:]

    classifier = nltk.NaiveBayesClassifier.train(train_set)

    accuracy = nltk.classify.util.accuracy(classifier, test_set)
    print("Generated Classifier")
    print('-'*70)
    print("Accuracy: ", accuracy)
    return classifier

preprocess.py

import nltk.classify
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

stop_words = stopwords.words("english")

def create_word_features_pos(words):
    useful_words = [word for word in words if word not in stop_words]
    my_list = [({word: True}, 'positive') for word in useful_words]
    return my_list

def create_word_features_neg(words):
    useful_words = [word for word in words if word not in stop_words]
    my_list = [({word: True}, 'negative') for word in useful_words]
    return my_list

def create_word_features(words):
    useful_words = [word for word in words if word not in stopwords.words("english")]

    pos_txt = get_tokenized_file(u"positive-words.txt")
    neg_txt = get_tokenized_file(u"negative-words.txt")

    my_dict = dict([(word, True) for word in pos_txt if word in useful_words])
    my_dict1 = dict([(word, False) for word in neg_txt if word in useful_words])
    my_dict3 = dict([word,])
    my_dict.update(my_dict1)

    return my_dict

def get_tokenized_file(file):
    return word_tokenize(open(file, 'r').read())

def get_data():
    print("Collecting Negative Words")
    neg_txt = get_tokenized_file(u"negative-words.txt")
    neg_features = create_word_features_neg(neg_txt)

    print("Collecting Positive Words")
    pos_txt = get_tokenized_file(u"positive-words.txt")
    pos_features = create_word_features_pos(pos_txt)
    return pos_features + neg_features

def process(data):
    return [word.lower() for word in word_tokenize(data)]

1 个答案:

答案 0 :(得分:0)

nltk.NaiveBayesClassifier.train的文档:

  

参数:labeled_featuresets –分类特征集的列表,   即元组列表(功能集,标签)。

这意味着您的train_set(features, label)的一组元组。

如果要添加neutral类型,则需要将某些数据标记为neutral,否则分类器将无法学习这种新类型。

现在您将数据标记为:(word, True)(word, False),切换到3个标签的示例是(word, 0)(word, 1)(word, 2)

nltk.NaiveBayesClassifier.prob_classify将返回每个标签的概率。

文档可在此处找到:https://www.nltk.org/api/nltk.classify.html#nltk.classify.naivebayes.NaiveBayesClassifier