0

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.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • Without looking at the documentation, passing a list of dicts of tuples of ... what? as `train_set` looks like a weird thing to do. – tripleee Jan 23 '18 at 18:06
  • @tripleee Do you have any other workaround for this? or any other suggestions to create train_set – Jitesh Mohite Jan 23 '18 at 18:20
  • Not where I can test anything, but this looks like you are adding many complexities without explaining why, and seemingly without a good reason. Can you make this look more like some working demo example? Having your `def` accept a list and then only calling it on one word at a time anyway is *clearly* overcomplicating things, but probably the least of your problems. I'm guessing other parts have similar, but much more convoluted, issues. – tripleee Jan 23 '18 at 18:25
  • It appears to be a duplicate of https://stackoverflow.com/q/48335460/841830 ? At least, you both seem to have started with the same code. Is this from a course? – Darren Cook Jan 24 '18 at 09:15

0 Answers0