I am trying to find sentiment from a word which would be positive or negative. But somehow my code returning always negative sentiment.
from nltk.classify import NaiveBayesClassifier
def word_feats(words):
return dict([(word, True) for word in words])
positive_vocab = ['awesome', 'outstanding', 'fantastic', 'terrific', 'good', 'nice', 'great', ':)', 'liked']
negative_vocab = ['bad', 'terrible', 'useless', 'hate', ':(']
positive_features = [(word_feats(pos), 'pos') for pos in positive_vocab]
negative_features = [(word_feats(neg), 'neg') for neg in negative_vocab]
train_set = positive_features + negative_features
classifier = NaiveBayesClassifier.train(train_set)
# Predict
neg = 0
pos = 0
sentence = "awesome"
sentence = sentence.lower()
words = sentence.split(' ')
for word in words:
print(word)
classResult = classifier.classify(word_feats(word))
print(classResult)
if classResult == 'neg':
neg = neg + 1
if classResult == 'pos':
pos = pos + 1
# if classResult == 'neu':
# neu = neu + 1
print('Positive: ' + str(float(pos) / len(words)))
print('Negative: ' + str(float(neg) / len(words)))
Above code returns result :
Positive: 0.0
Negative: 1.0
Can somebody please help me? Also, I am not getting how internally naive bayes train given set.