-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
69 lines (47 loc) · 1.67 KB
/
dataset.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
import json
import pickle as pkl
import logging
ds_logger = logging.getLogger(name='dataset')
class JSONLDataset:
def __init__(self, filepath):
self.filepath = filepath
self.examples = []
with open(filepath, 'r') as file:
for l in file:
self.examples.append(json.loads(l))
def __getitem__(self, key):
return self.examples[key]
def __len__(self):
return len(self.examples)
class PickleDataset:
def __init__(self, filepath):
self.filepath = filepath
self.examples = []
with open(filepath, 'rb') as file:
self.examples = list(pkl.load(file).values())
def __getitem__(self, key):
return self.examples[key]
def __len__(self):
return len(self.examples)
class TabularDataset:
def __init__(self, filepath, header=True, delimiter=','):
self.filepath = filepath
self.examples = []
with open(filepath, 'r') as file:
lines = file.readlines()
keys = None
if header:
keys, lines = lines[0].strip().split(delimiter), lines[1:]
for i, l in enumerate(lines):
row = l.strip().split(delimiter)
if keys and (len(row) != len(keys)):
ds_logger.error(f'Row no. {i} could not be parsed. continuing.')
continue
example = row
if keys:
example = {k: v for k,v in zip(keys, row)}
self.examples.append(example)
def __getitem__(self, key):
return self.examples[key]
def __len__(self):
return len(self.examples)