Completed
Push — master ( 3de2d7...fcfe03 )
by Gus
01:06
created

SentimentAnalyzer.score()   A

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
from .utils import post_json
4
from .ds import Sentence, Document
5
import json
6
7
8
class SentimentAnalysisAPI(object):
9
    """
10
    API for performing sentiment analysis
11
    """
12
    def __init__(self, address):
13
        self._service = address
14
        self.corenlp = CoreNLPSentimentAnalyzer(self._service)
15
16
17
class SentimentAnalyzer(object):
18
19
    def __init__(self, address):
20
        self._service = "{}/sentiment".format(address)
21
22
    def score_document(self, doc):
23
        """
24
        Sends a Document to the server for sentiment scoring
25
        Returns a list of scores (one for each sentence)
26
        """
27
        service = "{}/document".format(self._service)
28
        try:
29
            sentiment_scores = post_json(service, doc.to_JSON())
30
            return sentiment_scores["scores"]
31
32
        except Exception as e:
33
            print(e)
34
            return None
35
            #raise Exception("Connection refused!  Is the server running?")
36
37
    def score_sentence(self, sentence):
38
        """
39
        Sends a Sentence to the server for sentiment scoring
40
        Returns a single score
41
        """
42
        service = "{}/sentence".format(self._service)
43
        try:
44
            sentiment_scores = post_json(service, sentence.to_JSON())
45
            return sentiment_scores["scores"][0]
46
47
        except Exception as e:
48
            print(e)
49
            return None
50
            #raise Exception("Connection refused!  Is the server running?")
51
52
    def score_text(self, text):
53
        """
54
        Sends text to the server for sentiment scoring
55
        Returns a list of scores (one for each sentence)
56
        """
57
        service = "{}/text".format(self._service)
58
        try:
59
            sentiment_scores = post_json(service, json.dumps({"text":"{}".format(text)}))
60
            return sentiment_scores["scores"]
61
62
        except Exception as e:
63
            print(e)
64
            return None
65
            #raise Exception("Connection refused!  Is the server running?")
66
67
    def score(self, data):
68
        """
69
        Sniff out data type and to properly send to the server for sentiment scoring
70
        """
71
        if isinstance(data, str):
72
            return self.score_text(data)
73
        elif isinstance(data, Sentence):
74
            return self.score_sentence(data)
75
        elif isinstance(data, Document):
76
            return self.score_document(data)
77
        else:
78
            return None
79
80
81
class CoreNLPSentimentAnalyzer(SentimentAnalyzer):
82
    """
83
    Bridge to CoreNLP's tree-based sentiment analysis
84
    """
85
    def __init__(self, address):
86
        self._service = "{}/corenlp/sentiment".format(address)
87