【发布时间】:2015-05-10 17:50:31
【问题描述】:
您好,我创建了一个 python 脚本,使用 tweepy 将基于关键字数组的推文流式传输到 mongodb 集合中,该集合基于数组中通过 pymongo 过滤的元素的名称,即(苹果推文保存到苹果集合)。该脚本将它们保存为 JSON 格式,现在我想对这些保存的推文进行情绪分析。
我已经阅读了一些关于这方面的教程,并决定使用 TextBlob 模块中内置的 NaiveBayesClassifier。我创建了一些火车数据并将其传递给分类器(只是一个普通的文本数组,每个元素末尾都有情绪),但我不确定如何将此分类器应用于我已经保存的推文。我认为它如下所示,但这不起作用,因为它会引发错误:
Traceback (most recent call last):
File "C:/Users/Philip/PycharmProjects/FinalYearProject/TrainingClassification.py", line 25, in <module>
cl = NaiveBayesClassifier(train)
File "C:\Python27\lib\site-packages\textblob\classifiers.py", line 192, in __init__
self.train_features = [(self.extract_features(d), c) for d, c in self.train_set]
ValueError: too many values to unpack
到目前为止,这是我的代码:
from textblob.classifiers import NaiveBayesClassifier
import pymongo
train = [
'I love this sandwich.', 'pos',
'I feel very good about these beers.', 'pos',
'This is my best work.', 'pos',
'What an awesome view", 'pos',
'I do not like this restaurant', 'neg',
'I am tired of this stuff.', 'neg',
'I can't deal with this', 'neg',
'He is my sworn enemy!', 'neg',
'My boss is horrible.', 'neg'
]
cl = NaiveBayesClassifier(train)
conn = pymongo.MongoClient('localhost', 27017)
db = conn.TwitterDB
appleSentiment = cl.classify(db.Apple)
print ("Sentiment of Tweets about Apple is " + appleSentiment)
任何帮助将不胜感激。
【问题讨论】:
标签: python-2.7 pymongo textblob