forked from shinezai/QASystemOnFinancialKG
-
Notifications
You must be signed in to change notification settings - Fork 1
/
answer_search.py
279 lines (237 loc) · 14.2 KB
/
answer_search.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
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
#!/usr/bin/env python3
# coding: utf-8
# File: answer_search.py
# Author: https://github.com/shinezai
# Date: 19-02
from graph_database import GRAPH_DB_CONN
class AnswerSearcher:
def __init__(self):
self.g = GRAPH_DB_CONN
self.num_limit = 20
'''执行cypher查询,并返回相应结果'''
def search_main(self, sqls):
final_answers = []
for sql_ in sqls:
question_type = sql_['question_type']
queries = sql_['sql']
answers = []
for query in queries:
ress = self.g.run(query).data()
answers += ress
print(answers)
final_answer = self.answer_prettify(question_type, answers)
if final_answer:
final_answers.append(final_answer)
return final_answers
'''根据对应的qustion_type,调用相应的回复模板'''
def answer_prettify(self, question_type, answers):
final_answer = []
if not answers:
return ''
elif question_type == 'equityscale_concept_stockget':
desc = []
elif question_type == 'stockid_conceptget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的所属概念是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_conceptget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的所属概念是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'concept_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '属于{0}概念的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_controllerget':
desc = [i['n.name'] + '(' + i['n.type'] + ')' for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的实际控制人是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_controllerget':
desc = [i['n.name'] + '(' + i['n.type'] + ')' for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的实际控制人是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'controller_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
subject = answers[0]['n.name']
final_answer = '{0}作为实际控制人的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'conceptleading_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '属于{0}概念的龙头股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_industryget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的所属行业是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_industryget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的所属行业是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'industry_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
subject = answers[0]['n.name']
final_answer = '属于{0}行业的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_indextypeget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '将{0} {1}纳入的指数类有:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_indextypeget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '将{0} {1}纳入的指数类有:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'indextype_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '纳入{0}的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_equityscaleget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
subject3 = answers[0]['m.capital_total']
subject4 = answers[0]['m.capital_flow']
final_answer = '{0} {1}是一支{2},总股本为{3}股,A股流通{4}股'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]), subject3, subject4)
elif question_type == 'stockname_equityscaleget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
subject3 = answers[0]['m.capital_total']
subject4 = answers[0]['m.capital_flow']
final_answer = '{0} {1}是一支{2},总股本为{3}股,A股流通{4}股'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]), subject3, subject4)
elif question_type == 'equityscale_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
subject = answers[0]['n.name']
final_answer = '属于{0}的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_markettypeget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的市场类型是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_markettypeget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的市场类型是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'markettype_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '属于{0}的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_buysignalget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}释放的买入信号有:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_buysignalget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}释放的买入信号有:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'buysignal_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '释放出{0}买入信号的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_sellsignalget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}释放的卖出入信号有:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_sellsignalget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}释放的卖出信号有:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'sellsignal_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '释放出{0}卖出信号的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_techformget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}表现出的技术形态是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_techformget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}表现出的技术形态是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'techform_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '表现出{0}技术形态的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_movementget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '参考{0} {1}的这些指标:{2},再决定是否建仓吧!'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_movementget':
desc = [i['n.name'] for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '参考{0} {1}的这些指标:{2},再决定是否建仓吧!'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'movement_stockget':
desc = [i['m.stock_id'] + ' ' + i['m.stock_name'] for i in answers]
length = len(answers[0])
list_subject = []
if length > 2:
for i in range(2, length):
list_subject.append(answers[0]['n#i#.name'.replace('#i#', str(i-2))])
subject = "、".join(list_subject)
final_answer = '{0}的股票有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockid_scoreget':
desc = [str(i['m.score']) for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的诊股得分是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'stockname_scoreget':
desc = [str(i['m.score']) for i in answers]
subject1 = answers[0]['m.stock_id']
subject2 = answers[0]['m.stock_name']
final_answer = '{0} {1}的诊股得分是:{2}'.format(subject1, subject2, ';'.join(list(set(desc))[:self.num_limit]))
return final_answer
if __name__ == '__main__':
searcher = AnswerSearcher()
#g = Graph("bolt://127.0.0.1:7687", user="neo4j", password="stayalive")
#query = "MATCH (m:Stock20190111)-[r:ConceptLeadingInvolved]->(n:ConceptLeading) where n.name = '融资融券' return m.stock_name,n.name"
#ress = g.run(query).data()
#print(ress)