Я работаю над проектом анализа настроений в Python с использованием NLTK. Выходные данные проекта должны показать, является ли данное утверждение положительным или отрицательным. Мне удалось это сделать, но как я могу получить вывод для нейтрального утверждения?
И возможно ли выводить в виде процентов (т. Е. Положительного%, отрицательного% или нейтрального%)?
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)]