|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from __future__ import unicode_literals |
|
3
|
|
|
from processors.utils import is_string, post_json |
|
4
|
|
|
from processors.ds import Sentence, Document |
|
5
|
|
|
from processors.annotators import Message, SegmentedMessage |
|
6
|
|
|
import json |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class SentimentAnalysisAPI(object): |
|
10
|
|
|
""" |
|
11
|
|
|
API for performing sentiment analysis |
|
12
|
|
|
|
|
13
|
|
|
Parameters |
|
14
|
|
|
---------- |
|
15
|
|
|
address : str |
|
16
|
|
|
The base address for the API (i.e., everything preceding `/api/..`) |
|
17
|
|
|
|
|
18
|
|
|
Attributes |
|
19
|
|
|
---------- |
|
20
|
|
|
corenlp : processors.sentiment.CoreNLPSentimentAnalyzer |
|
21
|
|
|
Service using [`CoreNLP`'s tree-based system](https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf) for performing sentiment analysis. |
|
22
|
|
|
|
|
23
|
|
|
""" |
|
24
|
|
|
def __init__(self, address): |
|
25
|
|
|
self._service = address |
|
26
|
|
|
self.corenlp = CoreNLPSentimentAnalyzer(self._service) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class SentimentAnalyzer(object): |
|
30
|
|
|
|
|
31
|
|
|
def __init__(self, address): |
|
32
|
|
|
self._service = "{}/api/sentiment/score".format(address) |
|
33
|
|
|
self._text_service = self._service |
|
34
|
|
|
self._segmented_service = self._service |
|
35
|
|
|
self._sentence_service = self._service |
|
36
|
|
|
self._document_service = self._service |
|
37
|
|
|
|
|
38
|
|
|
def score_document(self, doc): |
|
39
|
|
|
""" |
|
40
|
|
|
Sends a Document to the server for sentiment scoring. |
|
41
|
|
|
|
|
42
|
|
|
Parameters |
|
43
|
|
|
---------- |
|
44
|
|
|
doc : processors.ds.Document |
|
45
|
|
|
The `doc` to be scored |
|
46
|
|
|
|
|
47
|
|
|
Returns |
|
48
|
|
|
------- |
|
49
|
|
|
[int] |
|
50
|
|
|
A list of int scores (one for each sentence) ranging from 1 (very negative) to 5 (very positive) |
|
51
|
|
|
|
|
52
|
|
|
""" |
|
53
|
|
|
try: |
|
54
|
|
|
sentiment_scores = post_json(self._document_service, doc.to_JSON()) |
|
55
|
|
|
return sentiment_scores["scores"] |
|
56
|
|
|
|
|
57
|
|
|
except Exception as e: |
|
58
|
|
|
#print(e) |
|
59
|
|
|
return None |
|
60
|
|
|
|
|
61
|
|
|
def score_sentence(self, sentence): |
|
62
|
|
|
""" |
|
63
|
|
|
Sends a Sentence to the server for sentiment scoring. |
|
64
|
|
|
|
|
65
|
|
|
Parameters |
|
66
|
|
|
---------- |
|
67
|
|
|
sentence : processors.ds.Sentence |
|
68
|
|
|
The `sentence` to be scored |
|
69
|
|
|
|
|
70
|
|
|
Returns |
|
71
|
|
|
------- |
|
72
|
|
|
int |
|
73
|
|
|
A single score ranging from 1 (very negative) to 5 (very positive) |
|
74
|
|
|
|
|
75
|
|
|
""" |
|
76
|
|
|
try: |
|
77
|
|
|
sentiment_scores = post_json(self._sentence_service, sentence.to_JSON()) |
|
78
|
|
|
return sentiment_scores["scores"][0] |
|
79
|
|
|
|
|
80
|
|
|
except Exception as e: |
|
81
|
|
|
print(e) |
|
82
|
|
|
return None |
|
83
|
|
|
|
|
84
|
|
|
def score_segmented_text(self, sentences): |
|
85
|
|
|
""" |
|
86
|
|
|
Sends segmented text to the server for sentiment scoring. |
|
87
|
|
|
|
|
88
|
|
|
Parameters |
|
89
|
|
|
---------- |
|
90
|
|
|
sentences : [str] |
|
91
|
|
|
A list of str representing segmented sentences/chunks to be scored. |
|
92
|
|
|
|
|
93
|
|
|
Returns |
|
94
|
|
|
------- |
|
95
|
|
|
[int] |
|
96
|
|
|
A list of int scores (one for each sentence/chunk) ranging from 1 (very negative) to 5 (very positive) |
|
97
|
|
|
|
|
98
|
|
|
""" |
|
99
|
|
|
try: |
|
100
|
|
|
msg = SegmentedMessage(sentences) |
|
101
|
|
|
sentiment_scores = post_json(self._segmented_service, msg.to_JSON()) |
|
102
|
|
|
return sentiment_scores["scores"] |
|
103
|
|
|
|
|
104
|
|
|
except Exception as e: |
|
105
|
|
|
#print(e) |
|
106
|
|
|
return None |
|
107
|
|
|
|
|
108
|
|
|
def score_text(self, text): |
|
109
|
|
|
""" |
|
110
|
|
|
Sends text to the server for sentiment scoring |
|
111
|
|
|
Returns a list of scores (one for each sentence) |
|
112
|
|
|
""" |
|
113
|
|
|
service = self._text_service |
|
114
|
|
|
try: |
|
115
|
|
|
msg = Message(text) |
|
116
|
|
|
sentiment_scores = post_json(self._text_service, msg.to_JSON()) |
|
117
|
|
|
return sentiment_scores["scores"] |
|
118
|
|
|
|
|
119
|
|
|
except Exception as e: |
|
120
|
|
|
#print(e) |
|
121
|
|
|
return None |
|
122
|
|
|
|
|
123
|
|
|
def score(self, data): |
|
124
|
|
|
""" |
|
125
|
|
|
Sniff out data type and assemble corresponding message to send to the server for sentiment scoring |
|
126
|
|
|
|
|
127
|
|
|
Parameters |
|
128
|
|
|
---------- |
|
129
|
|
|
data : str or [str] or processors.ds.Sentence or processors.ds.Document |
|
130
|
|
|
The data to be scored for sentiment polarity. |
|
131
|
|
|
""" |
|
132
|
|
|
if is_string(data): |
|
133
|
|
|
return self.score_text(data) |
|
134
|
|
|
elif isinstance(data, Sentence): |
|
135
|
|
|
return self.score_sentence(data) |
|
136
|
|
|
elif isinstance(data, Document): |
|
137
|
|
|
return self.score_document(data) |
|
138
|
|
|
# a list of pre segmented sentences |
|
139
|
|
|
elif isinstance(data, list): |
|
140
|
|
|
return self.score_segmented_text(data) |
|
141
|
|
|
else: |
|
142
|
|
|
#print("Type of data: {}".format(type(data))) |
|
143
|
|
|
return None |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
class CoreNLPSentimentAnalyzer(SentimentAnalyzer): |
|
147
|
|
|
""" |
|
148
|
|
|
Bridge to [`CoreNLP`'s tree-based sentiment analysis system](https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf) |
|
149
|
|
|
""" |
|
150
|
|
|
def __init__(self, address): |
|
151
|
|
|
self._service = "{}/api/sentiment/corenlp/score".format(address) |
|
152
|
|
|
self._text_service = self._service |
|
153
|
|
|
self._segmented_service = self._service |
|
154
|
|
|
self._sentence_service = self._service |
|
155
|
|
|
self._document_service = self._service |
|
156
|
|
|
|