-
Notifications
You must be signed in to change notification settings - Fork 0
/
contrastive_dataset_creation.py
364 lines (308 loc) · 12.1 KB
/
contrastive_dataset_creation.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
import os
import json
from sentence_transformers import SentenceTransformer, util
import torch
import csv
import random
from sentence_transformers import SentenceTransformer, util
import torch
from transformers import *
from sentence_transformers import SentenceTransformer
from sentence_transformers import SentenceTransformer, util
import torch
import csv
import re
from langchain_text_splitters import NLTKTextSplitter
import random
# set random seed
random_seed = 42
random.seed(random_seed)
device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu")
model = SentenceTransformer("NeuML/pubmedbert-base-embeddings").to(device)
def load_data(dataset, datatype):
data_folder = './biolaysumm2024_data'
data_path = os.path.join(data_folder, f'{dataset}_{datatype}.jsonl')
lay_sum = []
article =[]
keyword = []
headings = []
id = []
file = open(data_path, 'r')
for line in (file.readlines()):
dic = json.loads(line)
article.append(dic['article'])
keyword.append(dic['keywords'])
headings.append(dic['headings'])
id.append(dic['id'])
lay_sum.append(dic['lay_summary'])
return article, lay_sum, keyword, headings, id
def load_test_data(dataset, datatype):
data_folder = './biolaysumm2024_data'
data_path = os.path.join(data_folder, f'{dataset}_{datatype}.jsonl')
article =[]
keyword = []
headings = []
id = []
file = open(data_path, 'r')
for line in (file.readlines()):
dic = json.loads(line)
article.append(dic['article'])
keyword.append(dic['keywords'])
headings.append(dic['headings'])
id.append(dic['id'])
return article, keyword, headings, id
background = []
objective = []
methods = []
results = []
conclusions = []
with open('./data/Structured-Abstracts-Labels-102615.txt', 'r') as file:
for line in file:
components = line.strip().split('|')
title, category, _, _ = components
if category == 'BACKGROUND':
background.append(title)
elif category == 'OBJECTIVE':
objective.append(title)
elif category == 'METHODS':
methods.append(title)
elif category == 'RESULTS':
results.append(title)
elif category == 'CONCLUSIONS':
conclusions.append(title)
background = [item.lower() for item in background]
objective = [item.lower() for item in objective]
methods = [item.lower() for item in methods]
results = [item.lower() for item in results]
conclusions = [item.lower() for item in conclusions]
### PLOS
# train
plos_article_train, plos_lay_sum_train, plos_keyword_train, plos_headings_train, plos_id_train = load_data('PLOS', 'train')
# val
plos_article_val, plos_lay_sum_val, plos_keyword_val, plos_headings_val, plos_id_val = load_data('PLOS', 'val')
### eLife
# train
elife_article_train, elife_lay_sum_train, elife_keyword_train, elife_headings_train, elife_id_train = load_data('eLife', 'train')
# val
elife_article_val, elife_lay_sum_val, elife_keyword_val, elife_headings_val, elife_id_val = load_data('eLife', 'val')
### chunk articles and lay summs for PLOS
pattern = r'\s\[.*?\]'
text_splitter = NLTKTextSplitter(chunk_size=600)
### train
new_plos_article_train = []
for s in plos_article_train:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_plos_article_train.append(new_s)
chunked_plos_article_train = []
for article in new_plos_article_train:
texts = text_splitter.split_text(article)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
### remove the irrelevant citations and references
result = re.sub(pattern, "", t)
result = result.replace(' , ', ', ')
new_texts.append(result)
chunked_plos_article_train.append(new_texts)
### val
new_plos_article_val = []
for s in plos_article_val:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_plos_article_val.append(new_s)
chunked_plos_article_val = [] # 100 tokens per chunk
for article in new_plos_article_val:
texts = text_splitter.split_text(article)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
result = re.sub(pattern, "", t)
result = result.replace(' , ', ', ')
new_texts.append(result)
chunked_plos_article_val.append(new_texts)
### train lay sum
new_plos_lay_sum_train = []
for s in plos_lay_sum_train:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_plos_lay_sum_train.append(new_s)
chunked_plos_lay_sum_train = []
for sum in new_plos_lay_sum_train:
texts = text_splitter.split_text(sum)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
new_texts.append(t)
chunked_plos_lay_sum_train.append(new_texts)
### val lay sum
new_plos_lay_sum_val = []
for s in plos_lay_sum_val:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_plos_lay_sum_val.append(new_s)
chunked_plos_lay_sum_val = []
for sum in new_plos_lay_sum_val:
texts = text_splitter.split_text(sum)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
new_texts.append(t)
chunked_plos_lay_sum_val.append(new_texts)
### chunk articles and lay summs for eLife
pattern = r'(\(\s([^()]*\s\,\s)*[^()]*\s\))'
text_splitter = NLTKTextSplitter(chunk_size=600)
### train
new_elife_article_train = []
for s in elife_article_train:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_elife_article_train.append(new_s)
chunked_elife_article_train = []
for article in new_elife_article_train:
texts = text_splitter.split_text(article)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
### remove the irrelevant citations and references
result = re.sub(pattern, "", t)
result = result.replace(' , ', ', ')
new_texts.append(result)
chunked_elife_article_train.append(new_texts)
### val
new_elife_article_val = []
for s in elife_article_val:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_elife_article_val.append(new_s)
chunked_elife_article_val = []
for article in new_elife_article_val:
texts = text_splitter.split_text(article)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
### remove the irrelevant citations and references
result = re.sub(pattern, "", t)
result = result.replace(' , ', ', ')
new_texts.append(result)
chunked_elife_article_val.append(new_texts)
### train lay sum
new_elife_lay_sum_train = []
for s in elife_lay_sum_train:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_elife_lay_sum_train.append(new_s)
chunked_elife_lay_sum_train = []
for sum in new_elife_lay_sum_train:
texts = text_splitter.split_text(sum)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
new_texts.append(t)
chunked_elife_lay_sum_train.append(new_texts)
### val lay sum
new_elife_lay_sum_val = []
for s in elife_lay_sum_val:
new_s = s.replace(' . ', '. ')
new_s = new_s.replace(' , ', ', ')
new_elife_lay_sum_val.append(new_s)
chunked_elife_lay_sum_val = []
for sum in new_elife_lay_sum_val:
texts = text_splitter.split_text(sum)
new_texts = []
for t in texts:
t = t.replace('\n\n', ' ')
new_texts.append(t)
chunked_elife_lay_sum_val.append(new_texts)
### PLOS train contrastive datasets creation
positive_chunk = []
negative_chunk = []
for lay_sum, doc in zip(chunked_plos_lay_sum_train, chunked_plos_article_train):
doc_embeddings = model.encode(doc, convert_to_tensor=True)
lay_embeddings = model.encode(lay_sum, convert_to_tensor=True)
cosine_scores = util.cos_sim(lay_embeddings, doc_embeddings)
for i in range(cosine_scores.shape[0]):
for j in range(cosine_scores.shape[1]):
if cosine_scores[i][j] >= 0.9:
positive_chunk.append((lay_sum[i], doc[j], 1))
elif cosine_scores[i][j] <= 0.01:
negative_chunk.append((lay_sum[i], doc[j], 0))
print("Number of PLOS training positive sentences:", len(positive_chunk))
print("Number of PLOS training negative sentences:", len(negative_chunk))
selected_samples = random.sample(negative_chunk, 17910)
with open('./plos_train_sentence_level_positive_negative_pairs.csv', mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['lay_sum', 'sentence', 'label'])
for row in positive_chunk:
writer.writerow(row)
for row in selected_samples:
writer.writerow(row)
### eLife train contrastive datasets creation
positive_chunk = []
negative_chunk = []
for lay_sum, doc in zip(chunked_elife_lay_sum_train, chunked_elife_article_train):
doc_embeddings = model.encode(doc, convert_to_tensor=True)
lay_embeddings = model.encode(lay_sum, convert_to_tensor=True)
cosine_scores = util.cos_sim(lay_embeddings, doc_embeddings)
for i in range(cosine_scores.shape[0]):
for j in range(cosine_scores.shape[1]):
if cosine_scores[i][j] >= 0.9:
positive_chunk.append((lay_sum[i], doc[j], 1))
elif cosine_scores[i][j] <= 0.01:
negative_chunk.append((lay_sum[i], doc[j], 0))
print("Number of eLife training positive sentences:", len(positive_chunk))
print("Number of eLife training negative sentences:", len(negative_chunk))
selected_samples = random.sample(negative_chunk, 17910)
with open('./elife_train_sentence_level_positive_negative_pairs.csv', mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['lay_sum', 'sentence', 'label'])
for row in positive_chunk:
writer.writerow(row)
for row in selected_samples:
writer.writerow(row)
### PLOS val contrastive datasets creation
positive_chunk = []
negative_chunk = []
for lay_sum, doc in zip(chunked_plos_lay_sum_val, chunked_plos_article_val):
doc_embeddings = model.encode(doc, convert_to_tensor=True)
lay_embeddings = model.encode(lay_sum, convert_to_tensor=True)
cosine_scores = util.cos_sim(lay_embeddings, doc_embeddings)
for i in range(cosine_scores.shape[0]):
for j in range(cosine_scores.shape[1]):
if cosine_scores[i][j] >= 0.9:
positive_chunk.append((lay_sum[i], doc[j], 1))
elif cosine_scores[i][j] <= 0.01:
negative_chunk.append((lay_sum[i], doc[j], 0))
print("Number of val positive sentences:", len(positive_chunk))
print("Number of val negative sentences:", len(negative_chunk))
selected_samples = random.sample(negative_chunk, 16210)
with open('./plos_val_sentence_level_positive_negative_pairs.csv', mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['lay_sum', 'sentence', 'label'])
for row in positive_chunk:
writer.writerow(row)
for row in negative_chunk:
writer.writerow(row)
### eLife val contrastive datasets creation
positive_chunk = []
negative_chunk = []
for lay_sum, doc in zip(chunked_elife_lay_sum_val, chunked_elife_article_val):
doc_embeddings = model.encode(doc, convert_to_tensor=True)
lay_embeddings = model.encode(lay_sum, convert_to_tensor=True)
cosine_scores = util.cos_sim(lay_embeddings, doc_embeddings)
for i in range(cosine_scores.shape[0]):
for j in range(cosine_scores.shape[1]):
if cosine_scores[i][j] >= 0.9:
positive_chunk.append((lay_sum[i], doc[j], 1))
elif cosine_scores[i][j] <= 0.01:
negative_chunk.append((lay_sum[i], doc[j], 0))
print("Number of eLife val positive sentences:", len(positive_chunk))
print("Number of eLife val negative sentences:", len(negative_chunk))
selected_samples = random.sample(negative_chunk, 16210)
with open('./elife_val_sentence_level_positive_negative_pairs.csv', mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['lay_sum', 'sentence', 'label'])
for row in positive_chunk:
writer.writerow(row)
for row in negative_chunk:
writer.writerow(row)