-
Notifications
You must be signed in to change notification settings - Fork 8
/
json_to_para.py
142 lines (119 loc) · 3.95 KB
/
json_to_para.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
import sys
import os
import requests
import re,csv
import json
import numpy as np
from bs4 import BeautifulSoup
from collections import OrderedDict
import inflect
inflect = inflect.engine()
import re, datetime
import pandas as pd
import argparse
import random
def is_date(string):
match = re.search('\d{4}-\d{2}-\d{2}', string)
if match:
date = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
return True
else:
return False
def write_csv(data,split):
with open(args['save_dir']+split+'.tsv', 'at') as outfile:
writer = csv.writer(outfile,delimiter='\t')
writer.writerow(data)
def config(parser):
parser.add_argument('--json_dir', default="./../../data/tables/json/", type=str)
parser.add_argument('--data_dir', default="./../../data/infotabs_tsv/", type=str)
parser.add_argument('--save_dir', default="./../../temp/parapremise", type=str)
parser.add_argument('--splits',default=["train","dev","test_alpha1","test_alpha2","test_alpha3"], action='store', type=str, nargs='*')
parser.add_argument('--rand_prem', default=0, type=int)
#parser.add_argument('--multi_gpu_on', action='store_true')
return parser
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser = config(parser)
args = vars(parser.parse_args())
for split in args["splits"]:
data = pd.read_csv(args['data_dir']+"infotabs_"+split+".tsv",sep="\t")
with open(args['save_dir']+split+".tsv", 'wt') as out:
writer = csv.writer(out, delimiter='\t')
writer.writerow(["index","table_id","annotator_id","premise","hypothesis","label"])
if args['rand_prem'] == 2:
table_ids = []
for index,row in data.iterrows():
table_ids += [row['table_id']]
random.shuffle(table_ids)
for index,row in data.iterrows():
row['table_id'] = table_ids[index]
if args['rand_prem'] == 1:
table_ids = []
for index,row in data.iterrows():
table_ids += [row['table_id']]
set_of_orignal = list(set(table_ids))
set_of_random = set_of_orignal
random.shuffle(set_of_random)
set_of_orignal = list(set(table_ids))
random_mapping_tableids = {}
jindex = 0
for key in set_of_orignal:
random_mapping_tableids[key] = set_of_random[jindex]
jindex += 1
for index,row in data.iterrows():
table_id = row['table_id']
row['table_id'] = random_mapping_tableids[table_id]
for index,row in data.iterrows():
file = args['json_dir'] +str(row['table_id'])+".json"
json_file = open(file,"r")
data = json.load(json_file,object_pairs_hook=OrderedDict)
try:
title = data["title"][0]
except KeyError:
print(row)
exit()
del data["title"]
para = ""
for key in data:
line = ""
values = data[key]
if (len(values) > 1) or (inflect.plural_noun(key)):
verb_use = "are"
if is_date("".join(values)):
para += title+" was "+ str(key) +" on "
line += title+" was "+ str(key) +" on "
else:
try:
para += "The "+str(key)+" of "+title+" "+verb_use+" "
line += "The "+str(key)+" of "+title+" "+verb_use+" "
except TypeError:
print(row)
print(key)
print(title)
exit()
for value in values[:-1]:
para += value +", "
line += value +", "
if len(values) > 1:
para += "and "+values[-1] + ". "
line += "and "+values[-1] + ". "
else:
para += values[-1] + ". "
line += values[-1] + ". "
else:
verb_use = "is"
if is_date(values[0]):
para += title+" was "+ str(key) +" on "+values[0] +". "
line += title+" was "+ str(key) +" on "+values[0] +". "
else:
para +="The "+str(key)+" of "+title+" "+verb_use+" "+values[0] +". "
line +="The "+str(key)+" of "+title+" "+verb_use+" "+values[0] +". "
label = row["label"]
if row["label"] == "E":
label = 0
if row["label"] == "N":
label = 1
if row["label"] == "C":
label = 2
data = [index,row['table_id'],row['annotater_id'],para,row["hypothesis"],label]
write_csv(data,split)