forked from heckej/P-O-Entrepreneurship-Team-A-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudo_nlp.py
51 lines (46 loc) · 1.75 KB
/
pseudo_nlp.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
from cluster import connector
import json
import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def main():
con = connector.Connector("ws://localhost:39160/api/NLP/WS")
while True:
try:
task = con.get_next_task()
print(task)
sent = False
while not sent:
print(con._tasks_in_progress)
reply = generate_response(task)
print("Response: " + str(reply))
try:
if reply != "" and len(reply) > 0:
con.reply(reply)
sent = True
except TypeError as e:
print("Could not process input. TypeError: ")
print(e)
except json.JSONDecodeError as e:
print("Could not process input. JSONDecodeError")
print(e)
except KeyboardInterrupt:
con.close()
break
def generate_response(task):
response = dict()
try:
response['msg_id'] = task['msg_id']
if task['action'] == connector.Actions.ESTIMATE_OFFENSIVENESS.value:
response['prob'] = 0
response['sentence_id'] = task['sentence_id']
elif task['action'] == connector.Actions.IS_NONSENSE.value:
response['nonsense'] = False
response['sentence_id'] = task['sentence_id']
elif task['action'] == connector.Actions.MATCH_QUESTIONS.value:
response['possible_matches'] = list()
for question in task['compare_questions']:
response['possible_matches'].append({'question_id': question['question_id'], 'prob': 1})
except KeyError as ex:
print(ex)
return response
main()