-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatdb.py
230 lines (177 loc) · 6.26 KB
/
chatdb.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
# -*- coding: utf-8 -*-
'Chat数据库'
__author__='litterzhang'
import os
import copy
from model.answer import Answer
from model.question import Question
from model.ans2que import Ans2Que
from WordSegment.segs import bmseg
import ChatUtils
answers = list()
questions = list()
ans2ques = list()
ids = list([0, 0])
def init_db(key):
global answers, questions, ans2ques, ids
ANSWER = os.path.join(os.path.dirname(__file__), 'data/answer_%s.json' % key)
QUESTION = os.path.join(os.path.dirname(__file__), 'data/question_%s.json' % key)
ANS2QUE = os.path.join(os.path.dirname(__file__), 'data/ans2que_%s.json' % key)
IDS = os.path.join(os.path.dirname(__file__), 'data/ids_%s' % key)
if os.path.exists(ANSWER):
answers = Answer.load(ANSWER)
if os.path.exists(QUESTION):
questions = Question.load(QUESTION)
if os.path.exists(ANS2QUE):
ans2ques = Ans2Que.load(ANS2QUE)
if os.path.exists(IDS):
with open(IDS, 'r', encoding='utf-8') as fr:
ids = [int(x) for x in fr.readline().strip().split()]
print('\n----------Debug---------\n')
for question in questions:
print('question : %s ' % question.toString())
for answer in answers:
print('answer : %s ' % answer.toString())
for ans2que in ans2ques:
print('ans2que : %s ' % ans2que.toString())
print('\n----------Debug---------\n')
def save_db(key):
global answers, questions, ans2ques, ids
ANSWER = os.path.join(os.path.dirname(__file__), 'data/answer_%s.json' % key)
QUESTION = os.path.join(os.path.dirname(__file__), 'data/question_%s.json' % key)
ANS2QUE = os.path.join(os.path.dirname(__file__), 'data/ans2que_%s.json' % key)
IDS = os.path.join(os.path.dirname(__file__), 'data/ids_%s' % key)
# print('\n----------Debug---------\n')
# for question in questions:
# print('question : %s ' % question.toString())
# for answer in answers:
# print('answer : %s ' % answer.toString())
# for ans2que in ans2ques:
# print('ans2que : %s ' % ans2que.toString())
# print('\n----------Debug---------\n')
Answer.dump(answers, ANSWER)
Question.dump(questions, QUESTION)
Ans2Que.dump(ans2ques, ANS2QUE)
with open(IDS, 'w', encoding='utf-8') as fw:
fw.write('%s %s' % (str(ids[0]), str(ids[1])))
def match_question(que_str):
global answers, questions, ans2ques, ids
# print('\n----------Debug---------\n')
# for question in questions:
# print('question : %s ' % question.toString())
# for answer in answers:
# print('answer : %s ' % answer.toString())
# for ans2que in ans2ques:
# print('ans2que : %s ' % ans2que.toString())
# print('\n----------Debug---------\n')
sims = list()
que_words = [list(x) for x in bmseg.seg(que_str)]
que_words = list(filter(lambda x: x[2]!='bd', que_words))
for que in questions:
sims.append(ChatUtils.sim_calc(que_words, que.words))
if not sims:
return None, 0
sim_max = max(sims)
que_mat = questions[sims.index(sim_max)]
return que_mat, sim_max
def get_ans_by_que(que):
global answers, questions, ans2ques, ids
ans2que = Ans2Que.get_ans_by_que(ans2ques, que.id)
anss = list()
if ans2que:
for ans_id in ans2que.ans_ids:
ans = Answer.get_ans_by_id(answers, ans_id['id'])
if ans:
anss.append({'ans' : ans, 'score' : ans_id['score']})
return anss
def get_answer(que_str):
global answers, questions, ans2ques, ids
que, sim = match_question(que_str)
if not que:
return None, 0, list()
anss = get_ans_by_que(que)
# print('相似度: %s\n' % sim)
return que, sim, anss
def que_new_id():
global answers, questions, ans2ques, ids
ids[0] += 1
return ids[0]
def ans_new_id():
global answers, questions, ans2ques, ids
ids[1] += 1
return ids[1]
def que_new(que_str, que_type=2):
global answers, questions, ans2ques, ids
que_id = que_new_id()
que = Question(que_id, que_type, que_str, bmseg.seg(que_str))
questions.append(que)
# print('\n----------Debug---------\n')
# for question in questions:
# print('question : %s ' % question.toString())
# for answer in answers:
# print('answer : %s ' % answer.toString())
# for ans2que in ans2ques:
# print('ans2que : %s ' % ans2que.toString())
# print('\n----------Debug---------\n')
return que
def ans_new(ans_str, ans_type=2, ans_seed=-1, ans_deg=0):
global answers, questions, ans2ques, ids
ans_id = ans_new_id()
ans = Answer(ans_id, ans_type, ans_seed, ans_deg, ans_str, bmseg.seg(ans_str))
answers.append(ans)
return ans
def ans_new_and_match_que(que_id, ans_str, ans_type=2, ans_seed=-1, ans_deg=0, score=10):
global answers, questions, ans2ques, ids
ans = ans_new(ans_str, ans_type, ans_seed, ans_deg)
match = False
for ans2que in ans2ques:
if ans2que.que_id==que_id:
ans2que.ans_ids.append({'id' : ans.id, 'score' : score})
match = True
break
if not match:
ans2ques.append(Ans2Que(que_id, [{'id' : ans.id, 'score' : score}, ]))
# print('\n----------Debug---------\n')
# for question in questions:
# print('question : %s ' % question.toString())
# for answer in answers:
# print('answer : %s ' % answer.toString())
# for ans2que in ans2ques:
# print('ans2que : %s ' % ans2que.toString())
# print('\n----------Debug---------\n')
def match_old_anss(que_new_id, que_old_id):
global answers, questions, ans2ques, ids
ans2que = Ans2Que.get_ans_by_que(ans2ques, que_old_id)
if ans2que:
ans2que_new = Ans2Que(que_new_id, copy.deepcopy(ans2que.ans_ids))
ans2ques.append(ans2que_new)
# print('\n----------Debug---------\n')
# for question in questions:
# print('question : %s ' % question.toString())
# for answer in answers:
# print('answer : %s ' % answer.toString())
# for ans2que in ans2ques:
# print('ans2que : %s ' % ans2que.toString())
# print('\n----------Debug---------\n')
def change_score(que_id, ans_id, score):
global answers, questions, ans2ques, ids
if score!=0:
for ans2que in ans2ques:
if ans2que.que_id==que_id:
print(ans2que.toString())
for ans_score in ans2que.ans_ids:
if ans_score['id']==ans_id:
ans_score['score'] += score
break
break
# print('\n----------Debug---------\n')
# for question in questions:
# print('question : %s ' % question.toString())
# for answer in answers:
# print('answer : %s ' % answer.toString())
# for ans2que in ans2ques:
# print('ans2que : %s ' % ans2que.toString())
# print('\n----------Debug---------\n')
if __name__=='__main__':
que, sim = match_question('吃了吗??')
anss = get_ans_by_que(que)