forked from DebadityaPal/RoBERTa-NL2SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.py
152 lines (123 loc) · 5.22 KB
/
load_data.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
import json
import torch
import os
import json
from matplotlib.pylab import *
def get_data(file_path: str,batch_size: int):
'''
Gets data from the dataset and creates a data loader
Arguments:
file_path: The path to the directory in which the dataset is contained
batch_size: Batch size to be used for the data loaders
Returns:
train_data: Training dataset (Natural Language utterances)
train_table: Training tables (Table schema and table data)
dev_data: Development dataset (Natural Language utterances)
dev_table: Development tables (Table schema and table data)
train_loader: Training dataset loader
dev_loader: Development dataset loader
'''
# Loading Dev Files(Development Dataset)
dev_data = []
dev_table = {}
with open(file_path + '/dev_knowledge.jsonl') as dev_data_file:
for idx, line in enumerate(dev_data_file):
current_line = json.loads(line.strip())
dev_data.append(current_line)
with open(file_path + '/dev.tables.jsonl') as dev_table_file:
for idx, line in enumerate(dev_table_file):
current_line = json.loads(line.strip())
dev_table[current_line['id']] = current_line
# Loading Train Files(Training Dataset)
train_data = []
train_table = {}
with open(file_path + '/train_knowledge.jsonl') as train_data_file:
for idx, line in enumerate(train_data_file):
current_line = json.loads(line.strip())
train_data.append(current_line)
with open(file_path + '/train.tables.jsonl') as train_table_file:
for idx, line in enumerate(train_table_file):
current_line = json.loads(line.strip())
train_table[current_line['id']] = current_line
train_loader = torch.utils.data.DataLoader(
batch_size=batch_size,
dataset=train_data,
shuffle=True,
num_workers=4,
collate_fn=lambda x: x # now dictionary values are not merged!
)
dev_loader = torch.utils.data.DataLoader(
batch_size=batch_size,
dataset=dev_data,
shuffle=True,
num_workers=4,
collate_fn=lambda x: x # now dictionary values are not merged!
)
return train_data, train_table, dev_data, dev_table, train_loader, dev_loader
def get_test_data(file_path: str,batch_size: int):
test_data=[]
test_table = {}
with open(file_path + '/test_knowledge.jsonl') as test_data_file:
for idx, line in enumerate(test_data_file):
current_line = json.loads(line.strip())
test_data.append(current_line)
with open(file_path + '/test.tables.jsonl') as test_table_file:
for idx, line in enumerate(test_table_file):
current_line = json.loads(line.strip())
test_table[current_line['id']] = current_line
test_loader = torch.utils.data.DataLoader(
batch_size=batch_size,
dataset=test_data,
shuffle=True,
num_workers=4,
collate_fn=lambda x: x # now dictionary values are not merged!
)
return test_data,test_table,test_loader
def get_zero_data(file_path: str,batch_size: int):
test_data=[]
test_table = {}
with open(file_path + '/zero.jsonl') as test_data_file:
for idx, line in enumerate(test_data_file):
current_line = json.loads(line.strip())
test_data.append(current_line)
with open(file_path + '/test.tables.jsonl') as test_table_file:
for idx, line in enumerate(test_table_file):
current_line = json.loads(line.strip())
test_table[current_line['id']] = current_line
test_loader = torch.utils.data.DataLoader(
batch_size=batch_size,
dataset=test_data,
shuffle=True,
num_workers=4,
collate_fn=lambda x: x # now dictionary values are not merged!
)
return test_data,test_table,test_loader
def get_fields(data, header_tokenization=False, sql_tokenization=False):
natural_language_utterance = []
tokenized_natural_language_utterance = []
sql_indexing = []
sql_query = []
tokenized_sql_query = []
table_indices = []
tokenized_headers = []
headers = []
for one_data in data:
natural_language_utterance.append(one_data['question'])
tokenized_natural_language_utterance.append(one_data['question_tok'])
sql_indexing.append(one_data['sql'])
sql_query.append(one_data['query'])
headers.append(one_data['header'])
table_indices.append({
"id" : one_data["table_id"],
"header": one_data["header"],
"types" : one_data["types"]
})
if sql_tokenization:
tokenized_sql_query.append(one_data['query_tok'])
else:
tokenized_sql_query.append(None)
if header_tokenization:
tokenized_headers.append(one_data['header_tok'])
else:
tokenized_headers.append(None)
return natural_language_utterance,tokenized_natural_language_utterance,sql_indexing,sql_query,tokenized_sql_query,table_indices,tokenized_headers,headers