CSV_to_RDF_onomastica_ospedale.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #Parser to convert the Datini onomastics CSV file into TTL format
  2. # Utilities to read/write csv files
  3. import csv
  4. # Utilities to handle character encodings
  5. import unicodedata
  6. # Ordered Dicts
  7. from collections import OrderedDict
  8. import json
  9. import re
  10. # OPZIONAL IMPORTS
  11. # For timestamping/simple speed tests
  12. from datetime import datetime
  13. # Random number generator
  14. from random import *
  15. # System & command line utilities
  16. import sys
  17. # Json for the dictionary
  18. import json
  19. import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/CSV/ospedale/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/RDF/ospedale/'
  21. # Custom class to store URIs + related infos for the ontologies/repositories
  22. class RDFcoords:
  23. def __init__(self, uri, prefix, code = None):
  24. self.uri = uri
  25. self.prefix = prefix
  26. self.code = code
  27. # Repositories
  28. aspoCoords = RDFcoords('<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/>', 'aspo:')
  29. foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
  30. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  31. schemaCoords = RDFcoords('<http://schema.org/>', 'schema:')
  32. personCoords = RDFcoords('<http://www.w3.org/ns/person#>', 'person:')
  33. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  34. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  35. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  36. # Basic functions for triples / shortened triples in TTL format
  37. def triple(subject, predicate, object1):
  38. line = subject + ' ' + predicate + ' ' + object1
  39. return line
  40. def doublet(predicate, object1):
  41. line = ' ' + predicate + ' ' + object1
  42. return line
  43. def singlet(object1):
  44. line = ' ' + object1
  45. return line
  46. # Line endings in TTL format
  47. continueLine1 = ' ;\n'
  48. continueLine2 = ' ,\n'
  49. closeLine = ' .\n'
  50. def writeTTLHeader(output):
  51. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  52. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  53. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  54. output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)
  55. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  56. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  57. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  58. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  59. output.write('\n')
  60. filePrefix = 'OSPEDALE - onomastica '
  61. fileType = '- persone singole'
  62. max_entries = 10000000000000
  63. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  64. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  65. reader = csv.DictReader(csv_file)
  66. writeTTLHeader(output)
  67. first = True
  68. ii = 0
  69. for row in reader:
  70. # The index ii is used to process a limited number of entries for testing purposes
  71. ii = ii + 1
  72. if row['entityType'] == 'person':
  73. id_aspo = row['recordId']
  74. #placeHolders
  75. aspoPlaceHolder = aspoCoords.prefix + id_aspo
  76. line = triple(aspoPlaceHolder,
  77. nsCoords.prefix + 'type',
  78. cidocCoords.prefix + 'E21_Person') + closeLine
  79. output.write(line)
  80. line = triple(aspoPlaceHolder,
  81. nsCoords.prefix + 'type',
  82. personCoords.prefix + 'Person') + closeLine
  83. output.write(line)
  84. line = triple(aspoPlaceHolder,
  85. nsCoords.prefix + 'type',
  86. foafCoords.prefix + 'person') + closeLine
  87. output.write(line)
  88. line = triple(aspoPlaceHolder,
  89. cidocCoords.prefix + 'P1_is_identified_by',
  90. aspoPlaceHolder + "_E42") + closeLine
  91. output.write(line)
  92. line = triple(aspoPlaceHolder + "_E42",
  93. nsCoords.prefix + 'type',
  94. cidocCoords.prefix + 'E42_Identifier') + closeLine
  95. output.write(line)
  96. line = triple(aspoPlaceHolder + "_E42",
  97. rdfsCoords.prefix + 'label',
  98. '\"' + id_aspo + '\"') + closeLine
  99. output.write(line)
  100. line = triple(aspoPlaceHolder,
  101. foafCoords.prefix + 'name',
  102. '\"' + row['nameEntry@normal'] + '\"') + closeLine
  103. output.write(line)
  104. line = triple(aspoPlaceHolder,
  105. rdfsCoords.prefix + 'label',
  106. '\"' + row['nameEntry@normal'] + '\"') + closeLine
  107. output.write(line)
  108. if row['nome proprio'] != '':
  109. #Remove all white-space characters:
  110. txt = row['nome proprio']
  111. x = re.sub(" \n", "", txt)
  112. y = re.sub("\s\s", "", x)
  113. name = re.sub("\n", "", y)
  114. line = triple(aspoPlaceHolder,
  115. foafCoords.prefix + 'givenName',
  116. '\"' + name + '\"') + closeLine
  117. output.write(line)
  118. if row['nome di famiglia'] != '':
  119. #Remove all white-space characters:
  120. txt = row['nome di famiglia']
  121. x = re.sub("\n", " ", txt)
  122. y = re.sub("\s\s", "", x)
  123. line = triple(aspoPlaceHolder,
  124. foafCoords.prefix + 'familyName',
  125. '\"' + y + '\"') + closeLine
  126. output.write(line)
  127. if row['Alias'] != '' and row['Alias'] != ' ':
  128. #Remove all white-space characters:
  129. txt = row['Alias']
  130. x = re.sub("\n", " ", txt)
  131. y = re.sub("\s\s", "", x)
  132. line = triple(aspoPlaceHolder,
  133. schemaCoords.prefix + 'alternateName',
  134. '\"' + y + '\"') + closeLine
  135. output.write(line)
  136. if row['genere'] != '':
  137. #Remove all white-space characters:
  138. txt = row['genere']
  139. x = re.sub("\n", " ", txt)
  140. y = re.sub("\s\s", "", x)
  141. line = triple(aspoPlaceHolder,
  142. foafCoords.prefix + 'gender',
  143. '\"' + y + '\"') + closeLine
  144. output.write(line)
  145. if row['patronimico/matronimico'] != '':
  146. #Remove all white-space characters:
  147. txt = row['patronimico/matronimico']
  148. x = re.sub("\n", " ", txt)
  149. y = re.sub("\s\s", "", x)
  150. line = triple(aspoPlaceHolder,
  151. personCoords.prefix + 'patronymicName',
  152. '\"' + y + '\"') + closeLine
  153. output.write(line)
  154. if row['occupation'] != '' and row['occupation'] != ' ' :
  155. occupazioni = []
  156. pipe = "|"
  157. if pipe in row['occupation']:
  158. occupazioni = row['occupation'].split('|')
  159. for occupazione in occupazioni:
  160. #Remove all white-space characters:
  161. txt = occupazione
  162. x = re.sub("\n", " ", txt)
  163. y = re.sub("\s\s", "", x)
  164. occ = re.sub(r'[^A-Za-z]','', y)
  165. occupationPlaceHolder = '<http://www.archiviodistato.prato.it/' + occ.replace(" ","_") + '>'
  166. line = triple(aspoPlaceHolder,
  167. schemaCoords.prefix + 'hasOccupation',
  168. occupationPlaceHolder) + closeLine
  169. output.write(line)
  170. line = triple(occupationPlaceHolder,
  171. nsCoords.prefix + 'type',
  172. schemaCoords.prefix + 'Occupation') + closeLine
  173. output.write(line)
  174. line = triple(occupationPlaceHolder,
  175. rdfsCoords.prefix + 'label',
  176. '\"' + y + '\"') + closeLine
  177. output.write(line)
  178. else:
  179. #Remove all white-space characters:
  180. txt = row['occupation']
  181. x = re.sub("\n", " ", txt)
  182. y = re.sub("\s\s", "", x)
  183. occ = re.sub(r'[^A-Za-z]','', y)
  184. occupationPlaceHolder = '<http://www.archiviodistato.prato.it/' + occ.replace(" ","_") + '>'
  185. line = triple(aspoPlaceHolder,
  186. schemaCoords.prefix + 'hasOccupation',
  187. occupationPlaceHolder) + closeLine
  188. output.write(line)
  189. line = triple(occupationPlaceHolder,
  190. nsCoords.prefix + 'type',
  191. schemaCoords.prefix + 'Occupation') + closeLine
  192. output.write(line)
  193. line = triple(occupationPlaceHolder,
  194. rdfsCoords.prefix + 'label',
  195. '\"' + y + '\"') + closeLine
  196. output.write(line)
  197. if (row['avo 1'] != ''):
  198. id = row['avo 1']
  199. E13placeHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 1'].replace(' ', '_') + '_AVO1_' + row['recordId'] + ">"
  200. line = triple(E13placeHolder,
  201. nsCoords.prefix + 'type',
  202. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  203. output.write(line)
  204. line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
  205. output.write(line)
  206. line = triple(E13placeHolder,
  207. rdfsCoords.prefix + 'label',
  208. '\"Relazione: ' + row['avo 1'] + ' avo di secondo grado di ' + row['recordId'] + '\"') + closeLine
  209. output.write(line)
  210. if re.match(r'IT-ASPO', id):
  211. relazioneid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 1']+ ">"
  212. line = triple(relazioneid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  213. output.write(line)
  214. else:
  215. relazionenoid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 1'].replace(' ', '_').lower()+ ">"
  216. line = triple(relazionenoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder ) + closeLine
  217. output.write(line)
  218. line = triple(relazionenoid,
  219. rdfsCoords.prefix + 'label',
  220. '\"' + row['avo 1'] + '\"') + closeLine
  221. output.write(line)
  222. line = triple(relazionenoid,
  223. nsCoords.prefix + 'type',
  224. cidocCoords.prefix + 'E21_Person') + closeLine
  225. output.write(line)
  226. line = triple(relazionenoid,
  227. nsCoords.prefix + 'type',
  228. personCoords.prefix + 'Person') + closeLine
  229. output.write(line)
  230. line = triple(relazionenoid,
  231. nsCoords.prefix + 'type',
  232. foafCoords.prefix + 'person') + closeLine
  233. output.write(line)
  234. E55placeHolder = '<http://www.archiviodistato.prato.it/avo_secondo_grado>'
  235. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  236. output.write(line)
  237. line = triple(E55placeHolder,
  238. rdfsCoords.prefix + 'label',
  239. '\"Avo di secondo grado\"') + closeLine
  240. output.write(line)
  241. if (row['avo 2'] != ''):
  242. id = row['avo 2']
  243. E13placeHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 2'].replace(' ', '_') + '_AVO2_' + row['recordId'] + ">"
  244. line = triple(E13placeHolder,
  245. nsCoords.prefix + 'type',
  246. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  247. output.write(line)
  248. line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
  249. output.write(line)
  250. line = triple(E13placeHolder,
  251. rdfsCoords.prefix + 'label',
  252. '\"Relazione: ' + row['avo 2'] + ' avo di terzo grado di ' + row['recordId'] + '\"') + closeLine
  253. output.write(line)
  254. if re.match(r'IT-ASPO', id):
  255. relazioneid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 2']+ ">"
  256. line = triple(relazioneid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  257. output.write(line)
  258. else:
  259. relazionenoid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 2'].replace(' ', '_').lower()+ ">"
  260. line = triple(relazionenoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder ) + closeLine
  261. output.write(line)
  262. line = triple(relazionenoid,
  263. rdfsCoords.prefix + 'label',
  264. '\"' + row['avo 2'] + '\"') + closeLine
  265. output.write(line)
  266. line = triple(relazionenoid,
  267. nsCoords.prefix + 'type',
  268. cidocCoords.prefix + 'E21_Person') + closeLine
  269. output.write(line)
  270. line = triple(relazionenoid,
  271. nsCoords.prefix + 'type',
  272. personCoords.prefix + 'Person') + closeLine
  273. output.write(line)
  274. line = triple(relazionenoid,
  275. nsCoords.prefix + 'type',
  276. foafCoords.prefix + 'person') + closeLine
  277. output.write(line)
  278. E55placeHolder = '<http://www.archiviodistato.prato.it/avo_terzo_grado>'
  279. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  280. output.write(line)
  281. line = triple(E55placeHolder,
  282. rdfsCoords.prefix + 'label',
  283. '\"Avo di terzo grado\"') + closeLine
  284. output.write(line)
  285. if row['Qualifica'] != '':
  286. qualifiche = []
  287. pipe = "|"
  288. if pipe in row['Qualifica']:
  289. qualifiche = row['Qualifica'].split('|')
  290. for qualifica in qualifiche:
  291. #Remove all white-space characters:
  292. txt = qualifica
  293. x = re.sub("\n", " ", txt)
  294. y = re.sub("\s\s", " ", x)
  295. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + str(y) + '\"') + closeLine
  296. output.write(line)
  297. else:
  298. #Remove all white-space characters:
  299. txt = row['Qualifica']
  300. x = re.sub("\n", " ", txt)
  301. y = re.sub("\s\s", " ", x)
  302. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + y + '\"') + closeLine
  303. output.write(line)
  304. if row['biogHist p'] != '':
  305. #Remove all white-space characters:
  306. txt = row['biogHist p']
  307. x = re.sub("\n", " ", txt)
  308. y = re.sub("\s\s", " ", x)
  309. note = re.sub("\"", "", x)
  310. line = triple(aspoPlaceHolder,
  311. cidocCoords.prefix + 'P3_has_note',
  312. '\"' + note + '\"') + closeLine
  313. output.write(line)
  314. if row['Variante'] != '':
  315. varianti = []
  316. pipe = "|"
  317. if pipe in row['Variante']:
  318. varianti = row['Variante'].split('|')
  319. for variante in varianti:
  320. line = triple(aspoPlaceHolder,
  321. owlCoords.prefix + 'sameAs',
  322. aspoCoords.prefix + str(variante)) + closeLine
  323. output.write(line)
  324. else:
  325. line = triple(aspoPlaceHolder,
  326. owlCoords.prefix + 'sameAs',
  327. aspoCoords.prefix + row['Variante']) + closeLine
  328. output.write(line)
  329. if row['provenienza'] != '':
  330. e53placeHolder = "<http://www.archiviodistato.prato.it/" + row['provenienza'].replace('da', '').replace(' ', '') + ">"
  331. line = triple(aspoPlaceHolder,
  332. cidocCoords.prefix + 'P74_has_current_or_former_residence',
  333. e53placeHolder) + closeLine
  334. output.write(line)
  335. line = triple(e53placeHolder,
  336. nsCoords.prefix + 'type',
  337. cidocCoords.prefix + 'E53_Place') + closeLine
  338. output.write(line)
  339. line = triple(e53placeHolder,
  340. rdfsCoords.prefix + 'label',
  341. '\"' + row['provenienza'] + '\"') + closeLine
  342. output.write(line)
  343. line = triple(e53placeHolder,
  344. cidocCoords.prefix + 'P2_has_type',
  345. '\"Provenienza\"') + closeLine
  346. output.write(line)
  347. if (row['recordID relazione'] != ''):
  348. id = row['recordID relazione']
  349. E13placeHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['recordID relazione'].replace(' ', '_') + '_R_' + row['recordId'] + ">"
  350. line = triple(E13placeHolder,
  351. nsCoords.prefix + 'type',
  352. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  353. output.write(line)
  354. line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
  355. output.write(line)
  356. line = triple(E13placeHolder,
  357. rdfsCoords.prefix + 'label',
  358. '\"Relazione: ' + row['recordID relazione'] + row['nome relazione'] + ' di ' + row['recordId'] + '\"') + closeLine
  359. output.write(line)
  360. if re.match(r'IT-ASPO', id):
  361. relazioneid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['recordID relazione']+ ">"
  362. line = triple(relazioneid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  363. output.write(line)
  364. else:
  365. relazionenoid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['recordID relazione'].replace(' ', '_').lower()+ ">"
  366. line = triple(relazionenoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  367. output.write(line)
  368. line = triple(relazionenoid,
  369. rdfsCoords.prefix + 'label',
  370. '\"' + row['recordID relazione'] + '\"') + closeLine
  371. output.write(line)
  372. line = triple(relazionenoid,
  373. nsCoords.prefix + 'type',
  374. cidocCoords.prefix + 'E21_Person') + closeLine
  375. output.write(line)
  376. line = triple(relazionenoid,
  377. nsCoords.prefix + 'type',
  378. personCoords.prefix + 'Person') + closeLine
  379. output.write(line)
  380. line = triple(relazionenoid,
  381. nsCoords.prefix + 'type',
  382. foafCoords.prefix + 'person') + closeLine
  383. output.write(line)
  384. if (row['nome relazione'] != ''):
  385. relazioni = []
  386. pipe = "|"
  387. if pipe in row['nome relazione']:
  388. relazioni = row['nome relazione'].split('|')
  389. for relazione in relazioni:
  390. #Remove all white-space characters:
  391. txt = relazione
  392. x = re.sub("\n", " ", txt)
  393. y = re.sub("\s\s", "", x)
  394. rel = re.sub(r'[^A-Za-z]','', y)
  395. cleanlabel = rel.rstrip().lstrip()
  396. E55placeHolder = '<http://www.archiviodistato.prato.it/relation_' + cleanlabel.replace(" ","") + '>'
  397. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  398. output.write(line)
  399. line = triple(E55placeHolder,
  400. rdfsCoords.prefix + 'label',
  401. '\"' + cleanlabel + '\"') + closeLine
  402. output.write(line)
  403. else:
  404. E55placeHolder = '<http://www.archiviodistato.prato.it/relation_' + cleanlabel.replace(' ', '') + '>'
  405. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  406. output.write(line)
  407. cleanlabel = row['nome relazione'].rstrip().lstrip()
  408. line = triple(E55placeHolder,
  409. rdfsCoords.prefix + 'label',
  410. '\"' + cleanlabel + '\"') + closeLine
  411. output.write(line)
  412. output.write('\n')
  413. #
  414. #
  415. # Limit number of entries processed (if desired)
  416. if (ii > max_entries):
  417. break