forked from mderdun/indicator-leighhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVizPrep01.py
290 lines (258 loc) · 16.3 KB
/
VizPrep01.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import xml.etree.ElementTree as tree
import json
from os import listdir # install the "listdir" package (pip install dirlist)
from os.path import isfile, join
files =[]
dir = "Structural Versions"
for file in listdir(dir):
if isfile(join(dir, file)):
files.append(dir + "/" + file)
for f in files:
if ".xml" not in f:
files.remove(f)
files.sort()
result = {}
def compile():
for file in files:
result[file] = {}
root = tree.parse(file)
string = ""
no = ""
bibtitle = ""
nestedText= []
#hText = ""
for el in root.findall(".//{http://www.tei-c.org/ns/1.0}div2"):
n = el.get('n')
sectname = el.get("type")
if sectname is not None: #gets the section without an "n" attribute
if n is None and sectname != "title":
section = sectname
result[file][section] = {}
result[file][section]["header"] = ""
result[file][section]["subheader"] = ""
result[file][section]["title"] = []
result[file][section]["bibl"] = {}
result[file][section]["bibl"]["title"] = {}
result[file][section]["persons"] = {}
result[file][section]["persons"]["real"] = {}
result[file][section]["places"] = {}
result[file][section]["org"] = {}
result[file][section]["dates"] = {}
for e in el.findall("{http://www.tei-c.org/ns/1.0}head[@type='header']"):
ar =[]
header = ""
if e.find("./*") is None:
result[file][section]["header"] = e.text
else:
nestedText.append(e.text)
for x in e.findall("./*"):
if x.tag != "{http://www.tei-c.org/ns/1.0}note":
nestedText.append(x.text)
ar = x.findall("./*")
if len(ar) > 0:
string = ar[-1].text
if string != "":
nestedText.append(string)
after = x.tail
nestedText.append(after)
for i in nestedText:
if i is None:
nestedText.remove(i)
header = "".join(nestedText)
result[file][section]["header"] = header
for e in el.findall("{http://www.tei-c.org/ns/1.0}head[@type='subheader']"):
string = e.text
if string is not None:
result[file][section]["subheader"] = e.text
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}title"):
altstring = ""
expan = e.find("{http://www.tei-c.org/ns/1.0}choice/{http://www.tei-c.org/ns/1.0}expan")
if expan is not None:
altstring = expan.text
string = e.text
if altstring is not None and altstring != "" and altstring not in result[file][section]["title"]:
result[file][section]["title"].append(altstring)
if string is not None and string != "" and string not in result[file][section]["title"]:
result[file][section]["title"].append(string)
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}persName[@type = 'real']"):
key = e.get('key')
string = e.get('ref')
if key is not None and string is not None:
result[file][section]["persons"]["real"][key] = string
elif key is not None and string is None:
result[file][section]["persons"]["real"][key] = key
elif key is None:
string = el.text
result[file][section]["persons"]["real"][string] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}persName[@type = 'fictional']"):
result[file][section]["persons"]["fictional"] = []
if e.get('corresp') is not None:
no = e.text
if e.text != no:
string = e.text
if string not in result[file][section]["persons"]["fictional"]:
result[file][section]["persons"]["fictional"].append(string)
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}bibl"):
altstring = ""
head = e.find("{http://www.tei-c.org/ns/1.0}title")
if head is not None:
expan = head.find("{http://www.tei-c.org/ns/1.0}choice/{http://www.tei-c.org/ns/1.0}expan")
bibtitle = head.text
if expan is not None:
altstring = expan.text
if bibtitle is not None and bibtitle != "":
result[file][section]["bibl"]["title"][bibtitle] = {}
if altstring is not None and altstring != "":
bibtitle = altstring
result[file][section]["bibl"]["title"][bibtitle] = {}
author = e.find("{http://www.tei-c.org/ns/1.0}author")
if author is not None:
string = author.text
if string is not None:
result[file][section]["bibl"]["title"][bibtitle]["author"] = string
ref = e.find("{http://www.tei-c.org/ns/1.0}ref")
if ref is not None:
target = ref.get('target')
string = target
if string is not None:
if bibtitle != "":
result[file][section]["bibl"]["title"][bibtitle]["ref"] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}placeName[@key]"):
result[file][section]["places"] = {}
string = e.get('key')
ref = e.get('ref')
result[file][section]["places"][string] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}orgName"):
result[file][section]["org"] = {}
key = e.get('key')
string = e.get('ref')
if key is not None and string is not None:
result[file][section]["org"][key] = string
else:
string = e.text
result[file][section]["org"][string] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}date"):
result[file][section]["dates"] = {}
when = e.get('when')
string = e.text
if when is not None:
result[file][section]["dates"][when] = string
elif n is not None and sectname != "title": #gets the section with an "n" attribute
section = sectname + " " + n
result[file][section] = {}
result[file][section]["header"] = ""
result[file][section]["subheader"] = ""
result[file][section]["title"] = []
result[file][section]["bibl"] = {}
result[file][section]["bibl"]["title"] = {}
result[file][section]["persons"] = {}
result[file][section]["persons"]["real"] = {}
result[file][section]["places"] = {}
result[file][section]["org"] = {}
result[file][section]["dates"] = {}
for e in el.findall("{http://www.tei-c.org/ns/1.0}head[@type='header']"):
ar =[]
header = ""
if e.find("./*") is None:
result[file][section]["header"] = e.text
else:
nestedText.append(e.text)
for x in e.findall("./*"):
if x.tag != "{http://www.tei-c.org/ns/1.0}note":
nestedText.append(x.text)
ar = x.findall("./*")
if len(ar) > 0:
string = ar[-1].text
if string != "":
nestedText.append(string)
after = x.tail
nestedText.append(after)
for i in nestedText:
if i is None:
nestedText.remove(i)
header = "".join(nestedText)
result[file][section]["header"] = header
for e in el.findall("{http://www.tei-c.org/ns/1.0}head[@type='subheader']"):
string = e.text
if string is not None:
result[file][section]["subheader"] = e.text
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}title"):
altstring = ""
expan = e.find("{http://www.tei-c.org/ns/1.0}choice/{http://www.tei-c.org/ns/1.0}expan")
if expan is not None:
altstring = expan.text
string = e.text
if altstring is not None and altstring != "" and altstring not in result[file][section]["title"]:
result[file][section]["title"].append(altstring)
if string is not None and string != "" and string not in result[file][section]["title"]:
result[file][section]["title"].append(string)
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}persName[@type = 'real']"):
key = e.get('key')
string = e.get('ref')
if key is not None and string is not None:
result[file][section]["persons"]["real"][key] = string
elif key is not None and string is None:
result[file][section]["persons"]["real"][key] = key
elif key is None:
string = el.text
result[file][section]["persons"]["real"][string] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}persName[@type = 'fictional']"):
result[file][section]["persons"]["fictional"] = []
if e.get('corresp') is not None:
no = e.text
if e.text != no:
string = e.text
if string not in result[file][section]["persons"]["fictional"]:
result[file][section]["persons"]["fictional"].append(string)
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}bibl"):
altstring = ""
head = e.find("{http://www.tei-c.org/ns/1.0}title")
if head is not None:
expan = head.find("{http://www.tei-c.org/ns/1.0}choice/{http://www.tei-c.org/ns/1.0}expan")
bibtitle = head.text
if expan is not None:
altstring = expan.text
if bibtitle is not None and bibtitle != "":
result[file][section]["bibl"]["title"][bibtitle] = {}
if altstring is not None and altstring != "":
bibtitle = altstring
result[file][section]["bibl"]["title"][bibtitle] = {}
author = e.find("{http://www.tei-c.org/ns/1.0}author")
if author is not None:
string = author.text
if string is not None:
result[file][section]["bibl"]["title"][bibtitle]["author"] = string
ref = e.find("{http://www.tei-c.org/ns/1.0}ref")
if ref is not None:
target = ref.get('target')
string = target
if string is not None:
if bibtitle != "":
result[file][section]["bibl"]["title"][bibtitle]["ref"] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}placeName[@key]"):
result[file][section]["places"] = {}
string = e.get('key')
ref = e.get('ref')
result[file][section]["places"][string] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}orgName"):
result[file][section]["org"] = {}
key = e.get('key')
string = e.get('ref')
if key is not None and string is not None:
result[file][section]["org"][key] = string
else:
string = e.text
result[file][section]["org"][string] = string
for e in el.findall(".//{http://www.tei-c.org/ns/1.0}date"):
result[file][section]["dates"] = {}
when = e.get('when')
string = e.text
if when is not None:
result[file][section]["dates"][when] = string
return result
jsonfile = compile()
print(jsonfile)
json_obj = json.dumps(jsonfile, indent=10, ensure_ascii = False)
with open("Hunt_data01.json", "w") as outfile:
outfile.write(json_obj)
print("Done!")