-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson2csv.py
157 lines (140 loc) · 6.29 KB
/
json2csv.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
import json
import pandas as pd
from jinja2.nativetypes import NativeEnvironment
import os
import sys
import itertools
from collections import OrderedDict
from tqdm import tqdm
def json2csv(filename, domain, overwrite=False):
# Exit if file exists.
_, tail = os.path.split(filename)
save_pth = os.path.join(f"outputs/bbnli/{domain}",
tail.replace("json", "csv"))
if os.path.exists(save_pth):
print(f"{save_pth} already exits.")
if not overwrite:
return
else:
print("Overwriting.")
# Read the json file.
print(f"Reading {filename}.")
with open(filename, "r") as f:
inp = json.load(f)
env = NativeEnvironment()
# Populate a csv with all possible (premise, hypothesis) combinations.
group1 = inp["GROUP1"][0]
group2 = inp["GROUP2"][0]
data = OrderedDict(inp['data'])
name = inp["name"].replace(" ", "_")
df = pd.DataFrame(columns=["domain",
"name",
"id",
"type",
"unique_id",
"premise",
"hypothesis_type",
"hypothesis",
"question",
"true_label",
"bias_label",
"reference"
])
# Pairwise cross product data
variables = list(itertools.product(*list(data.values())))
keys = list(data.keys())
print("Iterating over premises...")
for ind,pre in enumerate(tqdm(inp['premise'], total=len(inp['premise']))):
for datum in variables:
m = dict(zip(keys, datum))
m["GROUP1"] = group1
m["GROUP2"] = group2
# Replace variables in premise.
pre_pro = env.from_string(pre).render(**m)
for ti, (test_hyp,test_hyp_l) in enumerate(inp['test_hypothesis']):
# Replace variables in hypothesis.
test_hyp = env.from_string(test_hyp).render(**m)
# Replace variables in question.
qq, _ = inp['test_question'][ti]
qq = env.from_string(qq).render(**m)
df.loc[len(df)] = [inp['domain'],
name,
f"{ind:02}",
"pro",
name+"_pro_"+f"{ind:02}",
pre_pro,
"test",
test_hyp,
qq,
test_hyp_l,
-1,
inp['reference'][ind]]
for ti,(hyp, label, bias_label) in enumerate(inp['bias_hypothesis_stereotypical']):
# Replace variables in hypothesis.
hyp = env.from_string(hyp).render(**m)
# Replace variables in question.
qq, *_ = inp['bias_question_stereotypical'][ti]
qq = env.from_string(qq).render(**m)
df.loc[len(df)] = [inp['domain'],
name,
f"{ind:02}",
"pro",
name+"_pro_"+f"{ind:02}",
pre_pro,
"stereotypical",
hyp,
qq,
label,
bias_label,
inp['reference'][ind]]
# Now anti-stereotypical formulation where group1 and group2 are swapped
# in both premises and hypotheses.
m["GROUP1"] = group2
m["GROUP2"] = group1
pre_anti = env.from_string(pre).render(**m)
for ti, (test_hyp,test_hyp_l) in enumerate(inp['test_hypothesis']):
# Replace variables in hypothesis.
test_hyp = env.from_string(test_hyp).render(**m)
# Replace variables in question.
qq, *_ = inp['test_question'][ti]
qq = env.from_string(qq).render(**m)
df.loc[len(df)] = [inp['domain'],
name,
f"{ind:02}",
"anti",
name+"_anti_"+f"{ind:02}",
pre_anti,
"test",
test_hyp,
qq,
test_hyp_l,
-1,
inp['reference'][ind]]
for ti, (hyp, label, bias_label) in enumerate(inp['bias_hypothesis_stereotypical']):
# Replace variables in hypothesis.
hyp = env.from_string(hyp).render(**m)
# Replace variables in question.
qq, *_ = inp['bias_question_stereotypical'][ti]
qq = env.from_string(qq).render(**m)
df.loc[len(df)] = [inp['domain'],
name,
f"{ind:02}",
"anti",
name+"_anti_"+f"{ind:02}",
pre_anti,
"anti-stereotypical",
hyp,
qq,
label,
bias_label,
inp['reference'][ind]]
df = df.drop_duplicates()
df = df.reset_index()
df.columns = ["Index"] + df.columns[1:].tolist()
print(f"Length of the resulting full csv: {len(df)}.")
df.to_csv(save_pth, index=False)
if __name__ == "__main__":
filename = sys.argv[1] # e.g. "data/nli/gender/man_is_to_programmer.json"
domain = sys.argv[2]
overwrite = sys.argv[3] == "--overwrite"
json2csv(filename, domain, overwrite)