-
Notifications
You must be signed in to change notification settings - Fork 291
/
comp_sql.py
380 lines (302 loc) · 14.3 KB
/
comp_sql.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# -*- coding: utf-8 -*-
# @Time : 2021/6/30 10:41 AM
# @Author : yuchen
# @FileName: comp_sql.py
# @Software: PyCharm
import numpy as np
import os
import pandas as pd
import pickle
import copy
## syn, syn_product, unit, konglie
from collections import Counter
class Syn():
def __init__(self, args):
self.all_syns_dict, self.col_scripts_dict, self.jianyu_syns_dict, self.m2e_syns_dict, self.crowd_syns_dict, self.crowd_scripts_dict, self.crowd_shifou_dict = self.load_syns_scripts()
table_words = self.loadTableWords()
self.args = args
def load_syns_scripts(self):
zh_dir = './zh_kb_syn_dict'
with open(os.path.join(zh_dir, 'all_syns.pickle'), 'rb') as f:
all_syns_dict = pickle.load(f)
with open(os.path.join(zh_dir, 'm2e_syns0125.pickle'), 'rb') as f:
m2e_syns_dict = {}
with open(os.path.join(zh_dir, 'col_scripts.pickle'), 'rb') as f:
col_scripts_dict = pickle.load(f)
with open(os.path.join(zh_dir, 'jianyu_syns.pickle'), 'rb') as f:
jianyu_syns_dict = pickle.load(f)
with open(os.path.join(zh_dir, 'syn_from_crowd0526.pickle'), 'rb') as f:
crowd_syns_dict = pickle.load(f)
with open(os.path.join(zh_dir, 'scripts_from_crowd0524.pickle'), 'rb') as f:
crowd_scripts_dict = pickle.load(f)
with open(os.path.join(zh_dir, 'shifou_from_crowd0524.pickle'), 'rb') as f:
crowd_shifou_dict = pickle.load(f)
# print('scropts:')
# for k, v in col_scripts_dict.items():
# print(k, v)
return all_syns_dict, col_scripts_dict, jianyu_syns_dict, m2e_syns_dict, crowd_syns_dict, crowd_scripts_dict, crowd_shifou_dict
def loadTableWords(self):
value_name_path = '/Users/yuchen/Documents/Workroom1/product_taccs/data/car/benz/benz_value_name.csv'
value_name_path = os.path.join(self.args.data_dir, self.args.table_words)
if os.path.exists(value_name_path):
star_words = pd.read_table(value_name_path, header=0)
# print(star_words.head())
rowx, y = star_words.shape
for idx in range(rowx):
star_words.loc[idx]['归一化列值'] = str(star_words.loc[idx]['归一化列值']).replace(' ', '')
else:
star_words = None
return star_words
def col_val_syn(table, table_words, col_index, val):
# table = self.tables[tableId]
val = str(val)
headers = table['header']
cond_col = headers[col_index]
cond_value_synonmys = []
cond_value_synonmys.append(val)
cond_value_synonmys.append(val.lower())
cond_value_synonmys.append(val+'的')
if table_words is None:
return cond_value_synonmys
## 单位
sel_unit = table['unit'][col_index]
if sel_unit != 'Null' and sel_unit != "":
sel_units = str(sel_unit).split('|')
# sel_uniti = random.choice(sel_units)
for sel_uniti in sel_units:
value_unit = str(val) + sel_uniti
cond_value_synonmys.append(value_unit)
for index, row in table_words.iterrows():
if row['列名'] == cond_col and str(row['归一化列值']) == val and (pd.isnull(row['同义词']) == False):
cond_value_synonmys += row['同义词'].split('|')
return cond_value_synonmys
def com_conds(gold_sql, pr_sql, tablei, table_words):
# print('tablei:', tablei)
gold_conds = gold_sql['conds'].tolist()
pre_conds1 = copy.deepcopy(pr_sql['conds'])
# print('gold conds:', gold_conds)
pre_conds = []
for x in pre_conds1:
pre_idx, pre_op, pre_val = x[:3]
pre_val = str(pre_val)
if pre_idx < len(tablei['header']) and tablei['types'][pre_idx] in ['text', 'bool']:
if pre_op not in [2, 3]:
x[1] = 2
if x not in pre_conds:
pre_conds.append(x)
# print('pre conds:', pre_conds)
if len(gold_conds) != len(pre_conds):
return False
## step 1: null cond:
if len(gold_conds) == 1 and int(gold_conds[0][0]) == len(tablei['header']):
# for pre_condi in pre_conds:
# if pre_condi[0] == len(tablei['header']):
# return True
if len(pre_conds) == 1 and pre_conds[0][0] == len(tablei['header']):
return True
else:
return False
else:
## step2 : 没有空条件,考虑同义词和单位
for gold_condsi in gold_conds:
# print('condi:', gold_condsi)
cond_idx, cond_op, val, val_syn = gold_condsi[:4]
cond_idx, cond_op = int(cond_idx), int(cond_op)
val = str(val)
val_syn = str(val_syn)
table_syns = col_val_syn(tablei, table_words, int(gold_condsi[0]), gold_condsi[2])
# print('table syns:', table_syns)
# print('kb syns:', kb_syns)
all_cond_syns = table_syns + [val_syn]
all_cond_syns_low = [x.lower() for x in all_cond_syns]
all_cond_syns += all_cond_syns_low
find_flag = False
for pre_condi in pre_conds:
pre_idx, pre_op, pre_val = pre_condi[:3]
pre_val = str(pre_val)
pre_idx, pre_op = int(pre_idx), int(pre_op)
val_right = False
for val_syni in all_cond_syns:
if val_syni in pre_val or pre_val in val_syni or pre_val == val_syni:
val_right = True
if pre_idx == cond_idx and pre_op == cond_op and (pre_val in all_cond_syns or val in pre_val or val_right):
find_flag = True
if not find_flag:
return False
return True
def com_sels(gold_sql, pr_sql, tablei, table_words):
# print('tablei:', tablei)
gold_sels = gold_sql['sel'].tolist()
gold_aggs = gold_sql['agg'].tolist()
pre_sels = pr_sql['sel']
pre_aggs = pr_sql['agg']
if len(gold_sels) != len(pre_sels):
return False
if len(gold_sels) == 1 and gold_sels[0] == len(tablei['header']):
if len(pre_sels) == 1 and pre_sels[0] == len(tablei['header']):
return True
else:
return False
else:
gold_sel_aggs = zip(gold_sels, gold_aggs)
pre_sel_aggs = zip(pre_sels, pre_aggs)
for idx, gold_sel_aggi in enumerate(gold_sel_aggs):
if gold_sel_aggi in pre_sel_aggs:
pass
else:
return False
return True
def com_conds_final(gold_sql, pr_sql, tablei, table_words):
# print('tablei:', tablei)
gold_conds = gold_sql['conds']
pre_conds1 = copy.deepcopy(pr_sql['conds'])
# print('gold conds:', gold_conds)
pre_conds = []
for x in pre_conds1:
pre_idx, pre_op, pre_val = x[:3]
if pre_idx < len(tablei['header']) and tablei['types'][pre_idx] in ['text', 'bool']:
if pre_op not in [2, 3]:
x[1] = 2
if x not in pre_conds:
pre_conds.append(x)
# print('pre conds:', pre_conds)
if len(gold_conds) != len(pre_conds):
return False
## step 1: null cond:
if len(gold_conds) == 1 and int(gold_conds[0][0]) == len(tablei['header']):
# for pre_condi in pre_conds:
# if pre_condi[0] == len(tablei['header']):
# return True
if len(pre_conds) == 1 and pre_conds[0][0] == len(tablei['header']):
return True
else:
return False
else:
## step2 : 没有空条件,考虑同义词和单位
for gold_condsi in gold_conds:
# print('condi:', gold_condsi)
cond_idx, cond_op, val, val_syn = gold_condsi[:4]
cond_idx = int(cond_idx)
if cond_idx < len(tablei['header']):
val = str(val)
val_syn = str(val_syn)
cond_idx, cond_op = int(cond_idx), int(cond_op)
table_syns = col_val_syn(tablei, table_words, int(gold_condsi[0]), gold_condsi[2])
# print('table syns:', table_syns)
# print('kb syns:', kb_syns)
all_cond_syns = table_syns + [val_syn]
all_cond_syns_low = [x.lower() for x in all_cond_syns]
all_cond_syns += all_cond_syns_low
find_flag = False
for pre_condi in pre_conds:
pre_idx, pre_op, pre_val = pre_condi[:3]
pre_val = str(pre_val)
pre_idx, pre_op = int(pre_idx), int(pre_op)
val_right = False
for val_syni in all_cond_syns:
if val_syni in pre_val or pre_val in val_syni or pre_val == val_syni:
val_right = True
if pre_idx == cond_idx and pre_op == cond_op and (pre_val in all_cond_syns or val in pre_val or val_right):
find_flag = True
if not find_flag:
return False
return True
def com_sels_final(gold_sql, pr_sql, tablei, table_words):
# print('tablei:', tablei)
gold_sels = gold_sql['sel'].tolist()
gold_aggs = gold_sql['agg'].tolist()
pre_sels = pr_sql['sel']
pre_aggs = pr_sql['agg']
if len(gold_sels) != len(pre_sels):
return False
if len(gold_sels) == 1 and gold_sels[0] == len(tablei['header']):
if len(pre_sels) == 1 and pre_sels[0] == len(tablei['header']):
return True
else:
return False
else:
gold_sel_aggs = zip(gold_sels, gold_aggs)
pre_sel_aggs = zip(pre_sels, pre_aggs)
for idx, gold_sel_aggi in enumerate(gold_sel_aggs):
if gold_sel_aggi in pre_sel_aggs:
pass
else:
return False
return True
def com_sels_with_split(gold_sql_sc, gold_sql_sa, pr_sql, tablei, table_words):
# print('tablei:', tablei)
gold_sels = gold_sql_sc
gold_aggs = gold_sql_sa
pre_sels = pr_sql['sel']
pre_aggs = pr_sql['agg']
if len(gold_sels) != len(pre_sels):
return False
if len(gold_sels) == 1 and gold_sels[0] == len(tablei['header']):
if len(pre_sels) == 1 and pre_sels[0] == len(tablei['header']):
return True
else:
return False
else:
gold_sel_aggs = zip(gold_sels, gold_aggs)
pre_sel_aggs = list(zip(pre_sels, pre_aggs))
for idx, gold_sel_aggi in enumerate(gold_sel_aggs):
if gold_sel_aggi in pre_sel_aggs:
pass
else:
return False
return True
def com_sels_with_split_final(gold_sql_sc, gold_sql_sa, pr_sql, tablei, table_words):
# print('tablei:', tablei)
gold_sels = gold_sql_sc
gold_aggs = gold_sql_sa
pre_sels = pr_sql['sel']
pre_aggs = pr_sql['agg']
if len(gold_sels) != len(pre_sels):
return False
if len(gold_sels) == 1 and gold_sels[0] == len(tablei['header']):
if len(pre_sels) == 1 and pre_sels[0] == len(tablei['header']):
return True
else:
return False
else:
gold_sel_aggs = zip(gold_sels, gold_aggs)
pre_sel_aggs = list(zip(pre_sels, pre_aggs))
# print(pre_sel_aggs)
for idx, gold_sel_aggi in enumerate(gold_sel_aggs):
if gold_sel_aggi in pre_sel_aggs:
pass
else:
# print('not', gold_sel_aggi)
return False
return True
if __name__ == '__main__':
gold = {'agg': [4], 'sel': [0], 'cond_conn_op': 1, 'conds': [['6', '3', '9.3', '9.3'],
['8', '2', 'False', '不带']],
'use_add_value': 0}
pre = {'agg': [0, 0, 0, 0], 'cond_conn_op': 2, 'sel': [2, 7, 9, 10],
'conds': [[0, 5, '焉[SEP]选汽车类型当产品名称等于'], [4, 5, '没']]}
pre = {'agg': [4], 'sel': [0], 'cond_conn_op': 1, 'conds': [['6', '3', '9.3', '9.3'],
['8', '2', '没有']],
'use_add_value': 0}
gold = {'agg': [0, 4, 0], 'sel': [0, 9, 4], 'cond_conn_op': 0, 'conds': [[6, 2, '8.4', '8.4']], 'use_add_value': 0}
pre = {'agg': [0, 0, 4], 'cond_conn_op': 0, 'sel': [0, 4, 9], 'conds': [[6, 2, '8.4']]}
tablei = {'tablename': 'benz',
'header': ['产品名称', '汽车类型', '综合耗油量', '排量', '轴距', '功率', '百公里加速', '全景天窗', '倒车影像', '智能泊车', '零售价'],
'types': ['text', 'text', 'number', 'number', 'number', 'number', 'number', 'bool', 'bool', 'bool',
'number'], 'unit': ['', '', 'L/百公里', '毫升', '毫米', '千瓦|KW', '秒', '', '', '', '万元|万'],
'attribute': ['PRIMARY', 'KEY', 'KEY', 'MODIFIER', 'MODIFIER', 'MODIFIER', 'KEY', 'MODIFIER', 'MODIFIER',
'MODIFIER', 'KEY'],
'rows': [['奔驰B180时尚型', '轿车', '5.7', '1332', '2729', '136', '9.3', 'False', 'False', 'True', '23.98'],
['奔驰C260', '轿车', '7.3', '1497', '2840', '184', '8.4', 'True', 'False', 'False', '36.68'],
['奔驰E300', '轿车', '7.5', '1991', '2873', '258', '6.5', 'True', 'False', 'False', '59.88'],
['奔驰E53', '轿车', '8.7', '2999', '2873', '435', '4.4', 'True', 'False', 'False', '97.88'],
['奔驰S63', '轿车', '10.0', '3982', '2945', '612', '3.5', 'True', 'False', 'False', '229.88'],
['奔驰GLA180', 'SUV', '6.0', '1332', '2729', '163', '9.9', 'True', 'True', 'True', '27.68'],
['奔驰GLC260动感型', 'SUV', '8.1', '1991', '2973', '197', '8.4', 'True', 'True', 'True', '39.78'],
['奔驰GLC300', 'SUV', '8.75', '1991', '2873', '258', '6.7', 'False', 'False', 'False', '51.88'],
['奔驰GLE53', 'SUV', '9.8', '2999', '2995', '435', '5.5', 'True', 'False', 'False', '102.88'],
['奔驰GLS600礼尚版', 'SUV', '12.0', '3982', '3135', '557', '4.9', 'True', 'False', 'False', '273.8']]}
is_right = com_conds_final(gold, pre, tablei,None)
is_right2 = com_sels_with_split_final(gold['sel'], gold['agg'], pre, tablei, None)
print(is_right)
print(is_right2)