-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_create_html_visualization.py
108 lines (97 loc) · 3.37 KB
/
05_create_html_visualization.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
# Create html outputfile from JSON inputfile with QA data to visualize questions and answers in the text.
from json2html import *
import json
import sys
MAX_PARAGRAPHS = 5
MAX_QUESTIONS = 3
inputfile = 'train_transl_mapped_dropped.json' #sys.argv[1]
outputfile = 'html.html' #sys.argv[2]
#load input json file
def load_file(file):
with open(file, "r", encoding='utf-8') as read_file:
data = json.load(read_file)
print('--DATA SUCCESSFULLY LOADED--')
return data
# save data to json file
def save_file(data, filename):
with open(filename, 'w', encoding='utf-8') as f:
f.write(data)
print('--DATA SUCCESSFULLY SAVED--')
def main():
data = load_file(inputfile)
html = """<!DOCTYPE html>
<html>
<head>
<style>
table {
border: 1px solid black;
width:100%;
border-collapse: collapse;
float: left;
margin-bottom: 50px;
background-color:#ccebff;
}
td {
border: 1px solid black;
widht:80%;
}
th {
border: 1px solid black;
width:20%;
}
h1 {
text-align: center;
background-color:#0099FF;
color:#ffffff;
padding: 15px 0px 15px 0px;
}
h2 {
text-align: left;
font-weight: bold;
color: #ffffff;
}
body {
background-color:#80CCFF;
}
</style>
</head>
<body>
<h1>Question Answering</h1>"""
for i in range(0, len(data['data'])):
topic = data['data'][i]['title']
html=html+"""
<h2>""" + topic + "</h2>"
num_paragraphs = min(len(data['data'][i]['paragraphs']), MAX_PARAGRAPHS)
for j in range(0, num_paragraphs):
context = data['data'][i]['paragraphs'][j]['context']
num_questions = min(len(data['data'][i]['paragraphs'][j]['qas']), MAX_QUESTIONS)
for k in range(0, num_questions):
question = data['data'][i]['paragraphs'][j]['qas'][k]['question']
num_answers = min(1, len(data['data'][i]['paragraphs'][j]['qas'][k]['answers']))
for l in range(0, num_answers):
answer = data['data'][i]['paragraphs'][j]['qas'][k]['answers'][l]['text']
start_idx = data['data'][i]['paragraphs'][j]['qas'][k]['answers'][l]['answer_start']
end_idx = start_idx + len(answer)
html=html+"""
<table>
<tr>
<th>CONTEXT</th>
<td>""" + context[0:start_idx] + "<font color='red'>" + context[start_idx:end_idx] + "</font>" + context[end_idx:len(context)] + """</td>
</tr>
<tr>
<th>QUESTION</th>
<td>""" + question + """</td>
</tr>
<tr>
<th>ANSWER</th>
<td>""" + answer + """</td>
</tr>
</table>"""
html = html + """
</body>
</html>"""
#html=html+""""""
print('--HTML CREATED CREATED--')
save_file(html, outputfile)
if __name__ == "__main__":
main()