-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaria.py
362 lines (341 loc) · 13.9 KB
/
varia.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
# coding: utf-8
import sys
import subprocess
import pkg_resources
# Package installation borrowed from:
# https://stackoverflow.com/questions/12332975/installing-python-module-within-code/58040520#58040520
required = {'bs4', 'pandas'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
# implement pip as a subprocess:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', *missing])
import pandas as pd
from bs4 import BeautifulSoup as bs
import re
from collections import defaultdict
with open("varia.xml", "r", encoding="utf-8") as file:
# Read each line in the file, readlines() returns a list of lines
content = file.readlines()
# Combine the lines in the list into a string
content = "".join(content)
soup = bs(content, "xml")
with open("navneregister.xml", "r", encoding="utf-8") as file:
# Read each line in the file, readlines() returns a list of lines
content = file.readlines()
# Combine the lines in the list into a string
content = "".join(content)
registry = bs(content, "xml")
d = defaultdict(dict)
docDates,regDates,mscontentscount,biblcount = 0,0,0,0
i=0
print("Working...")
for item in soup.findAll("TEI"): # For every TEI element (every document) in the xml file:
itemDate = "N/A" # I don't think this is required
itemType = item['subtype'] # Check the subtype of the document
if itemType == "dedication" or itemType == "greeting": # Include dedications and greetings only
xmlid = item['xml:id'] # Find the XML ID of the document
#print(item["xml:id"])
# First off, try to find the title in the bibl element.
try: # Try clause because title = None will throw an exception
bibl = item.find("bibl")
title = bibl.find("title")
#print("Title in bibl element")
biblcount += 1
# If exception, there's no title in bibl. Look for title in msContents:
except:
bibl = item.find("msContents")
title = bibl.find("title")
#print("Title in msContents element")
mscontentscount += 1
# With the title, unwrap any subelements
for match in title.findAll():
match.unwrap()
newTitle = ""
for x in title.contents: # Split and recombine the title into 1 whole string
newTitle+=" "+x
newTitle = re.sub(' +', ' ',newTitle) # Remove double+ spaces
title = newTitle.strip() # Remove spaces around the title
title = re.sub('\n',"",title) # Remove \n
itemDate = item.find("docDate") # Look for the date in docDate element
try: # If no docDate, will throw exception
xmldate = itemDate['when']
docDates+=1
except: # No docDate? Look for date in date element
itemDate = item.find("date")
xmldate = itemDate['when']
regDates+=1
date = itemDate['when']
datenumbers = xmldate.replace("-","") # Get the numbers only from the date (1000-10-10 = 10001010)
# Because xml:ids are V(udat)1000-10-10ID
if "Vudat" in xmlid: # Prefix for some xml:ids
persid = xmlid.replace("Vudat","")
else:
persid = xmlid[1:] # Prefix for the rest of the xml:ids
persid = persid.replace(datenumbers,"")
if "-" in persid: # Indicates date range in xmlid.
persid = persid[3:] # Remove -XX left behind.
#print("\n")
# Collect all known information in dict d
d[xmlid]['title'] = title
d[xmlid]['type'] = itemType
d[xmlid]['date'] = date
d[xmlid]['ID'] = persid
#print(xmlid,title,itemType,date,"pe/orgID:",persid)
i+=1
print(i,"total","\ndocDate elements:",docDates,"\nregular date elements:",regDates,"\nmscontentscount:",mscontentscount,"\nbibl count:",biblcount)
iP=0 # Count of persons
iO=0 # Count of organizations
found_persons = [] # Applies to both, where we have a title with a positive ID identified
print("Working...")
for item in d: # For every dict entry
whoid = d[item]['ID'] # Get the ID
persid = "pe"+whoid # Test for personal ID
orgid = "org"+whoid # Test for organization ID
try: # Try clause because attempting to use None below will throw an exception
persdiv = registry.find('div', {'xml:id':persid}) # Test for personal ID
name = persdiv.find('persName') # Attempt to retrieve personal name
for match in name.findAll():
match.unwrap() # Removes subelements
newname = ""
for x in name.contents: # Can't use title.contents[0]. Making new string as workaround.
newname+=" "+x
name = newname.strip()
name = re.sub(' +', ' ',name)
#print("PERS - Title:",d[item]['title'],"To:",d[item]['ID'],"Registry:",name) # Report
iP+=1
found_persons.append(item)
d[item]['peID'] = "pe"+whoid
d[item]['clearname'] = name
# Person confirmed
except: # If an exception is thrown, it's not a person
try: # Test for organization ID
orgid = registry.find('div', {'xml:id':orgid})
name = orgid.find('item',{'rend':'name'})
for match in name.findAll():
match.unwrap()
newname = ""
for x in name.contents: # Can't use title.contents[0]. Making new string as workaround.
newname+=" "+x
name = newname.strip()
name = re.sub(' +', ' ',name)
#print("ORG - Title:",d[item]['title'],"To:",d[item]['ID'],"Registry:",name) # Report
iO+=1
found_persons.append(item)
d[item]['orgID'] = "org"+whoid
d[item]['clearname'] = name
except: # If an exception is thrown, it's not an organization
pass # Report or pass.
print(iP,"personal names\n",iO,"organizations")
losttitles = [] # List of items that do not have a person/organization's ID *and* is not attributed to an anon
for item in d:
if item not in found_persons and d[item]['ID'] != "NN":
#print(item,"not found. Title",d[item]['title'],"ID:",d[item]['ID'])
losttitles.append(d[item]['title'])
# Get full names and their IDs from the registry xml file
i=0
iddict = defaultdict(dict)
orgs = []
pers = []
for orgid in registry.findAll('div', {'xml:id':re.compile('org.*')}):
name = orgid.find('item',{'rend':'name'})
for match in name.findAll():
match.unwrap()
newname = ""
for x in name.contents: # Can't use title.contents[0]... New string as workaround.
newname+=" "+x
newname = re.sub('\\n',"",newname)
name = newname.strip()
name = re.sub(' +', ' ',name)
print(name)
i+=1
orgs.append(name)
iddict[name]["ID"] = orgid['xml:id']
print(i,"organizations")
i=0
for persid in registry.findAll('div', {'xml:id':re.compile('pe.*')}):
name = persid.find('item',{'rend':'name'})
for match in name.findAll():
match.unwrap()
newname = ""
for x in name.contents: # Can't use title.contents[0]... New string as workaround.
newname+=" "+x
name = newname
name = re.sub('\\n',"",name)
name = name.strip()
name = re.sub(' +', ' ',name)
print(name)
i+=1
iddict[name]["ID"] = persid['xml:id']
pers.append(name)
print(i,"persons")
# Macerate the list of missing titles in an attempt to extract the name(s) included in each title by preposition guessing.
# This method has a success rate of, like, 75%. It's not perfect, but extracts a reasonable number of names.
# I suspect that chances of success would increase by creating a list of terms that should just be excluded entirely -
# like a stop word list - and analysing more thoroughly where each name is placed in relation to a preposition.
titlesstillmissing,nodontadd,fulltitlestillmissing = [],[],[]
v=0
for x in losttitles:
z = x.strip()
#print("Mod 0:",z)
z = z.replace("Dedikasjon til","")
z = z.replace("Hilsen til","")
z = z.replace("Hilsener til","")
#print("Mod 1:",z)
if " i " in z:
a = z.split(" i ")
elif " på " in z:
a = z.split(" på ")
elif " til " in z:
a = z.split(" til ")
elif " bakpå " in z:
a = z.split(" bakpå ")
else:
a = [z]
#print("Neither",x)
if "Hilsen" in a[0]:
z = a[1]
else:
z = a[0]
#print(z)
z = z.strip()
#print("Mod 2:",z)
# Check the names that we got against the registry - organizations
for xx in orgs:
if z.lower() in xx.lower():
print("Mod 3O:",x,"/",z,"/",xx)
v+=1
nodontadd.append(z)
for document in d:
#print(x)
exists = d.get(document, {}).get('title')
if exists == x:
theid = iddict.get(xx)["ID"]
d[document]['orgID'] = theid
d[document]['clearname'] = xx.strip()
print(x,"/",xx.strip(),exists,"updated to ID",theid)
# Check the names that we got against the registry - persons
for xx in pers:
if z.lower() in xx.lower():
#print("Mod 3P:",x,"/",z,"/",xx)
v+=1
nodontadd.append(z)
for document in d:
#print(x)
exists = d.get(document, {}).get('title')
if exists == x:
# WARNING This will populate "Hilsen til Møller på baksiden av fotografi fra Forum Romanum"
# "Møller" is not a good match. In future, exclude this.
theid = iddict.get(xx)["ID"]
d[document]['peID'] = theid
d[document]['clearname'] = xx.strip()
print(x,"/",xx.strip(),exists,"updated to ID",theid)
if z not in nodontadd:
titlesstillmissing.append(z)
fulltitlestillmissing.append(x)
#print("")
print(v,"new entries")
# Debug and report step. Creates fullID column in the dictionary.
i=0
for x in d:
#print(x)
peID = d.get(x, {}).get('peID')
orgID = d.get(x, {}).get('orgID')
ID = d.get(x, {}).get('ID')
clearname = d.get(x, {}).get('clearname')
if peID:
i+=1
print("PEID",x,"=",ID,"/",peID,"=",clearname)
print(i)
d[x]['fullID'] = peID
elif orgID:
i+=1
print("ORGID",x,"=",ID,"/",orgID,"=",clearname)
print(i)
d[x]['fullID'] = orgID
# Token matching by Ruth. This is an additional effort to mash up titles that still haven't received a recipient.
# Uses the partial titles that were macerated based on proposition guessing earlier.
PossibleTokenMatchReg = []
LeftoverNames = []
for entry in titlesstillmissing:
string = re.sub('\(', '',(re.sub('\)', '', str(entry))))
stringLower = string.lower()
tokens = stringLower.split()
TokenMatchReg = []
for person in pers:
personLower = person.lower()
personTokens = re.sub('\(', '',(re.sub('\)', '',(re.sub('\]', '',(re.sub('\[', '', personLower)))))))
# personTokens = personTokens.split()
if all(x in personTokens for x in tokens):
index = titlesstillmissing.index(entry)
variaID = ''
for item in d:
if d[item]['title'] == fulltitlestillmissing[index]:
variaID = item
TokenMatchReg.append(variaID + ':' + fulltitlestillmissing[index] + '/' + person)
for org in orgs:
orgLower = org.lower()
orgTokens = re.sub('\(', '',(re.sub('\)', '',(re.sub('\]', '',(re.sub('\[', '', orgLower)))))))
if all(x in orgTokens for x in tokens):
index = titlesstillmissing.index(entry)
variaID = ''
for item in d:
if d[item]['title'] == fulltitlestillmissing[index]:
variaID = item
TokenMatchReg.append( variaID + ':' + fulltitlestillmissing[index] + '/' + orgTokens)
if len(TokenMatchReg) != 0:
PossibleTokenMatchReg.append(TokenMatchReg)
else:
LeftoverNames.append(entry)
print(PossibleTokenMatchReg)
# print(LeftoverNames)
# print(len(LeftoverNames))
# Match fullnames to iddict for ID
# Create list for inexact matches
idsfound,ambignames,ambigids,ambig,ambigdocs=[],[],[],[],[]
for x in PossibleTokenMatchReg:
for y in x:
z = y.split(":")
docID = z[0]
names = z[1].split("/")
name = names[1]
name = name.strip()
if docID in idsfound:
print("-AMBIGUOUS:",docID,name,iddict[name]['ID'])
ambig.append(docID)
else:
#print(docID,name)
idsfound.append(docID)
for x in PossibleTokenMatchReg:
for y in x:
z = y.split(":")
docID = z[0]
names = z[1].split("/")
name = names[1]
name = name.strip()
if docID not in ambig:
print("Match for",docID,name,iddict[name]['ID'])
d[docID]['fullID'] = iddict[name]['ID']
d[docID]['clearname'] = name
else: # Really I'd just skip this and commit them to a list/file
ambigdocs.append(docID)
ambignames.append(name)
ambigids.append(iddict[name]['ID'])
df = pd.DataFrame.from_dict(d).fillna("N/A").T
df = df.drop(['peID', 'orgID'], axis=1)
df.to_csv("varia_file.csv", sep=',', encoding='utf-8',index=True)
# What's going on with the ambigs, then?
i=0
while i<len(ambigdocs):
print(ambigdocs[i],ambignames[i],ambigids[i])
i+=1
i=0
for x in df['fullID']:
if x != "N/A":
i+=1
print(i,"entries have IDs")
i=0
for x in df['ID']:
i+=1
print(i,"entries")