forked from deternitydx/SNAC-EAC-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_neo4jdb.py
234 lines (198 loc) · 7.26 KB
/
create_neo4jdb.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
import datetime
import codecs
import os
# Import the bulbs graph ORM
from bulbs.neo4jserver import Graph, ExactIndex
from relationships import *
from bulbs.element import Vertex
# Import XML parser
import xml.etree.ElementTree as ET
print "Starting processing at ", datetime.datetime.now()
# Neo4J Test
g = Graph()
g.add_proxy("agents", Agent)
g.add_proxy("places", Place)
g.add_proxy("documents", Document)
g.add_proxy("associated", AssociatedWith)
g.add_proxy("corresponded", CorrespondedWith)
g.add_proxy("referencedIn", ReferencedIn)
g.add_proxy("location", Location)
g.vertices.occindex = g.factory.get_index(Vertex, ExactIndex, "occupationIndex")
g.vertices.subindex = g.factory.get_index(Vertex, ExactIndex, "subjectIndex")
#print g
#print "Vertices:", g.V
#print "Edges:" , g.E
# XML Test
namespaces = { "snac" : "urn:isbn:1-931666-33-4" ,
"snac2" : "http://socialarchive.iath.virginia.edu/control/term#",
"schema" : "http://schema.org/",
"xlink" : "http://www.w3.org/1999/xlink",
"snac3" : "http://socialarchive.iath.virginia.edu/"}
ET.register_namespace("snac", "urn:isbn:1-931666-33-4")
ET.register_namespace("snac2", "http://socialarchive.iath.virginia.edu/control/term#")
ET.register_namespace("snac3", "http://socialarchive.iath.virginia.edu/")
ET.register_namespace("xlink", "http://www.w3.org/1999/xlink")
# Counter
counter = 0
# For each file, parse and fill out node information
path = "../../latestMerge/merge/"
for filename in os.listdir(path):
counter = counter + 1
tree = ET.parse(os.path.join(path,filename))
root = tree.getroot()
identifier = ""
subjects = []
nationalities = []
alt_names = []
realname = ""
places = []
placesIDs = []
occupations = []
languages = []
associated = []
corresponded = []
sameas = []
entity_type = ""
start = None
end = None
# Handle identifier
node = root.find(".//snac:recordId", namespaces)
identifier = node.text
# Handle birth/death/active dates
node = root.find(".//snac:existDates", namespaces)
if node is not None:
tmp = node.find(".//snac:fromDate", namespaces)
if tmp is not None:
start = tmp.get("standardDate")
tmp = node.find(".//snac:toDate", namespaces)
if tmp is not None:
end = tmp.get("standardDate")
# Handle entity type
node = root.find(".//snac:entityType", namespaces)
if node.text == "person":
entity_type = "edm:Agent"
elif node.text == "corporateBody":
entity_type = "edm:Agent"
else:
entity_type = "edm:Agent"
hidden_type = node.text
# Handle names
first = True;
for node in root.findall(".//snac:nameEntry", namespaces):
if first:
realname = node[0].text
first = False
else:
alt_names.append(node[0].text)
# Handle local descriptions (subjects, nationalities)
for node in root.findall(".//snac:localDescription", namespaces):
for attr in node.attrib.items():
if "AssociatedSubject" in attr[1]:
subjects.append(node[0].text)
if "nationalityOfEntity" in attr[1]:
nationalities.append(node[0].text)
# Handle places
for node in root.findall(".//snac3:placeEntryLikelySame", namespaces):
places.append([node.get("vocabularySource"), node.text, node.get("latitude"), node.get("longitude")])
placesIDs.append(node.get("vocabularySource"))
# Handle occupations
for node in root.findall(".//snac:occupation", namespaces):
occupations.append(node[0].text)
# Handle languages
for node in root.findall(".//snac:languageUsed", namespaces):
languages.append(node[0].get("languageCode"))
# Handle sameAs relationships
for node in root.findall(".//snac:cpfRelation", namespaces):
role = node.get("{http://www.w3.org/1999/xlink}arcrole")
link = node.get("{http://www.w3.org/1999/xlink}href")
if "sameAs" in role:
sameas.append(link)
# Create a node in the database for this object
# Create an Agent
pnode = g.agents.create(
snac_type=hidden_type,
identifier=identifier,
name=realname)
if alt_names:
pnode.altnames=alt_names
if subjects:
pnode.subjects=subjects
for sub in subjects:
g.vertices.subindex.put(pnode.eid, subject=sub)
if occupations:
pnode.occupations=occupations
for occ in occupations:
g.vertices.occindex.put(pnode.eid, occupation=occ)
if languages:
pnode.languages=languages
if start:
pnode.startDate=start
if end:
pnode.endDate=end
if sameas:
pnode.sameAs=sameas
# Handle places (need to create them if they don't exist)
if places:
pnode.places=placesIDs
for place in places:
tonodes = g.places.index.lookup(identifier=place[0])
tonode = None
if tonodes:
tonode = tonodes.next()
else:
tonode = g.places.create(identifier=place[0], name=place[1], latitude=place[2], longitude=place[3])
if tonode:
rel = g.location.create(pnode, tonode)
rel.save()
# Save the node so we can work on the next
pnode.save()
if counter % 1000 == 0:
print "Imported ", counter, " nodes at ", datetime.datetime.now()
# Print that the nodes have been created
print "Nodes have been successfully created at ", datetime.datetime.now()
# Counter
counter = 0
# For each file, parse and fill out edge information
for filename in os.listdir(path):
counter = counter + 1
tree = ET.parse(os.path.join(path,filename))
root = tree.getroot()
associated = []
corresponded = []
# Handle identifier
node = root.find(".//snac:recordId", namespaces)
identifier = node.text
# Handle relationships
for node in root.findall(".//snac:cpfRelation", namespaces):
role = node.get("{http://www.w3.org/1999/xlink}arcrole")
link = node.get("{http://www.w3.org/1999/xlink}href")
if "associatedWith" in role:
associated.append(link)
elif "correspondedWith" in role:
corresponded.append(link)
elif "sameAs" in role:
sameas.append(link)
# Input the relationships into the database
#rel = g.associated.create(gw, tj)
#rel.save()
pnode = None
pnodes = g.agents.index.lookup(identifier=identifier)
pnode = pnodes.next()
if pnode is not None:
for assoc in associated:
tonodes = g.agents.index.lookup(identifier=assoc)
if tonodes:
tonode = tonodes.next()
if tonode:
rel = g.associated.create(pnode, tonode)
rel.save()
for corr in corresponded:
tonodes = g.agents.index.lookup(identifier=corr)
if tonodes:
tonode = tonodes.next()
if tonode:
rel = g.corresponded.create(pnode, tonode)
rel.save()
if counter % 1000 == 0:
print "Imported ", counter, " node-edges at ", datetime.datetime.now()
print "Edges have been successfully created at ", datetime.datetime.now()