-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathclassify.py
66 lines (55 loc) · 1.87 KB
/
classify.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
import sys
sys.path.append("..")
from sys import argv
# from IMQ import IMessageQueue
import json
import tools.random_helper as random_helper
from utils.MQ_tools import RabbitMQTool
from mq_client import MQClient
from tools.word_cut import WordCutHelper
class Classify_CN(MQClient):
# def __init__(self, port_num):
# print('init cnn classify')
# self.url = "http://localhost:"+str(port_num)+"/"
# def getCategory(self, sentence):
# url = self.url+"s/"+quote(sentence.replace(' ','|'))
# result = urequest.request_url(url)
# if result:
# #print(result)
# return result
# return {}
def __init__(self, field):
MQClient.__init__(self)
self.field = field
prefix = 'nlp.classify.cnn.'+field+'.request.'
publish_key = prefix + random_helper.random_string()
mqTool = RabbitMQTool()
queue = prefix + '#'
status = mqTool.get_queue_status(queue)
if status:
receive_key = publish_key.replace('request', 'reply')
# self.mq = IMessageQueue(receive_key, publish_key, receive_key, receive_key, '')
self.create_connection(
receive_key, publish_key, receive_key, receive_key, '')
else:
print('{} is not found'.format(queue))
def getCategory(self, sentence):
if not sentence or not self.mq:
return {}
result = self.mq.request_synchronize(sentence)
if result:
return json.loads(result)
return {}
if __name__ == '__main__':
# script, field = argv
field = 'banktest'
cc = Classify_CN(field)
wh = WordCutHelper(1)
while 1:
s = input('input: ')
# 分词
value = wh.getTagAndWord(s)['word']
sentence = ' '.join(value)
print(sentence)
result = cc.getCategory(sentence)
print(result)