我试图从一个积极或消极的词中找到情绪。但不知何故,我的代码总是回归负面情绪。
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)))
上面的代码返回结果:
正面:0.0
否定:1.0
有人可以帮帮我吗?而且,我没有得到内部朴素的贝叶斯训练如何训练。