-
Notifications
You must be signed in to change notification settings - Fork 2
/
datareader.py
448 lines (363 loc) · 19.1 KB
/
datareader.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
import os
import pandas as pd
import torch
import re
import glob
import json
import numpy as np
from torch.utils.data import Dataset
from typing import AnyStr, Tuple, List, Callable
from transformers import PreTrainedTokenizer
import html
import csv
from pu_learning import get_negative_sample_weights
from pu_learning import estimate_class_prior_probability
def text_to_batch_transformer(text: AnyStr, tokenizer: PreTrainedTokenizer, text_pair: AnyStr = None) -> Tuple[List, List]:
"""Turn a piece of text into a batch for transformer model
:param text: The text to tokenize and encode
:param tokenizer: The tokenizer to use
:param: text_pair: An optional second string (for multiple sentence sequences)
:return: A list of IDs and a mask
"""
if text_pair is None:
input_ids = [tokenizer.encode(t, add_special_tokens=True, max_length=tokenizer.max_len) for t in text]
else:
input_ids = [tokenizer.encode(t, text_pair=p, add_special_tokens=True, max_length=tokenizer.max_len) for t,p in zip(text, text_pair)]
masks = [[1] * len(i) for i in input_ids]
return input_ids, masks
def collate_batch_transformer(input_data: Tuple) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
input_ids = [i[0][0] for i in input_data]
masks = [i[1][0] for i in input_data]
labels = [i[2] for i in input_data]
max_length = max([len(i) for i in input_ids])
input_ids = [(i + [0] * (max_length - len(i))) for i in input_ids]
masks = [(m + [0] * (max_length - len(m))) for m in masks]
assert (all(len(i) == max_length for i in input_ids))
assert (all(len(m) == max_length for m in masks))
return torch.tensor(input_ids), torch.tensor(masks), torch.tensor(labels)
def collate_batch_transformer_with_index(input_data: Tuple) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, List]:
return collate_batch_transformer(input_data) + ([i[-1] for i in input_data],)
def collate_batch_transformer_with_weight(input_data: Tuple) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
return collate_batch_transformer(input_data) + (torch.tensor([i[3] for i in input_data]),)
def get_first_sentence_redi_et_al(text: AnyStr) -> AnyStr:
"""Applies the first sentence selector used in Redi et al (2019), code is here:
https://github.com/mirrys/citation-needed-paper/blob/d5023eca274623963522e4a64364b572547fc014/run_citation_need_model.py#L41
:param text: The original statement
:return: The first sentence in the statement
"""
# check first if the statements is longer than a single sentence.
sentences = re.compile('\.\s+').split(str(text))
if len(sentences) != 1:
# text = sentences[random.randint(0, len(sentences) - 1)]
text = sentences[0]
return text
class WikipediaCitationDataset(Dataset):
"""Dataset reader for citation_detection citation dataset
"""
def __init__(self, pos_data_loc: AnyStr, neg_data_loc: AnyStr, tokenizer: PreTrainedTokenizer):
super(WikipediaCitationDataset, self).__init__()
self.pos_data = pd.read_csv(pos_data_loc, sep='\t').fillna('')
self.neg_data = pd.read_csv(neg_data_loc, sep='\t').fillna('')
self.tokenizer = tokenizer
# Combine into one dataset w/ labels
self.dataset = pd.concat([self.pos_data, self.neg_data], axis=0, ignore_index=True)[['statement']]
# Normalize the strings
self.dataset['statement'] = self.dataset['statement'].str.normalize('NFKD')
# Extract the first sentence
self.dataset['statement'] = self.dataset['statement'].apply(get_first_sentence_redi_et_al)
# Add labels
self.dataset['label'] = [1] * self.pos_data.shape[0] + [0] * self.neg_data.shape[0]
self.dataset = self.dataset
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
return input_ids, mask, label, item
class PULearningWikipediaCitationDataset(Dataset):
"""Dataset reader for citation detection with positive unlabelled learning
"""
def __init__(
self,
base_dataset: Dataset,
validation_dataset: Dataset,
base_network: torch.nn.Module,
device: torch.device,
scale: int = 1.0
):
super(PULearningWikipediaCitationDataset, self).__init__()
# Subset is used as the dataset
if type(base_dataset) == torch.utils.data.Subset:
self.tokenizer = base_dataset.dataset.tokenizer
indices = base_dataset.indices
orig_dataset = base_dataset.dataset.dataset.copy()
base_dataset = base_dataset.dataset
base_dataset.dataset = orig_dataset.iloc[indices]
base_dataset.dataset = base_dataset.dataset.reset_index(drop=True)
# Only look at negative samples
original_dataset = base_dataset.dataset.copy()
base_dataset.dataset = base_dataset.dataset[base_dataset.dataset['label'] == 0]
# Set the label to 1
base_dataset.dataset['label'] = [1]*base_dataset.dataset.shape[0]
# Get negatives weight, combine into one dataset and duplicate the negatives
train_dl = torch.utils.data.DataLoader(base_dataset, batch_size=8, collate_fn=collate_batch_transformer)
val_dl = torch.utils.data.DataLoader(validation_dataset, batch_size=8, collate_fn=collate_batch_transformer)
neg_weights = get_negative_sample_weights(train_dl, val_dl, base_network, device)
assert neg_weights.shape == base_dataset.dataset.shape, "Should have double the number of negative sample weights"
weights = np.asarray([0.] * original_dataset.shape[0])
weights[original_dataset.index[original_dataset['label'] == 1].tolist()] = 1.
weights[original_dataset.index[original_dataset['label'] == 0].tolist()] = neg_weights[:,0]
original_dataset['weight'] = weights
duplicated_data = base_dataset.dataset.copy()
duplicated_data['weight'] = neg_weights[:,1]
self.dataset = pd.concat([original_dataset, duplicated_data], ignore_index=True)
self.scale = scale
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
weight = self.scale * row[2]
return input_ids, mask, label, weight, item
class PULearningPriorBasedConversionWikipediaCitationDataset(Dataset):
"""Dataset reader for citation detection with positive unlabelled learning and
positive-negative removal
"""
def __init__(
self,
base_dataset: Dataset,
validation_dataset: Dataset,
base_network: torch.nn.Module,
device: torch.device,
gamma: float,
scale: int = 1.0
):
super(PULearningPriorBasedConversionWikipediaCitationDataset, self).__init__()
train_dl = torch.utils.data.DataLoader(base_dataset, batch_size=8, collate_fn=collate_batch_transformer)
val_dl = torch.utils.data.DataLoader(validation_dataset, batch_size=8, collate_fn=collate_batch_transformer)
prior = estimate_class_prior_probability(base_network, train_dl, val_dl, device)
print(prior)
# Subset is used as the dataset
if type(base_dataset) == torch.utils.data.Subset:
self.tokenizer = base_dataset.dataset.tokenizer
indices = base_dataset.indices
orig_dataset = base_dataset.dataset.dataset.copy()
base_dataset = base_dataset.dataset
base_dataset.dataset = orig_dataset.iloc[indices]
base_dataset.dataset = base_dataset.dataset.reset_index(drop=True)
# Only look at negative samples
original_dataset = base_dataset.dataset.copy()
base_dataset.dataset = base_dataset.dataset[base_dataset.dataset['label'] == 0]
# Get negatives weight, combine into one dataset and duplicate the negatives
train_dl = torch.utils.data.DataLoader(base_dataset, batch_size=8, collate_fn=collate_batch_transformer)
val_dl = torch.utils.data.DataLoader(validation_dataset, batch_size=8, collate_fn=collate_batch_transformer)
neg_weights = get_negative_sample_weights(train_dl, val_dl, base_network, device)
assert neg_weights.shape == base_dataset.dataset.shape, "Should have double the number of negative sample weights"
positives = original_dataset[original_dataset['label'] == 1]
positives['weight'] = [1.] * positives.shape[0]
# Keep adding examples until p(y=1) equals our estimate
keep_examples = np.asarray([True] * neg_weights.shape[0])
ordered_idx = np.argsort(neg_weights[:,1])[::-1]
i = 0
while (positives.shape[0] + sum(~keep_examples)) / original_dataset.shape[0] < prior:
keep_examples[ordered_idx[i]] = False
i += 1
kept_negatives = base_dataset.dataset[keep_examples].copy()
kept_negatives_plus = kept_negatives.copy()
kept_negatives_plus['label'] = [1] * kept_negatives_plus.shape[0]
kept_negatives['weight'] = neg_weights[keep_examples, 0]
kept_negatives_plus['weight'] = neg_weights[keep_examples, 1]
converted_positives = base_dataset.dataset[~keep_examples].copy()
converted_positives['label'] = [1] * converted_positives.shape[0]
converted_positives['weight'] = [1.] * converted_positives.shape[0]
print(positives.shape)
print(kept_negatives.shape)
print(converted_positives.shape)
self.dataset = pd.concat([positives, kept_negatives, kept_negatives_plus, converted_positives],
ignore_index=True)
self.scale = scale
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
weight = self.scale * row[2]
return input_ids, mask, label, weight, item
class PHEMEClassifierDataset(Dataset):
"""Datareader for basic PHEME classification with no context
"""
def __init__(self, pheme_directory, tokenizer):
"""
:param pheme_directory: The root directory of the PHEME data
"""
super(PHEMEClassifierDataset, self).__init__()
rumours = []
non_rumours = []
self.name = pheme_directory.split('/')[-1].split('-')[0]
for source_tweet_file in glob.glob(f'{pheme_directory}/non-rumours/**/source-tweets/*.json'):
with open(source_tweet_file) as js:
tweet = json.load(js)
non_rumours.append(tweet['text'])
for source_tweet_file in glob.glob(f'{pheme_directory}/rumours/**/source-tweets/*.json'):
with open(source_tweet_file) as js:
tweet = json.load(js)
rumours.append(tweet['text'])
self.dataset = pd.DataFrame(rumours + non_rumours, columns=['statement'])
self.dataset['label'] = [1] * len(rumours) + [0] * len(non_rumours)
self.dataset['statement'] = self.dataset['statement'].str.normalize('NFKD')
self.tokenizer = tokenizer
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
return input_ids, mask, label, item
def get_row(self, row):
return self.dataset[row]
class PULearningPHEMEDataset(Dataset):
"""Dataset reader for citation detection with positive unlabelled learning
"""
def __init__(
self,
base_dataset: Dataset,
validation_dataset: Dataset,
base_network: torch.nn.Module,
device: torch.device,
scale: int = 1.0
):
super(PULearningPHEMEDataset, self).__init__()
# Subset is used as the dataset
if type(base_dataset) == torch.utils.data.Subset:
self.tokenizer = base_dataset.dataset.tokenizer
indices = base_dataset.indices
orig_dataset = base_dataset.dataset.dataset.copy()
base_dataset = base_dataset.dataset
base_dataset.dataset = orig_dataset.iloc[indices]
base_dataset.dataset = base_dataset.dataset.reset_index(drop=True)
else:
self.tokenizer = base_dataset.tokenizer
# Only look at negative samples
original_dataset = base_dataset.dataset.copy()
base_dataset.dataset = base_dataset.dataset[base_dataset.dataset['label'] == 0]
# Set the label to 1
base_dataset.dataset['label'] = [1] * base_dataset.dataset.shape[0]
# Get negatives weight, combine into one dataset and duplicate the negatives
train_dl = torch.utils.data.DataLoader(base_dataset, batch_size=8, collate_fn=collate_batch_transformer)
val_dl = torch.utils.data.DataLoader(validation_dataset, batch_size=8, collate_fn=collate_batch_transformer)
neg_weights = get_negative_sample_weights(train_dl, val_dl, base_network, device)
assert neg_weights.shape == base_dataset.dataset.shape, "Should have double the number of negative sample weights"
weights = np.asarray([0.] * original_dataset.shape[0])
weights[original_dataset.index[original_dataset['label'] == 1].tolist()] = 1.
weights[original_dataset.index[original_dataset['label'] == 0].tolist()] = neg_weights[:, 0]
original_dataset['weight'] = weights
duplicated_data = base_dataset.dataset.copy()
duplicated_data['weight'] = neg_weights[:, 1]
self.dataset = pd.concat([original_dataset, duplicated_data], ignore_index=True)
self.scale = scale
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
weight = self.scale * row[2]
return input_ids, mask, label, weight, item
class PULearningPriorBasedConversionPHEMEDataset(Dataset):
"""Dataset reader for citation detection with positive unlabelled learning and
positive-negative removal
"""
def __init__(
self,
base_dataset: Dataset,
validation_dataset: Dataset,
base_network: torch.nn.Module,
device: torch.device,
gamma: float = 1.0,
scale: int = 1.0
):
super(PULearningPriorBasedConversionPHEMEDataset, self).__init__()
train_dl = torch.utils.data.DataLoader(base_dataset, batch_size=8, collate_fn=collate_batch_transformer)
val_dl = torch.utils.data.DataLoader(validation_dataset, batch_size=8, collate_fn=collate_batch_transformer)
prior = estimate_class_prior_probability(base_network, train_dl, val_dl, device)
print(prior)
# Subset is used as the dataset
if type(base_dataset) == torch.utils.data.Subset:
self.tokenizer = base_dataset.dataset.tokenizer
indices = base_dataset.indices
orig_dataset = base_dataset.dataset.dataset.copy()
base_dataset = base_dataset.dataset
base_dataset.dataset = orig_dataset.iloc[indices]
base_dataset.dataset = base_dataset.dataset.reset_index(drop=True)
else:
self.tokenizer = base_dataset.tokenizer
# Only look at negative samples
original_dataset = base_dataset.dataset.copy()
base_dataset.dataset = base_dataset.dataset[base_dataset.dataset['label'] == 0]
# Get negatives weight, combine into one dataset and duplicate the negatives
train_dl = torch.utils.data.DataLoader(base_dataset, batch_size=8, collate_fn=collate_batch_transformer)
val_dl = torch.utils.data.DataLoader(validation_dataset, batch_size=8, collate_fn=collate_batch_transformer)
neg_weights = get_negative_sample_weights(train_dl, val_dl, base_network, device)
assert neg_weights.shape == base_dataset.dataset.shape, "Should have double the number of negative sample weights"
positives = original_dataset[original_dataset['label'] == 1]
positives['weight'] = [1.] * positives.shape[0]
# Keep adding examples until p(y=1) equals our estimate
keep_examples = np.asarray([True] * neg_weights.shape[0])
ordered_idx = np.argsort(neg_weights[:,1])[::-1]
i = 0
while (positives.shape[0] + sum(~keep_examples)) / original_dataset.shape[0] < prior:
keep_examples[ordered_idx[i]] = False
i += 1
kept_negatives = base_dataset.dataset[keep_examples].copy()
kept_negatives_plus = kept_negatives.copy()
kept_negatives_plus['label'] = [1] * kept_negatives_plus.shape[0]
kept_negatives['weight'] = neg_weights[keep_examples, 0]
kept_negatives_plus['weight'] = neg_weights[keep_examples, 1]
converted_positives = base_dataset.dataset[~keep_examples].copy()
converted_positives['label'] = [1] * converted_positives.shape[0]
converted_positives['weight'] = [1.] * converted_positives.shape[0]
print(positives.shape)
print(kept_negatives.shape)
print(converted_positives.shape)
self.dataset = pd.concat([positives, kept_negatives, kept_negatives_plus, converted_positives],
ignore_index=True)
self.scale = scale
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
weight = self.scale * row[2]
return input_ids, mask, label, weight, item
class ClefClassifierDataset(Dataset):
"""Datareader for basic Clef classification with no context
"""
def __init__(self, loc, tokenizer, name='clef_data'):
"""
:param loc: The root directory of the PHEME data
"""
super(ClefClassifierDataset, self).__init__()
data = []
if os.path.isdir(loc):
files = glob.glob(f'{loc}/*.tsv') + glob.glob(f'{loc}/*.txt')
else:
files = [loc]
for file in files:
with open(file) as f:
data.extend([l.strip().split('\t')[-2:] for l in f])
self.dataset = pd.DataFrame(data, columns=['statement', 'label'])
self.dataset['statement'] = self.dataset['statement'].str.normalize('NFKD')
self.dataset['label'] = pd.to_numeric(self.dataset['label'])
self.tokenizer = tokenizer
self.name = name
def __len__(self):
return self.dataset.shape[0]
def __getitem__(self, item) -> Tuple:
row = self.dataset.values[item]
input_ids, mask = text_to_batch_transformer([row[0]], self.tokenizer)
label = row[1]
return input_ids, mask, label, item