def handle(self): global INDEX request = receive_json(self.request) start = time.time() if request['action'] == 'load': INDEX = Index(request['path']) self.request.sendall(json.dumps(INDEX.storage).encode()) elif request['action'] == 'create': INDEX.create(request['data']) elif request['action'] == 'add': INDEX.update(request['key'], request['value']) elif request['action'] == 'update': INDEX.update(request['key'], request['value']) elif request['action'] == 'delete': INDEX.delete(request['key']) elif request['action'] == 'get': self.request.sendall(INDEX.get(request['key']).encode()) else: self.request.sendall( json.dumps({ 'action': 'error', 'message': 'Invalid action.' }).encode()) print('Processed action "%s" in %.2f seconds' % (request['action'], time.time() - start))
def handle(self): global ADVISER request = utils.receive_json(self.request) start = time.time() if request['action'] == 'build': success = True # noinspection PyBroadException try: ADVISER = Adviser(request['path']) except Exception: success = False self.request.sendall(json.dumps({ 'action': 'report', 'success': success }).encode()) elif not ADVISER: self.request.sendall(json.dumps({ 'action': 'error', 'message': '"build" action required before any other action' }).encode()) elif request['action'] == 'fit': ADVISER.fit(request['token'], request['document']) elif request['action'] == 'suggest': self.request.sendall(ADVISER.suggest(request['token']).encode()) else: self.request.sendall(json.dumps({ 'action': 'error', 'message': 'Invalid action.' }).encode()) print('Processed action "%s" in %.2f seconds' % (request['action'], time.time() - start))
def handle(self): global MODEL, ACTIVE_MODEL request = utils.receive_json(self.request) start = time.time() if request['action'] == 'set_model': model = request['model'] if model in MODELS: ACTIVE_MODEL = request['model'] MODEL = MODELS[ACTIVE_MODEL]() else: self.request.sendall(json.dumps({ 'action': 'error', 'message': 'Incorrect model selected.' }).encode()) elif request['action'] == 'get_model': self.request.sendall(json.dumps({ 'action': 'report', 'model': ACTIVE_MODEL }).encode()) elif request['action'] == 'build': success = True # noinspection PyBroadException try: MODEL.build(request['path']) except Exception: success = False self.request.sendall(json.dumps({ 'action': 'report', 'success': success }).encode()) elif request['action'] == 'query': self.request.sendall( MODEL.query( request['token'], request['query'], request['count'], request['continuation'] ).encode() ) elif request['action'] == 'update_query': self.request.sendall( MODEL.update_query(request['token'], request['document'], request['positive'], request['count']).encode() ) else: self.request.sendall(json.dumps({ 'action': 'error', 'message': 'Invalid action.' }).encode()) print('Processed action "%s" in %.2f seconds' % (request['action'], time.time() - start))
def handle(self): request = receive_json(self.request) start = time.time() if request['action'] == 'process': text = request['data'] is_query = request.get('is_query', False) self.request.sendall(process(text, is_query).encode()) else: self.request.sendall(json.dumps({ 'action': 'error', 'message': 'Invalid action.' }).encode()) print('Processed action "%s" in %.2f seconds' % (request['action'], time.time() - start))
def handle(self): global SUMMARY request = utils.receive_json(self.request) start = time.time() if request['action'] == 'build': success = True # noinspection PyBroadException try: SUMMARY = Summary(request['path']) except Exception: success = False self.request.sendall( json.dumps({ 'action': 'report', 'success': success }).encode()) elif not SUMMARY: self.request.sendall( json.dumps({ 'action': 'error', 'message': '"build" action required before any other action' }).encode()) elif request['action'] == 'get': self.request.sendall(SUMMARY.get().encode()) else: self.request.sendall( json.dumps({ 'action': 'error', 'message': 'Invalid action.' }).encode()) print('Processed action "%s" in %.2f seconds' % (request['action'], time.time() - start))