-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatch_viaf.py
303 lines (164 loc) · 6.63 KB
/
match_viaf.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
# -*- encoding: utf-8 -*-
import time, requests, json, sys, csv
import xml.etree.ElementTree as etree
class viafSearch:
viafURL = 'http://viaf.org/viaf/search?query=local.personalNames+%3D+{SEARCH}&httpAccept=text/xml'
sourceFile = 'data/tulane-no-dupes.csv'
resultsFile = 'data/tulane_results.json'
results = {}
requestDelay = 0.1
def __init__(self):
self.processFile()
self.saveFile()
self.stats()
def requestProcess(self,searchVal):
r = requests.get(self.viafURL.replace('{SEARCH}',searchVal))
time.sleep(self.requestDelay)
results = []
if (r.status_code == 200):
try:
root = etree.fromstring(r.text)
except:
print ("Error in parsing the XML", "URL")
for records in root.findall('{http://www.loc.gov/zing/srw/}records'):
for record in records:
aResult = {}
aResult['birthDate'] = None
aResult['deathDate'] = None
aResult['altNames'] = []
aResult['sources'] = []
aResult['titles'] = []
for recordData in record.findall('{http://www.loc.gov/zing/srw/}recordData'):
for viafCluster in recordData.findall('{http://viaf.org/viaf/terms#}VIAFCluster'):
for el in viafCluster.findall('{http://viaf.org/viaf/terms#}birthDate'):
aResult['birthDate'] = el.text
for el in viafCluster.findall('{http://viaf.org/viaf/terms#}deathDate'):
aResult['deathDate'] = el.text
for mainHeadings in viafCluster.findall('{http://viaf.org/viaf/terms#}mainHeadings'):
for el in mainHeadings.findall('{http://viaf.org/viaf/terms#}data'):
aResult['altNames'].append((el[0].text))
for sources in viafCluster.findall('{http://viaf.org/viaf/terms#}sources'):
for el in sources:
aResult['sources'].append(el.text)
for titles in viafCluster.findall('{http://viaf.org/viaf/terms#}titles'):
for el in titles:
#they are contained in a sub work element that has a source and title
for workPart in el:
#is this the title element?
if (workPart.tag == '{http://viaf.org/viaf/terms#}title'):
aResult['titles'] .append(workPart.text)
results.append(aResult)
return results
else:
print ("Error in requesting", "URL")
return []
def processFile(self):
counter = 0
with open(self.sourceFile, 'r') as csvfile:
#dumps the file into the csv library with some info on how it is formatted
persons = csv.reader(csvfile, delimiter=',')
for row in persons:
fullName = row[0]
firstName = row[1]
lastName = row[2]
dobYear = row[3]
dodYear = row[4]
self.results[fullName] = {}
self.results[fullName]['tulane_name'] = fullName
self.results[fullName]['tulane_first'] = firstName
self.results[fullName]['tulane_last'] = lastName
self.results[fullName]['tulane_dob'] = dobYear
self.results[fullName]['tulane_dod'] = dodYear
self.results[fullName]['mapping'] = []
#here is a line of data from tulane
counter = counter + 1
print ("On ", counter)
self.processPerson(fullName)
#save
if counter % 25 == 0:
self.saveFile()
def processPerson(self,fullName):
print (self.results[fullName]['tulane_name'])
searchResult = []
searchString = self.results[fullName]['tulane_name']
if (self.results[fullName]['tulane_dob'] != 'NULL'):
searchString += " " + self.results[fullName]['tulane_dob']
#search with the DOB one way
searchResult = self.requestProcess(searchString)
print ("\t",searchString,"Found:",len(searchResult))
if (len(searchResult) == 0):
#try another format that VIAF search engine seems to like sometimes
searchString = self.results[fullName]['tulane_name'] + "," + self.results[fullName]['tulane_dob']
print ("\t",searchString,"Found:",len(searchResult))
#if no luck with that try regular name search
if (len(searchResult) == 0):
searchString = self.results[fullName]['tulane_name']
searchResult = self.requestProcess(searchString)
print ("\t",searchString,"Found:",len(searchResult))
quality = "low"
#lets qualify a little bit
if(self.results[fullName]['tulane_dob'] != 'NULL' and len(searchResult) == 1):
quality = 'high'
if(self.results[fullName]['tulane_dob'] == 'NULL' and len(searchResult) == 1):
quality = 'medium'
if(len(searchResult) > 1):
#Bill's attempt to find whitelisted terms in the "titles" field
#set up a whitelist of terms
terms = ['jazz', 'new orleans', 'ragtime', 'gospel', 'blues',]
#set up the new whitelist results list
whitelistResult = []
for aResult in searchResult:
titles = aResult['titles']
names = aResult['altNames']
#titles is an array, loop through it
for aTitle in titles:
#check if there's a whitelisted term in the "titles" results
for term in terms:
if (aTitle.lower().find(term) != -1):
#check if the names match at least a little
#if self.results[fullName]['tulane_last'] in names:
#then add the result to the new list if it's not already there
if aResult not in whitelistResult:
whitelistResult.append(aResult)
#if our new results list has one or more results, replace the original searchResult
if(len(whitelistResult) > 0):
searchResult = whitelistResult
#after all that if we narrowed it down to a single person set it to high probability
if(len(searchResult) == 1):
print ("THE WHITELIST WORKED ON THIS ONE!!!")
print (searchResult)
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
quality = 'high'
if(len(searchResult) == 0):
quality = 'none'
if(len(searchResult) == 100):
searchResult = []
quality = 'manual'
self.results[fullName]['mapping'] = searchResult
self.results[fullName]['mapping_quality'] = quality
#safe it after each try, just to see the progress and not have to wait till the end to check it
self.saveFile()
def saveFile(self):
f = open(self.resultsFile, "w")
f.write(json.dumps(self.results, indent=4, sort_keys=True))
def stats(self):
matchTypes = {}
manyMatchCount = {}
with open(self.resultsFile, 'r') as jsonFile:
results = json.loads(jsonFile.read())
for aResult in results:
x = results[aResult]
if x['mapping_quality'] not in matchTypes:
matchTypes[x['mapping_quality']] = 1
else:
matchTypes[x['mapping_quality']] += 1
if x['mapping_quality'] == 'many':
if len(x['mapping']) not in manyMatchCount:
manyMatchCount[len(x['mapping'])] = 1
else:
manyMatchCount[len(x['mapping'])] += 1
print (matchTypes)
for x in manyMatchCount:
print (x,"|",manyMatchCount[x])
if __name__ == "__main__":
v = viafSearch()