-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_preprocess.py
724 lines (648 loc) · 28.1 KB
/
data_preprocess.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import json
from tqdm import tqdm
from gensim import corpora
from gensim.summarization.bm25 import BM25
from nltk.corpus import stopwords
from transformers import BertModel
from transformers import BertTokenizer
from dense_retrieval import MySimCSE
from get_datasets import get_transformed_io, get_hashtag_list
from eval_utils import f1
from functools import cmp_to_key
import csv
from data_augmentation import random_augmentation
import jieba
import jieba.posseg as pseg
def generate_index_json_file(data_path):
out_path = data_path + "_index.json"
data = []
i = 0
with open(data_path, 'r', encoding='utf-8') as f:
for line in tqdm(f):
line = line.strip("\n")
if not line:
continue
item = {
"id": i,
"contents": line
}
data.append(item)
i += 1
print(i)
jsontext = json.dumps(data, indent=4)
with open(out_path, 'w') as json_file:
json_file.write(jsontext)
def bm25_retrieval_results(train_src_data_path, val_src_data_path, test_src_data_path):
train_out_path = train_src_data_path + "_bm25_index.json"
val_out_path = val_src_data_path + "_bm25_index.json"
test_out_path = test_src_data_path + "_bm25_index.json"
# read training documents
documents = []
with open(train_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
documents.append(line)
print("The number of training documents is: ", len(documents))
# read val queries
val_queries = []
with open(val_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
val_queries.append(line)
print("The number of val queries is: ", len(val_queries))
# read test queries
test_queries = []
with open(test_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
test_queries.append(line)
print("The number of test queries is: ", len(test_queries))
# build document index
# split word
texts = [doc.split() for doc in documents]
# remove stopwords
for i in range(len(texts)):
texts[i] = [word for word in texts[i] if word not in stopwords.words('english')]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
bm25_obj = BM25(corpus)
# training query
train_query_result = []
print("Start to create training queries result...")
for i in tqdm(range(len(documents))):
query = texts[i]
query_doc = dictionary.doc2bow(query)
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-11:][::-1]
if i in best_docs:
best_docs.remove(i)
else:
best_docs = best_docs[:10]
print(documents[i])
train_query_result.append(best_docs)
json_str = json.dumps(train_query_result, indent=4)
with open(train_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating training queries result!")
# val query
val_query_result = []
print("Start to create val queries result...")
val_texts = [vq.split() for vq in val_queries]
for i in tqdm(range(len(val_texts))):
query = [word for word in val_texts[i] if word not in stopwords.words('english')]
query_doc = dictionary.doc2bow(query)
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-10:][::-1]
val_query_result.append(best_docs)
json_str = json.dumps(val_query_result, indent=4)
with open(val_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating val queries result!")
# test query
test_query_result = []
print("Start to create test queries result...")
test_texts = [tq.split() for tq in test_queries]
for i in tqdm(range(len(test_texts))):
query = [word for word in test_texts[i] if word not in stopwords.words('english')]
query_doc = dictionary.doc2bow(query)
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-10:][::-1]
test_query_result.append(best_docs)
json_str = json.dumps(test_query_result, indent=4)
with open(test_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating test queries result!")
def bm25_retrieval_score_results(train_src_data_path, val_src_data_path, test_src_data_path):
train_out_path = train_src_data_path + "_bm25_score.json"
val_out_path = val_src_data_path + "_bm25_score.json"
test_out_path = test_src_data_path + "_bm25_score.json"
# read training documents
documents = []
with open(train_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
documents.append(line)
print("The number of training documents is: ", len(documents))
# read val queries
val_queries = []
with open(val_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
val_queries.append(line)
print("The number of val queries is: ", len(val_queries))
# read test queries
test_queries = []
with open(test_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
test_queries.append(line)
print("The number of test queries is: ", len(test_queries))
# build document index
# split word
texts = [doc.split() for doc in documents]
# remove stopwords
for i in range(len(texts)):
texts[i] = [word for word in texts[i] if word not in stopwords.words('english')]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
bm25_obj = BM25(corpus)
# training query
train_query_result = []
print("Start to create training queries result...")
for i in tqdm(range(len(documents))):
query = texts[i]
query_doc = dictionary.doc2bow(query)
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-11:][::-1]
if i in best_docs:
best_docs.remove(i)
else:
best_docs = best_docs[:10]
print(documents[i])
train_query_item = dict()
train_query_item['index'] = best_docs
train_query_item['score'] = [scores[doc] for doc in best_docs]
train_query_result.append(train_query_item)
json_str = json.dumps(train_query_result, indent=4)
with open(train_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating training queries result!")
# val query
val_query_result = []
print("Start to create val queries result...")
val_texts = [vq.split() for vq in val_queries]
for i in tqdm(range(len(val_texts))):
query = [word for word in val_texts[i] if word not in stopwords.words('english')]
query_doc = dictionary.doc2bow(query)
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-10:][::-1]
val_query_item = dict()
val_query_item['index'] = best_docs
val_query_item['score'] = [scores[doc] for doc in best_docs]
val_query_result.append(val_query_item)
json_str = json.dumps(val_query_result, indent=4)
with open(val_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating val queries result!")
# test query
test_query_result = []
print("Start to create test queries result...")
test_texts = [tq.split() for tq in test_queries]
for i in tqdm(range(len(test_texts))):
query = [word for word in test_texts[i] if word not in stopwords.words('english')]
query_doc = dictionary.doc2bow(query)
scores = bm25_obj.get_scores(query_doc)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-10:][::-1]
test_query_item = dict()
test_query_item['index'] = best_docs
test_query_item['score'] = [scores[doc] for doc in best_docs]
test_query_result.append(test_query_item)
json_str = json.dumps(test_query_result, indent=4)
with open(test_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating test queries result!")
def dense_retrieval_results(train_src_data_path, val_src_data_path, test_src_data_path):
train_out_path = train_src_data_path + "_bert_original_score.json"
val_out_path = val_src_data_path + "_bert_original_score.json"
test_out_path = test_src_data_path + "_bert_original_score.json"
# loading model
# model = MySimCSE("princeton-nlp/sup-simcse-roberta-large", device='cpu')
# model = MySimCSE("/home/qiupeng/frz_project/SimCSE/result/retrieval_bert_chinese_base", device='cuda', pooler='cls')
model = MySimCSE("bert-base-chinese", device='cuda', pooler='cls')
# read training documents
documents = []
with open(train_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
documents.append(line)
print("The number of training documents is: ", len(documents))
# read val queries
val_queries = []
with open(val_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
val_queries.append(line)
print("The number of val queries is: ", len(val_queries))
# read test queries
test_queries = []
with open(test_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
test_queries.append(line)
print("The number of test queries is: ", len(test_queries))
# create index
model.build_index(documents, device='cuda', batch_size=64)
# training query
train_query_result = []
print("Start to create training queries result...")
for i in tqdm(range(len(documents))):
results = model.search(documents[i], device='cuda', threshold=-99, top_k=11)
for k in range(len(results)):
if results[k][0] == i:
results.pop(k)
break
if len(results) > 10:
results = results[:10]
train_query_item = dict()
train_query_item['index'] = [ind for ind, score in results]
train_query_item['score'] = [score for ind, score in results]
train_query_result.append(train_query_item)
json_str = json.dumps(train_query_result, indent=4)
with open(train_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating training queries result!")
# val query
val_query_result = []
print("Start to create val queries result...")
for i in tqdm(range(len(val_queries))):
query = val_queries[i]
results = model.search(query, device='cuda', threshold=-99, top_k=10)
val_query_item = dict()
val_query_item['index'] = [ind for ind, score in results]
val_query_item['score'] = [score for ind, score in results]
val_query_result.append(val_query_item)
json_str = json.dumps(val_query_result, indent=4)
with open(val_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating val queries result!")
# test query
test_query_result = []
print("Start to create test queries result...")
for i in tqdm(range(len(test_queries))):
query = test_queries[i]
results = model.search(query, device='cuda', threshold=-99, top_k=10)
test_query_item = dict()
test_query_item['index'] = [ind for ind, score in results]
test_query_item['score'] = [score for ind, score in results]
test_query_result.append(test_query_item)
json_str = json.dumps(test_query_result, indent=4)
with open(test_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating test queries result!")
def clean_repetition_datasets(str_data_path, dst_data_path):
# completed
str_out_path = str_data_path + "_after_cleaning.txt"
dst_out_path = dst_data_path + "_after_cleaning.txt"
documents = []
dst = []
with open(str_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
documents.append(line)
with open(dst_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
dst.append(line)
print(len(documents))
print(len(dst))
new_doc = []
new_dst = []
for idx in range(len(documents)):
if documents[idx] not in new_doc or dst[idx] not in new_dst:
new_doc.append(documents[idx])
new_dst.append(dst[idx])
else:
print(idx)
print(documents[idx])
print(dst[idx])
print('=' * 30)
print(len(new_doc))
print(len(new_dst))
with open(str_out_path, 'w', encoding='utf-8') as f:
for doc in new_doc:
f.write(doc + '\n')
with open(dst_out_path, 'w', encoding='utf-8') as f:
for d in new_dst:
f.write(d + '\n')
def preprocess_wangyue_data(post_path, conv_path, tag_path, src_path, dst_path):
post_list = []
with open(post_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
post_list.append(line.strip())
conv_list = []
with open(conv_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
conv_list.append(line.strip())
tag_list = []
with open(tag_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
tag_list.append(line.strip())
assert len(post_list) == len(conv_list) and len(conv_list) == len(tag_list)
src_list = []
dst_list = []
for i in range(len(post_list)):
src = post_list[i] + '. ' + conv_list[i]
dst = tag_list[i].replace(';', ' [SEP] ')
src_list.append(src)
dst_list.append(dst)
with open(src_path, 'w', encoding='utf-8') as f:
for src in src_list:
f.write(src + '\n')
with open(dst_path, 'w', encoding='utf-8') as f:
for dst in dst_list:
f.write(dst + '\n')
def compute_hashtag_coverage(labels, hashtags):
total_r = len(labels)
total_p = len(hashtags)
label_list = labels.copy()
hashtag_list = hashtags.copy()
true_num = 0
for lab in label_list:
for hashtag in hashtag_list:
if lab == hashtag:
true_num += 1
hashtag_list.remove(lab)
break
p = true_num / total_p
r = true_num / total_r
f = f1(p, r)
return p, r, f
def hashtag_coverage_cmp(x, y):
# x is in front of y if the function returns -1
if x[1][1] + x[1][2] > y[1][1] + y[1][2]:
return -1
if x[1][1] + x[1][2] == y[1][1] + y[1][2]:
if x[1][0] > y[1][0]:
return -1
elif x[1][0] == y[1][0]:
return 0
else:
return 1
return 1
def generate_training_data_for_retrieval(src_data_path, dst_data_path, retrieval_data_path, output_path):
src, targets, hashtags = get_transformed_io(src_data_path, dst_data_path)
with open(retrieval_data_path, 'r', encoding='UTF-8') as fp:
rev_index_list = json.load(fp)
total_num = len(src)
positive_samples = []
hard_negative_samples = []
for i in tqdm(range(total_num)):
coverage_rate = dict()
for j in range(total_num):
if i == j:
coverage_rate[i] = (-99, -99, -99)
continue
rate = compute_hashtag_coverage(hashtags[i], hashtags[j])
coverage_rate[j] = rate
coverage_rate = sorted(coverage_rate.items(), key=cmp_to_key(hashtag_coverage_cmp), reverse=False)[0]
positive_sample = src[coverage_rate[0]]
rev_index = rev_index_list[i]["index"]
nega_rate = dict()
for ind in rev_index:
rate = compute_hashtag_coverage(hashtags[i], hashtags[ind])
nega_rate[ind] = rate
nega_rate = sorted(nega_rate.items(), key=cmp_to_key(hashtag_coverage_cmp), reverse=True)[:2]
hard_negative_sample = src[nega_rate[0][0]]
if hard_negative_sample == positive_sample:
hard_negative_sample = src[nega_rate[1][0]]
positive_samples.append(positive_sample)
hard_negative_samples.append(hard_negative_sample)
assert len(src) == len(positive_samples) == len(hard_negative_samples)
with open(output_path, 'w', encoding='UTF-8') as fp:
header = ['sent0', 'sent1', 'hard_neg']
writer = csv.writer(fp)
writer.writerow(header)
data = []
for i in range(total_num):
line = [src[i], positive_samples[i], hard_negative_samples[i]]
data.append(line)
writer.writerows(data)
# print(src[i])
# print(hashtags[i])
# print(coverage_rate)
# for cov in coverage_rate:
# print(cov[0])
# print(src[cov[0]])
# print(hashtags[cov[0]])
# print('-'*30)
def generate_training_data_for_selector(src_data_path, dst_data_path, output_path):
src, targets, hashtags = get_transformed_io(src_data_path, dst_data_path)
constructive_src = []
positive_samples = []
hard_negative_samples = []
for i in tqdm(range(len(src))):
for hashtag in hashtags[i]:
hard_negative_hashtag = random_augmentation(hashtag)
constructive_src.append(src[i])
positive_samples.append(hashtag)
hard_negative_samples.append(hard_negative_hashtag)
total_num = len(constructive_src)
with open(output_path, 'w', encoding='UTF-8') as fp:
header = ['sent0', 'sent1', 'hard_neg']
writer = csv.writer(fp)
writer.writerow(header)
data = []
for i in range(total_num):
line = [constructive_src[i], positive_samples[i], hard_negative_samples[i]]
data.append(line)
writer.writerows(data)
def generate_selector_result_for_retrieval_result(src_data_path, retrieval_index_path, retrieval_document_path, selector_model_path, out_path):
src_data = []
with open(src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
src_data.append(line)
retrieval_document = []
with open(retrieval_document_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
retrieval_document.append(line)
with open(retrieval_index_path, 'r', encoding='UTF-8') as fp:
retrieval_index = json.load(fp)
assert len(src_data) == len(retrieval_index)
rev_dst = [[get_hashtag_list(retrieval_document[index]) for index in retrieval_index[i]["index"]] for i in
range(len(src_data))]
model = MySimCSE(selector_model_path, device='cuda')
out_put = []
for i in range(len(src_data)):
hashtag_set = []
for hashtag in rev_dst[i]:
hashtag_set += hashtag
hashtag_set = list(set(hashtag_set))
hashtag_dic = dict()
model.build_index(hashtag_set, device='cuda', batch_size=64)
results = model.search(src_data[i], device='cuda', threshold=-99, top_k=99999)
for ind, score in results:
hashtag_dic[hashtag_set[ind]] = score
out_put.append(hashtag_dic)
json_str = json.dumps(out_put, indent=4, ensure_ascii=False)
with open(out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
def Chinese_delete_stop_words(seq: str):
stop_flag = ['x', 'c', 'u', 'd', 'p', 't', 'uj', 'm', 'f', 'r']
words = pseg.cut(seq)
key_words = [word for word, flag in words if flag not in stop_flag]
# result = ' '.join(key_words)
return key_words
def Chinese_bm25_retrieval_score_results(train_src_data_path, val_src_data_path, test_src_data_path):
train_out_path = train_src_data_path + "_bm25_score.json"
val_out_path = val_src_data_path + "_bm25_score.json"
test_out_path = test_src_data_path + "_bm25_score.json"
# read training documents
documents = []
with open(train_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
documents.append(line)
print("The number of training documents is: ", len(documents))
# read val queries
val_queries = []
with open(val_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
val_queries.append(line)
print("The number of val queries is: ", len(val_queries))
# read test queries
test_queries = []
with open(test_src_data_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip("\n")
if not line:
continue
test_queries.append(line)
print("The number of test queries is: ", len(test_queries))
# build document index
# split word
texts = []
# remove stopwords
for doc in documents:
texts.append(Chinese_delete_stop_words(doc))
bm25_obj = BM25(texts)
# training query
train_query_result = []
print("Start to create training queries result...")
for i in tqdm(range(len(documents))):
query = texts[i]
scores = bm25_obj.get_scores(query)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-11:][::-1]
if i in best_docs:
best_docs.remove(i)
else:
best_docs = best_docs[:10]
print(documents[i])
train_query_item = dict()
train_query_item['index'] = best_docs
train_query_item['score'] = [scores[doc] for doc in best_docs]
train_query_result.append(train_query_item)
json_str = json.dumps(train_query_result, indent=4)
with open(train_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating training queries result!")
# val query
val_query_result = []
print("Start to create val queries result...")
for i in tqdm(range(len(val_queries))):
query = Chinese_delete_stop_words(val_queries[i])
scores = bm25_obj.get_scores(query)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-10:][::-1]
val_query_item = dict()
val_query_item['index'] = best_docs
val_query_item['score'] = [scores[doc] for doc in best_docs]
val_query_result.append(val_query_item)
json_str = json.dumps(val_query_result, indent=4)
with open(val_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating val queries result!")
# test query
test_query_result = []
print("Start to create test queries result...")
for i in tqdm(range(len(test_queries))):
query = Chinese_delete_stop_words(test_queries[i])
scores = bm25_obj.get_scores(query)
best_docs = sorted(range(len(scores)), key=lambda j: scores[j])[-10:][::-1]
test_query_item = dict()
test_query_item['index'] = best_docs
test_query_item['score'] = [scores[doc] for doc in best_docs]
test_query_result.append(test_query_item)
json_str = json.dumps(test_query_result, indent=4)
with open(test_out_path, 'w', encoding='utf-8') as f:
f.write(json_str)
print("Finish creating test queries result!")
if __name__ == '__main__':
src_train_data_path = 'data/THG_twitter/twitter.2021.train.src_after_cleaning.txt'
dst_data_path = 'data/THG_twitter/twitter.2021.train.dst_after_cleaning.txt'
src_val_data_path = 'data/THG_twitter/twitter.2021.valid.src_after_cleaning.txt'
src_test_data_path = 'data/THG_twitter/twitter.2021.test.src_after_cleaning.txt'
rev_index_path = 'data/THG_twitter/twitter.2021.train.src_after_cleaning.txt_simcse_tuned_dense_score.json'
# compute_hashtag_coverage(['fanconi anemia', '12345'], ['anemia', '9977'])
# retrieval_training_data_path = 'data/THG_twitter/retrieval_training_data.csv'
# generate_training_data_for_retrieval(src_train_data_path, dst_data_path, rev_index_path, retrieval_training_data_path)
# selector_training_data_path = 'data/THG_twitter/selector_training_data.csv'
# generate_training_data_for_selector(src_train_data_path, dst_data_path, selector_training_data_path)
# generate_selector_result_for_retrieval_result(src_train_data_path, rev_index_path, dst_data_path, './result/selector-tuned-sup-simcse-roberta-large')
# post_path = 'data/Twitter_naacl_wangyue/test_post.txt'
# conv_path = 'data/Twitter_naacl_wangyue/test_conv.txt'
# tag_path = 'data/Twitter_naacl_wangyue/test_tag.txt'
# src_path = 'data/Twitter_naacl_wangyue/test_src.txt'
# dst_path = 'data/Twitter_naacl_wangyue/test_dst.txt'
# preprocess_wangyue_data(post_path, conv_path, tag_path, src_path, dst_path)
# wangyue_train_src_path = 'data/Twitter_naacl_wangyue/train_src.txt'
# wangyue_val_src_path = 'data/Twitter_naacl_wangyue/valid_src.txt'
# wangyue_test_src_path = 'data/Twitter_naacl_wangyue/test_src.txt'
# src_train_data_path = 'data/THG_twitter/sample_src.txt'
# src_val_data_path = 'data/THG_twitter/sample_src.txt'
# src_test_data_path = 'data/THG_twitter/sample_src.txt'
# generate_index_json_file(data_path)
# dense_retrieval_results(data_path)
# src_data_path = 'data/WHG/new4_test.src'
# dst_data_path = 'data/WHG/new4_test.dst'
# clean_repetition_datasets(src_data_path, dst_data_path)
# dense_retrieval_results(src_train_data_path, src_val_data_path, src_test_data_path)
# bm25_retrieval_score_results(wangyue_train_src_path, wangyue_val_src_path, wangyue_test_src_path)
train_src_data_path = 'data/WHG/new4_train.src_after_cleaning.txt'
val_src_data_path = 'data/WHG/new4_validation.src'
test_src_data_path = 'data/WHG/new4_test.src'
# Chinese_bm25_retrieval_score_results(train_src_data_path, val_src_data_path, test_src_data_path)
dense_retrieval_results(train_src_data_path, val_src_data_path, test_src_data_path)
# src_train_data_path = 'data/WHG/new4_train.src_after_cleaning.txt'
# dst_data_path = 'data/WHG/new4_train.dst_after_cleaning.txt'
# src_val_data_path = 'data/WHG/new4_validation.src'
# src_test_data_path = 'data/WHG/new4_test.src'
# rev_index_train_path = 'data/WHG/new4_train.src_after_cleaning.txt_bert_dense_score.json'
# rev_index_val_path = 'data/WHG/new4_validation.src_bert_dense_score.json'
# rev_index_test_path = 'data/WHG/new4_test.src_bert_dense_score.json'
# out_train_path = 'data/WHG/train_tunedbert_selector_result.json'
# out_val_path = 'data/WHG/validation_tunedbert_selector_result.json'
# out_test_path = 'data/WHG/test_tunedbert_selector_result.json'
# generate_selector_result_for_retrieval_result(src_val_data_path, rev_index_val_path, dst_data_path, '/home/qiupeng/frz_project/SimCSE/result/selector_bert_chinese_base', out_val_path)
# generate_selector_result_for_retrieval_result(src_test_data_path, rev_index_test_path, dst_data_path, '/home/qiupeng/frz_project/SimCSE/result/selector_bert_chinese_base', out_test_path)
# generate_selector_result_for_retrieval_result(src_train_data_path, rev_index_train_path, dst_data_path, '/home/qiupeng/frz_project/SimCSE/result/selector_bert_chinese_base', out_train_path)