コード例 #1
0
ファイル: test.py プロジェクト: gabefair/Opinion-Miner
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)
コード例 #2
0
ファイル: test.py プロジェクト: trunghlt/Opinion-Miner
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)