Python用于情绪分析

时间:2018-01-14 00:42:58

标签: python nltk sentiment-analysis

我有一个示例代码如下,它使用来自nltk语料库的训练和测试数据并打印出句子的情感。我想做的是用任何文本替换测试数据集。

from nltk.classify import NaiveBayesClassifier
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
from nltk.sentiment.util import *

n_instances = 100

# Each document is represented by a tuple (sentence, label).
# The sentence is tokenized, so it is represented by a list of strings:
subj_docs = [(sent, 'subj') for sent in subjectivity.sents(categories='subj')[:n_instances]]
obj_docs = [(sent, 'obj') for sent in subjectivity.sents(categories='obj')[:n_instances]]

# split subjective and objective instances to keep a balanced uniform class distribution
# in both train and test sets
train_subj_docs = subj_docs[:80]
test_subj_docs = subj_docs[80:100]
train_obj_docs = obj_docs[:80]
test_obj_docs = obj_docs[80:100]
training_docs = train_subj_docs+train_obj_docs
testing_docs = test_subj_docs+test_obj_docs


sentim_analyzer = SentimentAnalyzer()
all_words_neg = sentim_analyzer.all_words([mark_negation(doc) for doc in training_docs])

# simple unigram word features, handling negation
unigram_feats = sentim_analyzer.unigram_word_feats(all_words_neg, min_freq=4)
sentim_analyzer.add_feat_extractor(extract_unigram_feats, unigrams=unigram_feats)

# apply features to obtain a feature-value representation of our datasets
training_set = sentim_analyzer.apply_features(training_docs)
test_set = sentim_analyzer.apply_features(testing_docs)

# train the Naive Bayes classifier on the training set
trainer = NaiveBayesClassifier.train
classifier = sentim_analyzer.train(trainer, training_set)

# output evaluation results
for key,value in sorted(sentim_analyzer.evaluate(test_set).items()):
    print('{0}: {1}'.format(key, value))

因此,当我尝试将testing_docs替换为存储文本的变量时,类似于paragraph = "Hello World, this is a test dataset"。我收到此错误消息ValueError: too many values to unpack (expected 2)

任何人都知道如何解决此错误?谢谢。

1 个答案:

答案 0 :(得分:1)

这是因为testing_docs不是字符串而是元组列表。从示例中打印出testing_docs的值,如果您想将其替换为paragraphs,请确保它使用相同的格式。

如果您想了解所获得的错误,请先阅读并理解元组解包

这个简单的例子复制了它:

>>> a = 'abc'
>>> b,c=a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

这是因为我上面的字符串有三个值,所以要解压缩它我必须将它分配给三个变量(即b,c,d=a工作)。

但是testing_docs实际上与

更相似
a = [
    ('a','subj'),
    ('b','subj'),
    ('c','obj')
]

(虽然我非常怀疑每个元组的第一个元素是单个字符。)

我的猜测是在代码中的某个地方找到了一个循环,试图将testing_docs的值解包为两个变量,所以类似

for val, category in testing_docs:
    ...