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