-
Notifications
You must be signed in to change notification settings - Fork 2
/
ead2rdf.py
180 lines (152 loc) · 6.78 KB
/
ead2rdf.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
from lxml import etree
import os
import glob
import sys
import codecs
import re
from utils import *
from ead import Ead
from time import localtime, strftime
from optparse import OptionParser
def printComponents(components):
for component in components:
if 'arch:hasParent' in component.metadata.keys():
print f + "|" + component.metadata['dc:identifier'].encode('utf-8') + ": " + component.metadata['dct:title'].encode('utf-8') + "|hasParent|" + component.metadata['arch:hasParent']
else:
print f + "|" + component.metadata['dc:identifier'].encode('utf-8') + ": " + component.metadata['dct:title'].encode('utf-8') + "|hasParent|Top Level"
if component.components: printComponents(component.components)
def main(opts):
lookups = codecs.open(opts.lookups, 'w', encoding='utf-8')
#iterate thr.ough files
#Or the config file
entitySet = {}
count = 0
for f in glob.glob( os.path.join(opts.sourcedir, '*.xml') ):
if opts.limit and count == opts.limit:
break
#print f
#innerli = []
#print "current file is: " + infile
#tree = ET.ElementTree(file=f)
#print '{0} -> {1}'.format(file, tree.getroot())
#if generate_rdf(tree.getroot()) == " ":
# print '{0}|{1}'.format(f, generate_rdf(tree.getroot()))
time = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
print 'Starttime: {0}: {1}'.format(f, time)
try:
root = etree.parse(f)
ead = Ead(root, f)
for e in ead.entities:
#print dir(e)
key = e.metadata["id"]
if key not in entitySet:
entitySet[key] = e
#pri.nt key + " added to entitySet."
else:
#print "You already have a " + key
if e.collections[0] not in entitySet[key].collections:
entitySet[key].collections.extend(e.collections)
if e.headings[0] not in entitySet[key].headings:
entitySet[key].headings.extend(e.headings)
#print entitySet[key].collections
#print entitySet[key].headings
#print f
#if f == "ALBA.PHOTO.015-ead.xml":
#print 'Title: {0}; ID: {1}; Access Restrictions: {2}'.format(ead.title.encode('utf-8'), ead.identifier.encode('utf-8'), ead.restrictions.encode('utf-8'))
'''
for k in ead.metadata:
print k
for k, v in ead.metadata.iteritems():
#print type(v)
if type(v) == unicode or type(v) == str:
print " -{0}: {1}".format(k, v.encode('utf-8'))
elif type(v) == list:
for e in v:
print " -{0}: {1}".format(k, e.encode('utf-8'))
for heading in ead.headinglist:
print f + "|head|" + heading.encode('utf-8')
for headroot in ead.headrootlist:
print f + "|root|" + headroot.encode('utf-8')
'''
#printComponents(ead.components)
#ead.output()
#ead.fourstore()
#ead.makeSolr()
except Exception, e:
print "Error processing %s: %s" % (f, e)
time = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
print 'EndTime: {0}: {1}'.format(f, time)
count += 1
fuzzmatch = codecs.open(opts.fuzzmatch, 'r', encoding='utf-8')
dbmatches = {}
for line in fuzzmatch:
split = line.split('|')
ratiodict = {'dbid': split[1], 'dblabel': split[2], 'label': split[3]}
if split[0] not in dbmatches:
dbmatches[split[0]] = [ratiodict]
else:
dbmatches[split[0]].append(ratiodict)
for eid, entity in entitySet.iteritems():
entity.makeSolr()
print eid
'''
#This block of code moves to makeSolr() for the entity....
sameArray = sameAs('http://chrpr.com/data/' + eid.encode('utf-8') + '.rdf')
for uri in sameArray:
print " -" + uri.encode('utf-8')
abstracts = dbpField(uri.encode('utf-8'), 'http://dbpedia.org/ontology/abstract')
for abstract in abstracts:
print " Abstract:" + abstract.encode('utf-8')
'''
'''
if eid in dbmatches:
candidates = dbmatches[eid]
toprat = 0
bestmatch = ""
for candidate in candidates:
ratio = fuzzer(candidate['label'], candidate['dblabel'])
if ratio > toprat:
toprat = ratio
bestid = candidate['dbid']
bestmatch = (eid.encode('utf-8') + "|" + candidate['dbid'].encode('utf-8') +
"|" + candidate['dblabel'].encode('utf-8') + "|" +
candidate['label'].encode('utf-8') + "|" +
str(ratio))
#print bestmatch
#print bestid
#load4store("dbpedia", bestid)
#load4store("relations", [eid, 'owl', 'sameas', bestid])
load4store([eid, 'owl', 'sameas', bestid])
'''
#entity.makeGraph()
#entity.fourstore()
#for lookup in entity.metadata['lookup']:
# lookups.write(eid + '|' + lookup + '\n')
# lookups.write('{0}|{1}'.format(eid, lookup.decode('utf-8')))
#print catext.encode('utf-8')
#for k, v, in entity.metadata.iteritems():
#print k + " is a " + type(v)
#if type(v) == "str":
#print "{0}: {1}".format(k, v)
#print "Collections: " + "; ".join(entity.collections).encode('utf-8')
#print "Headings: " + "; ".join(entity.headings).encode('utf-8')
#dbpmatch(entitySet)
if __name__ == '__main__':
op = OptionParser()
op.add_option('--sourcedir', dest='sourcedir', action='store',
help='load from this directory path', default='')
op.add_option('--limit', dest='limit', action='store', type=int,
help='only process this many', default=None)
op.add_option('--fuzzmatch', dest='fuzzmatch', action='store',
help='path to fuzzmatch input', default='fuzzmatch.txt')
op.add_option('--lookups', dest='lookups', action='store',
help='path to lookups output', default='lookups.txt')
opts, args = op.parse_args()
if not os.path.exists(opts.fuzzmatch):
raise Exception("file not found: %s" % opts.fuzzmatch)
scriptstart = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
main(opts)
scriptend = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
print "Total Time:"
print "Start: " + scriptstart
print "End: " + scriptend