def __init__(self, id, workQueue, total, cm): self.id = id self.workQueue = workQueue self.total = total self.cm = cm self.client = Client(config="../server.cfg", enable_timer=True) Thread.__init__(self)
class MyThread(Thread): def __init__(self, id, workQueue, total, cm): self.id = id self.workQueue = workQueue self.total = total self.cm = cm self.client = Client(config="../server.cfg", enable_timer=True) Thread.__init__(self) def test_sent(self, return_confidence=True, confidence_type = Confidence.LOCAL, confidence_threshold=CONFIDENCE_THRESHOLD): """ Test sentiment scoring ``return_confidence``: whether or not the sentiment client computes confidence score for each prediction. ``confidence_threshold``: confidence threshold for building confusion matrix. Default value is %f. """ % (CONFIDENCE_THRESHOLD) for work in iter(self.workQueue.get, "STOP"): data = work["data"] id = work["id"] real_sent, review = int(data[0]), data[1] svm = self.client.score(review, return_confidence, confidence_type) if not return_confidence or svm["confidence"] > confidence_threshold: self.total[real_sent] += 1 self.cm[real_sent][int(svm["score"])] += 1 if id % 1000 == 0: print id def test_probs(self): for work in iter(self.workQueue.get, "STOP"): data = work["data"] id = work["id"] real_sent, review = data[0], data[1] svm = self.client.score(review) predicted_sent = svm["score"] max_prob_sent = max(zip(svm["probs"].keys(), svm["probs"].values()), key = lambda x: x[1]) raise Exception("Predicted score is different from the score with " "maximum probability: (%d, %d)"\ % (predicted_sent, max_prob_sent)) def run(self): self.test_sent(return_confidence=False, confidence_type=Confidence.LOCAL)
class MyThread(Thread): def __init__(self, id, workQueue, total, cm): self.id = id self.workQueue = workQueue self.total = total self.cm = cm self.client = Client(config="../server.cfg", enable_timer=True) Thread.__init__(self) def test_sent( self, return_confidence=True, confidence_type=Confidence.LOCAL, confidence_threshold=CONFIDENCE_THRESHOLD ): """ Test sentiment scoring ``return_confidence``: whether or not the sentiment client computes confidence score for each prediction. ``confidence_threshold``: confidence threshold for building confusion matrix. Default value is %f. """ % ( CONFIDENCE_THRESHOLD ) for work in iter(self.workQueue.get, "STOP"): data = work["data"] id = work["id"] real_sent, review = int(data[0]), data[1] svm = self.client.score(review, return_confidence, confidence_type) if not return_confidence or svm["confidence"] > confidence_threshold: self.total[real_sent] += 1 self.cm[real_sent][int(svm["score"])] += 1 if id % 1000 == 0: print id def test_probs(self): for work in iter(self.workQueue.get, "STOP"): data = work["data"] id = work["id"] real_sent, review = data[0], data[1] svm = self.client.score(review) predicted_sent = svm["score"] max_prob_sent = max(zip(svm["probs"].keys(), svm["probs"].values()), key=lambda x: x[1]) raise Exception( "Predicted score is different from the score with " "maximum probability: (%d, %d)" % (predicted_sent, max_prob_sent) ) def run(self): self.test_sent(return_confidence=False, confidence_type=Confidence.LOCAL)
import os.path import tornado.ioloop import tornado.web import tornado.httpserver from twitter import Twitter from data_processing import utils import config import credential import demjson import memcache import lxml.html from servers.client import Client, Confidence from tornado.options import define, options import tornado.database SAClient = Client(config=config.SENTIMENT["config"], enable_timer=False) Mem = memcache.Client([config.MEMCACHE["address"]], debug=0) def retrieve_items(q, rpp=5, since_id=0): items = Twitter.search(q, rpp, since_id) if len(items) > 0: Mem.set("last_tweet_id", items[0]["id"]) filtered_items = [] for item in items: filtered_text = Twitter.clean(item["text"], q) score = SAClient.score( filtered_text.encode("utf-8"), return_confidence=True, confidence_type=Confidence.NNP, )
import argparse from servers.client import Client if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('host', type=str) parser.add_argument('port', type=int) parser.add_argument('request', type=str) args = parser.parse_args() try: client = Client(args.host, args.port) client.send_message(args.request) except Exception as ex: print("Couldn't make the client call because:") print(ex)