CSV_to_RDF_onomastica_ospedale_person.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 = 'CSV/'
  20. export_dir = 'RDF/'
  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['avo 1'] != ''):
  155. id = row['avo 1']
  156. E13placeHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 1'].replace(' ', '_') + '_AVO1_' + row['recordId'] + ">"
  157. line = triple(E13placeHolder,
  158. nsCoords.prefix + 'type',
  159. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  160. output.write(line)
  161. line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
  162. output.write(line)
  163. line = triple(E13placeHolder,
  164. rdfsCoords.prefix + 'label',
  165. '\"Relazione: ' + row['avo 1'] + ' avo di secondo grado di ' + row['recordId'] + '\"') + closeLine
  166. output.write(line)
  167. if re.match(r'IT-ASPO', id):
  168. relazioneid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 1']+ ">"
  169. line = triple(relazioneid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  170. output.write(line)
  171. else:
  172. relazionenoid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 1'].replace(' ', '_').lower()+ ">"
  173. line = triple(relazionenoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder ) + closeLine
  174. output.write(line)
  175. line = triple(relazionenoid,
  176. rdfsCoords.prefix + 'label',
  177. '\"' + row['avo 1'] + '\"') + closeLine
  178. output.write(line)
  179. line = triple(relazionenoid,
  180. nsCoords.prefix + 'type',
  181. cidocCoords.prefix + 'E21_Person') + closeLine
  182. output.write(line)
  183. line = triple(relazionenoid,
  184. nsCoords.prefix + 'type',
  185. personCoords.prefix + 'Person') + closeLine
  186. output.write(line)
  187. line = triple(relazionenoid,
  188. nsCoords.prefix + 'type',
  189. foafCoords.prefix + 'person') + closeLine
  190. output.write(line)
  191. E55placeHolder = '<http://www.archiviodistato.prato.it/avo_secondo_grado>'
  192. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  193. output.write(line)
  194. line = triple(E55placeHolder,
  195. rdfsCoords.prefix + 'label',
  196. '\"Avo di secondo grado\"') + closeLine
  197. output.write(line)
  198. if (row['avo 2'] != ''):
  199. id = row['avo 2']
  200. E13placeHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 2'].replace(' ', '_') + '_AVO2_' + row['recordId'] + ">"
  201. line = triple(E13placeHolder,
  202. nsCoords.prefix + 'type',
  203. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  204. output.write(line)
  205. line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
  206. output.write(line)
  207. line = triple(E13placeHolder,
  208. rdfsCoords.prefix + 'label',
  209. '\"Relazione: ' + row['avo 2'] + ' avo di terzo grado di ' + row['recordId'] + '\"') + closeLine
  210. output.write(line)
  211. if re.match(r'IT-ASPO', id):
  212. relazioneid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 2']+ ">"
  213. line = triple(relazioneid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  214. output.write(line)
  215. else:
  216. relazionenoid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['avo 2'].replace(' ', '_').lower()+ ">"
  217. line = triple(relazionenoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder ) + closeLine
  218. output.write(line)
  219. line = triple(relazionenoid,
  220. rdfsCoords.prefix + 'label',
  221. '\"' + row['avo 2'] + '\"') + closeLine
  222. output.write(line)
  223. line = triple(relazionenoid,
  224. nsCoords.prefix + 'type',
  225. cidocCoords.prefix + 'E21_Person') + closeLine
  226. output.write(line)
  227. line = triple(relazionenoid,
  228. nsCoords.prefix + 'type',
  229. personCoords.prefix + 'Person') + closeLine
  230. output.write(line)
  231. line = triple(relazionenoid,
  232. nsCoords.prefix + 'type',
  233. foafCoords.prefix + 'person') + closeLine
  234. output.write(line)
  235. E55placeHolder = '<http://www.archiviodistato.prato.it/avo_terzo_grado>'
  236. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  237. output.write(line)
  238. line = triple(E55placeHolder,
  239. rdfsCoords.prefix + 'label',
  240. '\"Avo di terzo grado\"') + closeLine
  241. output.write(line)
  242. if row['Qualifica'] != '':
  243. qualifiche = []
  244. pipe = "|"
  245. if pipe in row['Qualifica']:
  246. qualifiche = row['Qualifica'].split('|')
  247. for qualifica in qualifiche:
  248. #Remove all white-space characters:
  249. txt = qualifica
  250. x = re.sub("\n", " ", txt)
  251. y = re.sub("\s\s", " ", x)
  252. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + str(y) + '\"') + closeLine
  253. output.write(line)
  254. else:
  255. #Remove all white-space characters:
  256. txt = row['Qualifica']
  257. x = re.sub("\n", " ", txt)
  258. y = re.sub("\s\s", " ", x)
  259. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + y + '\"') + closeLine
  260. output.write(line)
  261. if row['biogHist p'] != '':
  262. #Remove all white-space characters:
  263. txt = row['biogHist p']
  264. x = re.sub("\n", " ", txt)
  265. y = re.sub("\s\s", " ", x)
  266. note = re.sub("\"", "", x)
  267. line = triple(aspoPlaceHolder,
  268. cidocCoords.prefix + 'P3_has_note',
  269. '\"' + note + '\"') + closeLine
  270. output.write(line)
  271. if row['Variante'] != '':
  272. varianti = []
  273. pipe = "|"
  274. if pipe in row['Variante']:
  275. varianti = row['Variante'].split('|')
  276. for variante in varianti:
  277. line = triple(aspoPlaceHolder,
  278. owlCoords.prefix + 'sameAs',
  279. aspoCoords.prefix + str(variante)) + closeLine
  280. output.write(line)
  281. else:
  282. line = triple(aspoPlaceHolder,
  283. owlCoords.prefix + 'sameAs',
  284. aspoCoords.prefix + row['Variante']) + closeLine
  285. output.write(line)
  286. if row['provenienza'] != '':
  287. e53placeHolder = "<http://www.archiviodistato.prato.it/" + row['provenienza'].replace('da', '').replace(' ', '') + ">"
  288. line = triple(aspoPlaceHolder,
  289. cidocCoords.prefix + 'P74_has_current_or_former_residence',
  290. e53placeHolder) + closeLine
  291. output.write(line)
  292. line = triple(e53placeHolder,
  293. nsCoords.prefix + 'type',
  294. cidocCoords.prefix + 'E53_Place') + closeLine
  295. output.write(line)
  296. line = triple(e53placeHolder,
  297. rdfsCoords.prefix + 'label',
  298. '\"' + row['provenienza'] + '\"') + closeLine
  299. output.write(line)
  300. line = triple(e53placeHolder,
  301. cidocCoords.prefix + 'P2_has_type',
  302. '\"Provenienza\"') + closeLine
  303. output.write(line)
  304. if (row['recordID relazione'] != ''):
  305. id = row['recordID relazione']
  306. E13placeHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['recordID relazione'].replace(' ', '_') + '_R_' + row['recordId'] + ">"
  307. line = triple(E13placeHolder,
  308. nsCoords.prefix + 'type',
  309. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  310. output.write(line)
  311. line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
  312. output.write(line)
  313. line = triple(E13placeHolder,
  314. rdfsCoords.prefix + 'label',
  315. '\"Relazione: ' + row['recordID relazione'] + row['nome relazione'] + ' di ' + row['recordId'] + '\"') + closeLine
  316. output.write(line)
  317. if re.match(r'IT-ASPO', id):
  318. relazioneid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['recordID relazione']+ ">"
  319. line = triple(relazioneid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  320. output.write(line)
  321. else:
  322. relazionenoid = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + row['recordID relazione'].replace(' ', '_').lower()+ ">"
  323. line = triple(relazionenoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
  324. output.write(line)
  325. line = triple(relazionenoid,
  326. rdfsCoords.prefix + 'label',
  327. '\"' + row['recordID relazione'] + '\"') + closeLine
  328. output.write(line)
  329. line = triple(relazionenoid,
  330. nsCoords.prefix + 'type',
  331. cidocCoords.prefix + 'E21_Person') + closeLine
  332. output.write(line)
  333. line = triple(relazionenoid,
  334. nsCoords.prefix + 'type',
  335. personCoords.prefix + 'Person') + closeLine
  336. output.write(line)
  337. line = triple(relazionenoid,
  338. nsCoords.prefix + 'type',
  339. foafCoords.prefix + 'person') + closeLine
  340. output.write(line)
  341. if (row['nome relazione'] != ''):
  342. relazioni = []
  343. pipe = "|"
  344. if pipe in row['nome relazione']:
  345. relazioni = row['nome relazione'].split('|')
  346. for relazione in relazioni:
  347. #Remove all white-space characters:
  348. txt = relazione
  349. x = re.sub("\n", " ", txt)
  350. y = re.sub("\s\s", "", x)
  351. rel = re.sub(r'[^A-Za-z]','', y)
  352. cleanlabel = rel.rstrip().lstrip()
  353. E55placeHolder = '<http://www.archiviodistato.prato.it/relation_' + cleanlabel.replace(" ","") + '>'
  354. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  355. output.write(line)
  356. line = triple(E55placeHolder,
  357. rdfsCoords.prefix + 'label',
  358. '\"' + cleanlabel + '\"') + closeLine
  359. output.write(line)
  360. else:
  361. E55placeHolder = '<http://www.archiviodistato.prato.it/relation_' + cleanlabel.replace(' ', '') + '>'
  362. line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
  363. output.write(line)
  364. cleanlabel = row['nome relazione'].rstrip().lstrip()
  365. line = triple(E55placeHolder,
  366. rdfsCoords.prefix + 'label',
  367. '\"' + cleanlabel + '\"') + closeLine
  368. output.write(line)
  369. output.write('\n')
  370. #
  371. #
  372. # Limit number of entries processed (if desired)
  373. if (ii > max_entries):
  374. break