This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_preprocessing.py
273 lines (195 loc) · 8.67 KB
/
data_preprocessing.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
from __future__ import annotations
import json
import math
import pickle
import random
from sentence_transformers import SentenceTransformer
file_path_1 = "paraphrase/MSRParaphraseCorpus/msr_paraphrase_train.txt"
file_path_2 = "paraphrase/multinli_1.0/multinli_1.0_train.jsonl"
file1 = open(file_path_1)
lines_of_sentences = file1.readlines()
# list to store values
labeled_pairs_list = []
for value in lines_of_sentences:
# split on "\t"
data = value.split("\t")
label = data[0]
# list of pairs of sentences needed
sent_pairs = [data[3].strip("\n"), data[4].strip("\n")]
# labeled pairs
labeled_pairs_list.append([label, sent_pairs])
# remove item 1 with headings
labeled_pairs_list.pop(0)
count_1 = 0
count_0 = 0
for item in labeled_pairs_list:
if int(item[0]) == 1:
count_1 += 1
else:
count_0 += 1
# print(count_1,count_0)
with open(file_path_2, "r") as json_file:
json_list = list(json_file)
nli_contr_count = 0
nli_pairs_list = []
for json_str in json_list:
result = json.loads(json_str)
if result["annotator_labels"] == ["contradiction"]:
label = str(0)
sent1 = result["sentence1"]
sent2 = result["sentence2"]
sent_pairs = [sent1, sent2]
nli_contr_count += 1
nli_pairs_list.append([label, sent_pairs])
subset = random.sample(nli_pairs_list, 5000)
# print(subset[0:5])
# print(len(subset))
list_of_MRPC_pairs = labeled_pairs_list
print(len(list_of_MRPC_pairs))
# print(list_of_MRPC_pairs[0:5])
final_list_of_pairs = random.sample(
labeled_pairs_list + subset, len(labeled_pairs_list + subset)
)
print(len(final_list_of_pairs))
# print(final_list_of_pairs[0:5])
# print(len(nli_pairs_list))
# print(nli_pairs_list[0:5])
model = SentenceTransformer("all-MiniLM-L6-v2", device="cuda")
train_labels = [int(item[0]) for item in final_list_of_pairs]
sentence_1_list = [item[1][0] for item in final_list_of_pairs]
sentence_2_list = [item[1][1] for item in final_list_of_pairs]
print(len(train_labels), len(sentence_1_list), len(sentence_2_list))
"""embeddings_1 = model.encode(sentence_1_list)
embeddings_2 = model.encode(sentence_2_list)"""
"""with open('paraphrase/data/embeddings_1.pkl', "wb") as fOut1:
pickle.dump({'sentences': sentence_1_list, 'embeddings': embeddings_1}, fOut1, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/embeddings_2.pkl', "wb") as fOut2:
pickle.dump({'sentences': sentence_2_list, 'embeddings': embeddings_2}, fOut2, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/labels.pkl', "wb") as fOut3:
pickle.dump({'labels': train_labels}, fOut3, protocol=pickle.HIGHEST_PROTOCOL)"""
# Embeddings for only MPRC dataset
train_labels_MPRC = [int(item[0]) for item in list_of_MRPC_pairs]
MPRC_list_1 = [item[1][0] for item in list_of_MRPC_pairs]
MPRC_list_2 = [item[1][1] for item in list_of_MRPC_pairs]
print(len(train_labels_MPRC), len(MPRC_list_1), len(MPRC_list_2))
"""mprc_embeddings_1 = model.encode(MPRC_list_1)
mprc_embeddings_2 = model.encode(MPRC_list_2)
with open('paraphrase/data/mprc_embeddings_1.pkl', "wb") as fOut_mprc1:
pickle.dump({'sentences': MPRC_list_1, 'embeddings': mprc_embeddings_1}, fOut_mprc1, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/mprc_embeddings_2.pkl', "wb") as fOut_mprc2:
pickle.dump({'sentences': MPRC_list_2, 'embeddings': mprc_embeddings_2}, fOut_mprc2, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/mprc_labels.pkl', "wb") as fOut_mprc3:
pickle.dump({'labels': train_labels_MPRC}, fOut_mprc3, protocol=pickle.HIGHEST_PROTOCOL)"""
# Testing vectors using MPRC test file only:
file_path_test = "paraphrase/MSRParaphraseCorpus/msr_paraphrase_test.txt"
file_test = open(file_path_test)
lines_of_sentences = file_test.readlines()
# list to store values
test_pairs_list = []
for value in lines_of_sentences:
# split on "\t"
data = value.split("\t")
label = data[0]
# list of pairs of sentences needed
sent_pairs = [data[3].strip("\n"), data[4].strip("\n")]
# labeled pairs
test_pairs_list.append([label, sent_pairs])
# remove item 1 with headings
test_pairs_list.pop(0)
count_1 = 0
count_0 = 0
for item in test_pairs_list:
if int(item[0]) == 1:
count_1 += 1
else:
count_0 += 1
# print(count_1,count_0)
list_of_test_pairs = test_pairs_list
model = SentenceTransformer("all-MiniLM-L6-v2", device="cuda")
test_labels = [int(item[0]) for item in list_of_test_pairs]
test_1_list = [item[1][0] for item in list_of_test_pairs]
test_2_list = [item[1][1] for item in list_of_test_pairs]
print(len(test_labels), len(test_1_list), len(test_2_list))
"""test_embeddings1 = model.encode(test_1_list)
test_embeddings2 = model.encode(test_2_list)
with open('paraphrase/data/test_embeddings_1.pkl', "wb") as fOut1:
pickle.dump({'sentences': test_1_list, 'embeddings': test_embeddings1}, fOut1, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/test_embeddings_2.pkl', "wb") as fOut2:
pickle.dump({'sentences': test_2_list, 'embeddings': test_embeddings2}, fOut2, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/test_labels.pkl', "wb") as fOut3:
pickle.dump({'labels': test_labels}, fOut3, protocol=pickle.HIGHEST_PROTOCOL)"""
file_path_3 = "paraphrase/paws_corpus/test.tsv"
file3 = open(file_path_3)
lines_of_sentences_paws = file3.readlines()
# list to store values
labeled_pairs_list_paws = []
for value in lines_of_sentences_paws:
# split on "\t"
data = value.split("\t")
label = data[3].strip("\n")
# list of pairs of sentences needed
sent_pairs = [data[1].strip("\n"), data[2].strip("\n")]
# labeled pairs
labeled_pairs_list_paws.append([label, sent_pairs])
labeled_pairs_list_paws.pop(0)
# print(labeled_pairs_list_paws[0])
# print(labeled_pairs_list_paws[1])
count_1 = 0
count_0 = 0
for item in labeled_pairs_list_paws:
if int(item[0]) == 1:
count_1 += 1
else:
count_0 += 1
# print(count_1, count_0)
model = SentenceTransformer("all-MiniLM-L6-v2", device="cuda")
test_labels = [int(item[0]) for item in labeled_pairs_list_paws]
test_1_list = [item[1][0] for item in labeled_pairs_list_paws]
test_2_list = [item[1][1] for item in labeled_pairs_list_paws]
print(len(test_labels), len(test_1_list), len(test_2_list))
"""test_embeddings_paws1 = model.encode(test_1_list)
test_embeddings_paws2 = model.encode(test_2_list)"""
"""with open('paraphrase/data/test_embeddings_paws1.pkl', "wb") as fOut1:
pickle.dump({'sentences': test_1_list, 'embeddings': test_embeddings_paws1}, fOut1, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/test_embeddings_paws2.pkl', "wb") as fOut2:
pickle.dump({'sentences': test_2_list, 'embeddings': test_embeddings_paws2}, fOut2, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/test_labels_paws.pkl', "wb") as fOut3:
pickle.dump({'labels': test_labels}, fOut3, protocol=pickle.HIGHEST_PROTOCOL)"""
# Also try to train on PAWS dataset, generate PAWS train embeddings
file_path_4 = "paraphrase/paws_corpus/train.tsv"
file4 = open(file_path_4)
lines_of_sentences_paws_train = file4.readlines()
# list to store values
labeled_pairs_list_paws_train = []
for value in lines_of_sentences_paws_train:
# split on "\t"
data = value.split("\t")
label = data[3].strip("\n")
# list of pairs of sentences needed
sent_pairs = [data[1].strip("\n"), data[2].strip("\n")]
# labeled pairs
labeled_pairs_list_paws_train.append([label, sent_pairs])
labeled_pairs_list_paws_train.pop(0)
# print(labeled_pairs_list_paws_train[0])
# print(labeled_pairs_list_paws_train[1])
count_1 = 0
count_0 = 0
for item in labeled_pairs_list_paws_train:
if int(item[0]) == 1:
count_1 += 1
else:
count_0 += 1
# print(count_1, count_0)
model = SentenceTransformer("all-MiniLM-L6-v2", device="cuda")
train_labels_paws = [int(item[0]) for item in labeled_pairs_list_paws_train]
train_1_list = [item[1][0] for item in labeled_pairs_list_paws_train]
train_2_list = [item[1][1] for item in labeled_pairs_list_paws_train]
print(len(train_labels_paws), len(train_1_list), len(train_2_list))
"""train_embeddings_paws1 = model.encode(train_1_list)
train_embeddings_paws2 = model.encode(train_2_list)
with open('paraphrase/data/train_embeddings_paws1.pkl', "wb") as fOut1:
pickle.dump({'sentences': train_1_list, 'embeddings': train_embeddings_paws1}, fOut1, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/train_embeddings_paws2.pkl', "wb") as fOut2:
pickle.dump({'sentences': train_2_list, 'embeddings': train_embeddings_paws2}, fOut2, protocol=pickle.HIGHEST_PROTOCOL)
with open('paraphrase/data/train_labels_paws.pkl', "wb") as fOut3:
pickle.dump({'labels': train_labels_paws}, fOut3, protocol=pickle.HIGHEST_PROTOCOL)"""