-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
36 lines (27 loc) · 982 Bytes
/
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
#coding:utf-8
from torch.utils.data import Dataset
from tqdm import tqdm
import json
from transformers import BertTokenizer
import torch
class ZhWikipediaDataSet(Dataset):
def __init__(self, filepath='',is_train = True,mini_test = True):
self.mini_test = mini_test
self.dataset = self.load_json_data(filepath)
def load_json_data(self,filename):
error_cnt = 0
tmp_dataset = []
with open(filename,'r',encoding='utf-8') as lines:
for idx,line in enumerate(lines):
if self.mini_test and idx>100:
break
try:
data = json.loads(line.strip())
tmp_dataset.append(data)
except Exception as e:
error_cnt+=1
return tmp_dataset
def __getitem__(self, index):
return self.dataset[index]
def __len__(self):
return len(self.dataset)