-
Notifications
You must be signed in to change notification settings - Fork 2
/
assign_uris_for_entities.py
276 lines (230 loc) · 8.45 KB
/
assign_uris_for_entities.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
#Assigns URIs to the resources
#For entities that are not authors, it is assumed that entities with the same String are in fact the same entity.
import pandas as pd
#dskg-beta-version without IDs
df = pd.read_csv("/DSKG_BETA.csv")
foafPersonID = 10000
foafOrganizationID = 20000
foafAgentID = 30000
vcardKindID = 40000
df["creatorPersonID"] = ""
df["creatorOrganizationID"] = ""
df["creatorAgentID"] = ""
df["publisherOrganizationID"] = ""
df["publisherAgentID"] = ""
df["contributorOrganizationID"] = ""
df["contributorAgentID"] = ""
df["contactPointID"] = ""
#Assigning IDs for entities of the class foaf:Person
i = 0
while i < len(df):
authors_string = df['creatorPersonName'][i]
authors = str(authors_string).split(", ")
for author in authors:
if str(author) != "nan":
df["creatorPersonID"][i] = df["creatorPersonID"][i] + str(foafPersonID) + ", "
foafPersonID += 1
if str(df["creatorPersonID"][i]).endswith(", "):
max_len = len(str(df["creatorPersonID"][i]))
unwanted_cut_off = max_len - 2
df["creatorPersonID"][i] = str(df["creatorPersonID"][i])[0:unwanted_cut_off]
i += 1
#Assign IDs for entities of the class foaf:Organization
foafOrganization_dict = dict()
#creatorOrganization
i = 0
while i < len(df):
creators_string = df['creatorOrganizationName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if creator not in foafOrganization_dict:
if str(creator) != "nan":
foafOrganization_dict[creator] = str(foafOrganizationID)
foafOrganizationID += 1
i += 1
#publisherOrganization
i = 0
while i < len(df):
creators_string = df['publisherOrganizationName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if creator not in foafOrganization_dict:
if str(creator) != "nan":
foafOrganization_dict[creator] = str(foafOrganizationID)
foafOrganizationID += 1
i += 1
#contributorOrganization
i = 0
while i < len(df):
creators_string = df['contributorOrganizationName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if creator not in foafOrganization_dict:
if str(creator) != "nan":
foafOrganization_dict[creator] = str(foafOrganizationID)
foafOrganizationID += 1
i += 1
#Save the IDs in the csv-file
i = 0
while i < len(df):
creators_string = df['creatorOrganizationName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if str(creator) != "nan":
creator_id = foafOrganization_dict.get(creator)
df["creatorOrganizationID"][i] = df["creatorOrganizationID"][i] + str(creator_id) + ", "
ids = df["creatorOrganizationID"][i]
if str(ids).endswith(", "):
max_len = len(str(ids))
unwanted_cut_off = max_len - 2
ids = str(ids)[0:unwanted_cut_off]
df["creatorOrganizationID"][i] = ids
i += 1
i = 0
while i < len(df):
creators_string = df['publisherOrganizationName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if str(creator) != "nan":
creator_id = foafOrganization_dict.get(creator)
df["publisherOrganizationID"][i] = df["publisherOrganizationID"][i] + str(creator_id) + ", "
ids = df["publisherOrganizationID"][i]
if str(ids).endswith(", "):
max_len = len(str(ids))
unwanted_cut_off = max_len - 2
ids = str(ids)[0:unwanted_cut_off]
df["publisherOrganizationID"][i] = ids
i += 1
i = 0
while i < len(df):
creators_string = df['contributorOrganizationName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if str(creator) != "nan":
creator_id = foafOrganization_dict.get(creator)
df["contributorOrganizationID"][i] = df["contributorOrganizationID"][i] + str(creator_id) + ", "
ids = df["contributorOrganizationID"][i]
if str(ids).endswith(", "):
max_len = len(str(ids))
unwanted_cut_off = max_len - 2
ids = str(ids)[0:unwanted_cut_off]
df["contributorOrganizationID"][i] = ids
i += 1
#Assign IDs for entities of the class foaf:Agent
#creatorAgent
foafAgent_dict = dict()
i = 0
while i < len(df):
creators_string = df['creatorAgentName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if creator not in foafAgent_dict:
if str(creator) != "nan":
foafAgent_dict[creator] = str(foafAgentID)
foafAgentID += 1
i += 1
#publisherAgent
i = 0
while i < len(df):
creators_string = df['publisherAgentName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if creator not in foafAgent_dict:
if str(creator) != "nan":
foafAgent_dict[creator] = str(foafAgentID)
foafAgentID += 1
i += 1
#contributorAgent
i = 0
while i < len(df):
creators_string = df['contributorAgentName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if creator not in foafAgent_dict:
if str(creator) != "nan":
foafAgent_dict[creator] = str(foafAgentID)
foafAgentID += 1
i += 1
#Save the IDs in the csv-file
i = 0
while i < len(df):
creators_string = df['creatorAgentName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if str(creator) != "nan":
creator_id = foafAgent_dict.get(creator)
df["creatorAgentID"][i] = df["creatorAgentID"][i] + str(creator_id) + ", "
ids = df["creatorAgentID"][i]
if str(ids).endswith(", "):
max_len = len(str(ids))
unwanted_cut_off = max_len - 2
ids = str(ids)[0:unwanted_cut_off]
df["creatorAgentID"][i] = ids
i += 1
i = 0
while i < len(df):
creators_string = df['publisherAgentName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if str(creator) != "nan":
creator_id = foafAgent_dict.get(creator)
df["publisherAgentID"][i] = df["publisherAgentID"][i] + str(creator_id) + ", "
ids = df["publisherAgentID"][i]
if str(ids).endswith(", "):
max_len = len(str(ids))
unwanted_cut_off = max_len - 2
ids = str(ids)[0:unwanted_cut_off]
df["publisherAgentID"][i] = ids
i += 1
i = 0
while i < len(df):
creators_string = df['contributorAgentName'][i]
creators = str(creators_string).split(", ")
for creator in creators:
if str(creator) != "nan":
creator_id = foafAgent_dict.get(creator)
df["contributorAgentID"][i] = df["contributorAgentID"][i] + str(creator_id) + ", "
ids = df["contributorAgentID"][i]
if str(ids).endswith(", "):
max_len = len(str(ids))
unwanted_cut_off = max_len - 2
ids = str(ids)[0:unwanted_cut_off]
df["contributorAgentID"][i] = ids
i += 1
#Assign IDs for entities of the class vcard:Kind
#contactPoint
new_dict = dict()
i = 0
while i < len(df):
creator = df['contactPointName'][i]
if creator not in new_dict:
if str(creator) != "nan":
new_dict[creator] = str(vcardKindID)
vcardKindID += 1
i += 1
i = 0
while i < len(df):
creator = df['contactPointName'][i]
if str(creator) != "nan":
creator_id = new_dict.get(creator)
df["contactPointID"][i] =str(creator_id)
i += 1
#Corrects the authors' URIs based on the results of the author disambiguation
#Identical authors have identical ID
with open("/author-disambiguation/all_positives.txt", "r") as inp:
for line in inp:
ids = line.split("\t")
id_replace = ids[0]
id_keep = ids[1].strip()
i = 0
while i < len(df):
author_ids = df["creatorPersonID"][i]
new_author_ids = df["creatorPersonID"][i]
author_ids_split = str(author_ids).split(", ")
for aid in author_ids_split:
if str(aid) == str(id_replace):
new_author_ids = new_author_ids.replace(aid, id_keep)
df["creatorPersonID"][i] = new_author_ids
i += 1
#dskg-beta-version with IDs
df.to_csv("/DSKG_BETA_IDs.csv")