This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseattrs.py
182 lines (164 loc) · 5.38 KB
/
parseattrs.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
import couchdb
import csv
import config
# Server setup block
couchserver = couchdb.Server("http://%s:%s@%s/" % (config.user, config.password, config.server))
# Parse CSV Block
objects = {}
links = {}
# Read in object IDS
with open('Objects.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
cols = row
line_count += 1
else:
objects[row[0]] = {"_id":row[0]}
line_count += 1
# Read in link IDS, sources, targets
with open('Links.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
cols = row
line_count += 1
else:
links[row[0]] = {"_id": row[0], "o1":row[1], "o2":row[2]}
line_count += 1
# Read in attributes and and apply them to appropriate objects.
with open('Attributes.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
cols = row
line_count += 1
else:
name = row[0],
t = row[1]
val = row[2]
i = row[3]
if isinstance(name, tuple):
name = name[0]
if t == "O":
obj = objects.get(i)
if obj is None:
break
obj[name] = val
objects[i] = obj
elif t == "L":
link = links.get(i)
if link is None:
break
link[name] = val
links[i] = link
line_count += 1
# Flatten links into objects, bidirectionally
for link_id, link in links.items():
link_type = link["link-type"]
o1 = link["o1"]
o2 = link["o2"]
# Based on link type, add _id to appropriate collection on both source and target objects.
if link_type == "editor-of":
editor = objects[o1]
if editor.get("editor-of") is None:
editor["editor-of"] = [o2]
else:
editor["editor-of"].append(o2)
objects[o1] = editor
edited = objects[o2]
if edited.get("edited-by") is None:
edited["edited-by"] = [o1]
else:
edited["edited-by"].append(o1)
objects[o2] = edited
elif link_type == "author-of":
author = objects[o1]
if author.get("author-of") is None:
author["author-of"] = [o2]
else:
author["author-of"].append(o2)
objects[o1] = author
authored = objects[o2]
if authored.get("authored-by") is None:
authored["authored-by"] = [o1]
else:
authored["authored-by"].append(o1)
objects[o2] = authored
elif link_type == "in-journal":
pub = objects[o1]
if pub.get("in-journal") is None:
pub["in-journal"] = [o2]
else:
pub["in-journal"].append(o2)
objects[o1] = pub
journal = objects[o2]
if journal.get("journal-contents") is None:
journal["journal-contents"] = [o1]
else:
journal["journal-contents"].append(o1)
objects[o2] = journal
elif link_type == "cites":
cites = objects[o1]
if cites.get("cites") is None:
cites["cites"] = [o2]
else:
cites["cites"].append(o2)
objects[o1] = cites
cited = objects[o2]
if cited.get("cited-by") is None:
cited["cited-by"] = [o1]
else:
cited["cited-by"].append(o1)
objects[o2] = cited
elif link_type == "in-proceedings":
pub = objects[o1]
if pub.get("in-proceedings") is None:
pub["in-proceedings"] = [o2]
else:
pub["in-proceedings"].append(o2)
objects[o1] = pub
proceedings = objects[o2]
if proceedings.get("proceedings-contents") is None:
proceedings["proceedings-contents"] = [o1]
else:
proceedings["proceedings-contents"].append(o1)
objects[o2] = proceedings
elif link_type == "in-collection":
pub = objects[o1]
if pub.get("in-collection") is None:
pub["in-collection"] = [o2]
else:
pub["in-collection"].append(o2)
objects[o1] = pub
collection = objects[o2]
if collection.get("collection-contents") is None:
collection["collection-contents"] = [o1]
else:
collection["collection-contents"].append(o1)
objects[o2] = collection
else:
print("Unknown link type, skipping")
break
# Break object collection into two lists of documents.
persons = [v for k,v in objects.items() if v["object-type"] == "person"]
publications = [v for k,v in objects.items() if v["object-type"] != "person"]
# Open/Create databases
dbname = "persons"
if dbname in couchserver:
persondb = couchserver[dbname]
else:
persondb = couchserver.create(dbname)
dbname = "publications"
if dbname in couchserver:
pubdb = couchserver[dbname]
else:
pubdb = couchserver.create(dbname)
# Upload documents in batches of 1000
for pp in [persons[i:i + 1000] for i in range(0, len(persons), 1000)]:
persondb.update(pp)
for pb in [publications[i:i+1000] for i in range(0, len(publications), 1000)]:
pubdb.update(pb)