-
Notifications
You must be signed in to change notification settings - Fork 1
/
topic_service.py
68 lines (56 loc) · 2.29 KB
/
topic_service.py
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
# -*- coding: utf-8 -*-
import json
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
import logging
import sys
from urllib.parse import unquote_plus
from taxonomy import taxonomy as taxonomy
from classification import Classificator
class ClassificationService(RequestHandler):
def initialize(self):
self.default_classificator = Classificator(taxonomy)
def get(self):
logging.debug("Received GET request")
service_id = self.get_argument('service_id', default=None)
text = self.get_argument('text')
lang = self.get_argument('lang', default=None)
taxonomy = self.get_argument('taxonomy', default=None)
if taxonomy:
taxonomy = json.loads(taxonomy)
classificator = Classificator(taxonomy)
else:
classificator = self.default_classificator
classification = classificator.classify(text)
logging.debug(classification)
result = self.__get_concepts_from_classification(classification)
if result:
self.write(json.dumps(result))
else:
self.write(json.dumps(['N/A']))
def post(self):
results = list()
logging.debug("Received POST request")
for line in str(self.request.body, 'utf8').split('\n'):
fields = line.split('\t')
text = unquote_plus(unquote_plus(fields[0]))
logging.debug("Classificating %s" % text)
classification = self.default_classificator.classify(text)
result = {"text":text, "topics":self.__get_concepts_from_classification(classification)}
results.append(result)
self.write({"response":results})
def __get_concepts_from_classification(self, classification):
unique_concepts = set()
for word, concepts in classification.items():
unique_concepts|=set(concepts)
return list(unique_concepts)
def __format_post_classification(self, classification):
concepts = self.__get_concepts_from_classification(classification)
if concepts:
return ';;'.join(concepts)
else:
return 'N/A'
print('Topic classification service started')
app = Application([(r'/', ClassificationService)])
app.listen(sys.argv[1])
IOLoop.instance().start()