【发布时间】:2015-08-03 16:46:00
【问题描述】:
您好,我正在寻找一个程序,该程序将对推文进行正分类和负分类,对已保存在 mongodb 中并且一旦分类的公司的推文进行分类,以根据当时的结果更新整数。
我编写了代码使这成为可能,但我想对程序进行多线程处理,但我在 python 中没有这方面的经验,并且一直在尝试遵循教程但没有运气,因为程序只是启动和退出而没有通过任何代码。
如果有人可以帮助我,将不胜感激。该程序的代码和预期的多线程如下。
from textblob.classifiers import NaiveBayesClassifier
import pymongo
import datetime
from threading import Thread
train = [
('I love this sandwich.', 'pos'),
('This is an amazing place!', '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'),
(':)', 'pos'),
(':(', 'neg'),
('gr8', 'pos'),
('gr8t', 'pos'),
('lol', 'pos'),
('bff', 'neg'),
]
test = [
'The beer was good.',
'I do not enjoy my job',
"I ain't feeling dandy today.",
"I feel amazing!",
'Gary is a friend of mine.',
"I can't believe I'm doing this.",
]
filterKeywords = ['IBM', 'Microsoft', 'Facebook', 'Yahoo', 'Apple', 'Google', 'Amazon', 'EBay', 'Diageo',
'General Motors', 'General Electric', 'Telefonica', 'Rolls Royce', 'Walmart', 'HSBC', 'BP',
'Investec', 'WWE', 'Time Warner', 'Santander Group']
# Create pos/neg counter variables for each company using dicts
vars = {}
for word in filterKeywords:
vars[word + "SentimentOverall"] = 0
# Initialising the classifier
cl = NaiveBayesClassifier(train)
class TrainingClassification():
def __init__(self):
#creating the mongodb connection
try:
conn = pymongo.MongoClient('localhost', 27017)
print "Connected successfully!!!"
global db
db = conn.TwitterDB
except pymongo.errors.ConnectionFailure, e:
print "Could not connect to MongoDB: %s" % e
thread1 = Thread(target=self.apple_thread, args=())
thread1.start()
thread1.join()
print "thread finished...exiting"
def apple_thread(self):
appleSentimentText = []
for record in db.Apple.find():
if record.get('created_at'):
created_at = record.get('created_at')
dt = datetime.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
if record.get('text') and dt > datetime.today():
appleSentimentText.append(record.get("text"))
for targetText in appleSentimentText:
classificationApple = cl.classify(targetText)
if classificationApple == "pos":
vars["AppleSentimentOverall"] = vars["AppleSentimentOverall"] + 1
elif classificationApple == "neg":
vars["AppleSentimentOverall"] = vars["AppleSentimentOverall"] - 1
【问题讨论】:
-
你需要像这样初始化
TrainingClassification.]:TrainingClassification(conn)。不过,我不知道conn是什么。 -
那位是要删除的对不起,我会更新我的问题。
标签: python multithreading mongodb twitter pymongo