-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_resources_service.py
More file actions
299 lines (242 loc) · 9.75 KB
/
api_resources_service.py
File metadata and controls
299 lines (242 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import getopt
import tornado.ioloop
import tornado.web
import sys
from api_resources import Resources
from sentiment_analysis import SentimentAnalysis
from solr_service import SolrService
import time
class GetWordInformation(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
word = self.get_argument("word")
pos = self.get_argument("pos", ['v', 'n', 'a', 'r'])
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
if isinstance(pos, list):
response['synsets'] = {}
for postagging in pos:
pos_result = sentiwordnet.get_info_word(word, postagging, language)
if pos_result:
response['synsets'].update(pos_result)
response["elapsed_time"] = time.time() - start
else:
response["synsets"] = sentiwordnet.get_info_word(word, pos, language)
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class Synonym(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
word = self.get_argument("word")
pos = self.get_argument("pos")
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["sentiment"] = sentiwordnet.get_synonym(word, pos, language)
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class GetAffect(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
text = self.get_argument("text").encode('ascii', errors='ignore')
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["affects"] = sentiwordnet.get_affects(text, language)
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class GetDommain(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
text = self.get_argument("text")
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["dommains"] = sentiwordnet.get_domains(text, language)
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class GetSentiment(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
text = self.get_argument("text").encode('ascii', errors='ignore')
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["sentiment"] = sentiwordnet.get_sentiment(text, language)
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class GetSentimentEmotion(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
text = self.get_argument("text")
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
sentiment = SentimentAnalysis(text, language, self.sentiwordnet)
sentiment.analyze()
response = sentiment.tostring()
self.write(response)
else:
# Create DS to save result
response = {}
response.update(sentiwordnet.get_sentiment_and_emotion(text, language))
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class PosTagging(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
text = self.get_argument("text")
language = self.get_argument("language")
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["postagging"] = sentiwordnet.get_postagging(text, language)
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class GetSynsetInformation(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
word = self.get_argument("synset")
pos = self.get_argument("pos")
score = sentiwordnet.get_info(word, pos)
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["info"] = score
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class Translator(tornado.web.RequestHandler):
def initialize(self, sentiwordnet):
self.sentiwordnet = sentiwordnet
def get(self):
start = time.time()
# Check input text
word = self.get_argument("word")
pos = self.get_argument("pos")
from_language = self.get_argument("from_language")
to_language = self.get_argument("to_language")
translation = sentiwordnet.get_translation(word, pos, from_language, to_language)
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
# Create DS to save result
response = {}
response["translation"] = translation
response["elapsed_time"] = time.time() - start
# Return result
self.write(response)
class GetWebMining(tornado.web.RequestHandler):
def initialize(self):
self.solr_service = SolrService()
def get(self):
start = time.time()
domain = self.get_argument("domain")
term = self.get_argument("term")
result = self.solr_service.get_normalized_sentiment_emotion(term, domain)
if self.get_argument("format", None) == "onyx":
self.clear()
self.set_status(400)
self.finish("<html><body>ONYX response not implemented</body></html>")
else:
response = {}
response["result"] = result
response["elapsed_time"] = time.time() - start
self.write(response)
# Check if server por is valid
try:
opts, args = getopt.getopt(sys.argv[1:], "p:", ["port="])
assert (len(opts) == 1)
except:
print 'api_resources_service.py -p <port>'
sys.exit()
for o, a in opts:
if o == "-p":
port = a
else:
pass
if __name__ == "__main__":
# Init treetagger with a specific language
sentiwordnet = Resources()
# Init Tornado web server
application = tornado.web.Application([
(r"/get_synset_information", GetSynsetInformation, dict(sentiwordnet=sentiwordnet)),
(r"/translate", Translator, dict(sentiwordnet=sentiwordnet)),
(r"/get_synonym", Synonym, dict(sentiwordnet=sentiwordnet)),
(r"/get_postagging", PosTagging, dict(sentiwordnet=sentiwordnet)),
(r"/get_sentiment", GetSentiment, dict(sentiwordnet=sentiwordnet)),
(r"/get_affect", GetAffect, dict(sentiwordnet=sentiwordnet)),
(r"/get_dommain", GetDommain, dict(sentiwordnet=sentiwordnet)),
(r"/get_word_information", GetWordInformation, dict(sentiwordnet=sentiwordnet)),
(r"/get_sentiment_emotion", GetSentimentEmotion, dict(sentiwordnet=sentiwordnet)),
(r"/get_web_mining", GetWebMining, {})
])
# Listen on specific port and start server
application.listen(port)
tornado.ioloop.IOLoop.instance().start()