-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_odk.py
More file actions
330 lines (271 loc) · 11.2 KB
/
csv_to_odk.py
File metadata and controls
330 lines (271 loc) · 11.2 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sys
import time
import pandas as pd
from datetime import datetime
#query
import rdflib#from rdflib import Graph#, Literal, URIRef, BNode
from SPARQLWrapper import SPARQLWrapper, JSON
import rdflib.plugins.sparql.update
#import owlrl.RDFSClosure
sys.path.insert(1, './lib') #add local library path
import template_maker as tm
tm.hide_labels=True
tsv_prefer=True #replaces commas to tabs, semicolons to commas also
inp_name=sys.argv[1]
print("reading from " + inp_name)
df = pd.read_csv(inp_name, keep_default_na=False)
outname = "template.csv"
if (tsv_prefer==True):
outname="template.tsv"
print(df.head())
#url,title,creator,gitlink,filelink,descr,license,contact,documentation,proj,vers,module,branch,otype,extens
titles=[]
uris=[]
vers=[]
repolinks=[]
filelinks=[]
extens=[]
variants=[]
descriptions=[]
cpoints=[]
creators=[]
doclinks=[]
licenses=[]
acronyms=[]
short_desc=[]
#L,Link inside repository,creator,license,contact,documentation link,related project, version, module, branch, type,extension, description
for x in df['title'][:]:
titles.append(x)
for x in df['Link (URL/PID)'][:]:
uris.append(x)
for x in df['Repository address'][:]:
repolinks.append(x)
for x in df['Link inside repository'][:]:
filelinks.append(x)
for x in df[' version'][:]:
vers.append(x)
for x in df[' type'][:]:
variants.append(x)
for x in df['extension'][:]:
extens.append(x)
for x in df[' description'][:]:
descriptions.append(x)
for x in df['contact'][:]:
cpoints.append(x)
for x in df['creator'][:]:
creators.append(x)
for x in df['documentation link'][:]:
doclinks.append(x)
#if manually added columns are present at human-readable csv file
#for x in df['License'][:]:
# licenses.append(x)
#for x in df['Acronym'][:]:
# acronyms.append(x)
#for x in df['Short description'][:]:
# short_desc.append(x)
#for compatibility, fill with empty if not upper case
for i in range(len(titles)):
if (i >= len(licenses)):
licenses.append("")
acronyms.append("")
short_desc.append("")
#sort by titles
#titles,uris,repolinks,filelinks,vers,variants,extens,descriptions,cpoints,creators,doclinks,licenses,acronyms,short_desc = zip(*sorted(zip(titles,uris,repolinks,filelinks,vers,variants,extens,descriptions,cpoints,creators,doclinks,licenses,acronyms,short_desc)))
titles = list(titles)
uris = list(uris)
repolinks = list(repolinks)
filelinks = list(filelinks)
vers = list(vers)
variants = list(variants)
extens = list(extens)
descriptions = list(descriptions)
cpoints = list(cpoints)
creators = list(creators)
doclinks = list(doclinks)
licenses = list(licenses)
acronyms = list(acronyms)
short_desc = list(short_desc)
odk_lines=[]
odk_lines.append(tm.header1)
odk_lines.append(tm.header2)
odk_lines.append(tm.header3)
odk_lines.append(tm.header4)
def_version="main"
def_variant = "release"
curr_repo=""
curr_title=""
curr_vers=""
id_titles=[] #remember ids of titles
unique_titles=[] #not to return to the same ontology
unique_viris=[] #uniqe vers. iris
skip_title=False
titles_finished=[] #if 0, then in process, 1=finished
unique_uris=[]
unique_vers=[]
unique_vars=[] #for version!
unique_exts=[] #for version + variant!
print(str(len(titles)) + " titles total")
[kg_ontos,kg_repos] = tm.get_mse_kg_ontologies()
print("found ontologies in MSE KG:")
print(kg_ontos)
for i in range(len(titles)):
titles[i] = titles[i].replace(",",";").replace("\n","; ")
creators[i] = creators[i].replace("<","(").replace(">",")").replace("[","(").replace("]",")").replace("{","(").replace("}",")")
cpoints[i] = cpoints[i].replace("<","(").replace(">",")").replace("[","(").replace("]",")").replace("{","(").replace("}",")")
#print("i: "+str(i))
if (titles[i]==''):
continue
#if ((curr_title==titles[i]) | (curr_repo==repolinks[i])): #within the same title = ontology class
############
#manual csv import, tailored for a limited number of columns: license, acronym, short_description
###########
if (repolinks[i]==""):
continue #skip
#print(titles[i])
curr_title = titles[i]
try:
short_desc[i] = short_desc[i].replace(",",";").replace("\n","; ")
except:
pass
if (titles[i] not in unique_titles):
[onto_line,onto_id] = tm.get_ontology_line(curr_title,licenses[i],acronyms[i])
odk_lines.append(onto_line) #add onto line to odk
odk_lines.append(tm.get_ontology_title_namespace_descript_lines(curr_title,titles[i],"",short_desc[i],onto_id)[0])
repolinks[i] = uris[i]
curr_repo = repolinks[i]
if (repolinks[i]!=""):
if (repolinks[i] not in tm.used_repos):
[repo_line,repo_id] = tm.get_repository_line(onto_id,curr_title+" repository", curr_repo,"")
odk_lines.append(repo_line) #currently only main version
tm.used_repos.append(repolinks[i])
tm.used_repos_id.append(repo_id)
#print("new repo for " + curr_title, "repo=" + repo_line)
else:
repo_id = tm.used_repos_id[tm.used_repos.index(repolinks[i])]
tm.get_repository_line(onto_id,curr_title+" repository", curr_repo,repo_id)
#print("reuse repo for " + curr_title)
else:
i = unique_titles.index(titles[i])
id_reuse = id_titles[i]
[onto_line,onto_id] = tm.get_ontology_line(curr_title,licenses[i],acronyms[i],id_reuse)
odk_lines.append(onto_line) #add onto line to odk
print("reuse title for " + curr_title)
##############
#main: harvester case
################
else:
#print("onto+repo: " + curr_title)
line_vers = uris[i]
if (line_vers==curr_vers):
i+=0
else: #entering new repository
#if (titles[i] not in unique_titles):
if (line_vers not in unique_viris):
curr_vers = line_vers
unique_viris.append(line_vers)
skip_title=False
else:
skip_title=True
#print("skipping "+titles[i])
print("skipping "+line_vers)
continue
if (titles[i] not in unique_titles):
unique_titles.append(titles[i])
skip_title=False
else:
skip_title=True
#print("skipping "+titles[i])
print("skipping title "+titles[i])
continue
onto_id=""
vers_id=""
variant_id=""
serializ_id=""
repo_id=""
downl_id=""
ext_id=""
contact_id=""
creatorS_id=""
contact_role_id=""
curr_repo = repolinks[i]
curr_title = titles[i]
[onto_line,onto_id] = tm.get_ontology_line(curr_title,licenses[i],acronyms[i])
odk_lines.append(onto_line) #add onto line to odk
id_titles.append(onto_id)
if (repolinks[i] not in tm.used_repos):
[repo_line,repo_id] = tm.get_repository_line(onto_id,curr_title+" repository", curr_repo,"")
odk_lines.append(repo_line) #currently only main version
tm.used_repos.append(repolinks[i])
tm.used_repos_id.append(repo_id)
else:
repo_id = tm.used_repos_id[tm.used_repos.index(repolinks[i])]
tm.get_repository_line(onto_id,curr_title+" repository", curr_repo,repo_id)
unique_uris=[]
unique_vers=[]
unique_vars=[]
unique_exts=[]
odk_lines.append(tm.get_ontology_title_namespace_descript_lines(curr_title,titles[i],uris[i],descriptions[i],onto_id)[0])
#doc
if ((doclinks[i]!="") & (doclinks[i]!="no info")& (doclinks[i]!="no info ")):
#print("doclinks="+doclinks[i])
odk_lines.append(tm.get_documentation_line(curr_title,onto_id,doclinks[i])[0])
#versions
if ((variants[i]!="base") & (variants[i]!="reasoned") & (variants[i]!="inferred")):
variants[i]=""
var_title= variants[i]
vers_title = vers[i] #def_version
# [var_line,variant_id] = tm.get_ontology_variant_line(curr_title+" v. "+def_version + " " +variants[i],vers_id,onto_id,variants[i])
# odk_lines.append(var_line) #add variant
#extensions
if ((vers_title+"_"+var_title+"_"+extens[i]) not in unique_exts):
unique_exts.append(vers_title+"_"+var_title+"_"+extens[i])
if ((vers_title+"_"+var_title) not in unique_vars):
unique_vars.append((vers_title+"_"+var_title))
else:
var_title=""
if (vers_title not in unique_vers):
unique_vers.append(vers_title)
else:
vers_title=""
odk_lines.append(tm.get_ontology_version_variant_extension_line(titles[i],repo_id,onto_id,vers_title,var_title, filelinks[i],extens[i]))
#contact point
if ((cpoints[i]!="") & (cpoints[i]!="no info") & ( (cpoints[i]!="no info "))):
if (contact_id==""):
#print("cp="+cpoints[i])
[contact_line,contact_id,contact_role_id]=tm.get_contact_role_process_line(curr_title,onto_id,cpoints[i])
odk_lines.append(contact_line)
if (contact_id!=""): #we have info about this contact already
odk_lines.append(tm.get_contact_details_lines(curr_title,contact_id,contact_role_id,cpoints[i])[0])
#creator
if (creators[i]!=""):
#multiline handle
if (creatorS_id==""):
creatorS_id = creators[i]
creation_proc_id=""
for creator in (creators[i].split(";")):
if (len(creator.replace(" ",""))<2):
continue
[creator_line,creator_id,creation_proc_id]=tm.get_creator_role_process_line(curr_title,onto_id,creator,creation_proc_id)
odk_lines.append(creator_line)
if (creator_id!=""): #we have info about this creator already
odk_lines.append(tm.get_creator_details_lines(curr_title,creator_id,creator)[0])
f = open(outname,'a', encoding="utf-8")
ind=0
for line in odk_lines:
#print(line)
# print("\n")
if (len(line.split(","))<tm.nprops/2): #skip empty lines
continue
line+="\n"
line=line.replace("\n\n","\n")
if (ind>=4):
newline = tm.append_annotations(line)
else:
newline = line
if (tsv_prefer==True):
newline = newline.replace(",","\t").replace(";",",")
f.write(newline)
else:
f.write(newline)
ind+=1
f.close()