CSV_to_RDF_onomastica_ospedale.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/DallASPO/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/Data/DallASPO/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 = 'onomastica_'
  61. fileType = 'ospedale'
  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. avo1 = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/avo1>"
  199. line = triple(aspoPlaceHolder,
  200. schemaCoords.prefix + 'relatedTo',
  201. avo1) + closeLine
  202. output.write(line)
  203. line = triple(avo1,
  204. nsCoords.prefix + 'type',
  205. foafCoords.prefix + 'Person') + closeLine
  206. output.write(line)
  207. line = triple(avo1,
  208. rdfsCoords.prefix + 'label',
  209. '\"' + row['avo 1'] + '\"') + closeLine
  210. output.write(line)
  211. if row['avo 2'] != '':
  212. avo2 = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/avo2>"
  213. line = triple(aspoPlaceHolder,
  214. schemaCoords.prefix + 'relatedTo',
  215. avo2) + closeLine
  216. output.write(line)
  217. line = triple(avo2,
  218. nsCoords.prefix + 'type',
  219. foafCoords.prefix + 'Person') + closeLine
  220. output.write(line)
  221. line = triple(avo2,
  222. rdfsCoords.prefix + 'label',
  223. '\"' + row['avo 2'] + '\"') + closeLine
  224. output.write(line)
  225. if row['Qualifica'] != '':
  226. qualifiche = []
  227. pipe = "|"
  228. if pipe in row['Qualifica']:
  229. qualifiche = row['Qualifica'].split('|')
  230. for qualifica in qualifiche:
  231. #Remove all white-space characters:
  232. txt = qualifica
  233. x = re.sub("\n", " ", txt)
  234. y = re.sub("\s\s", " ", x)
  235. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + str(y) + '\"') + closeLine
  236. output.write(line)
  237. else:
  238. #Remove all white-space characters:
  239. txt = row['Qualifica']
  240. x = re.sub("\n", " ", txt)
  241. y = re.sub("\s\s", " ", x)
  242. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + y + '\"') + closeLine
  243. output.write(line)
  244. if row['biogHist p'] != '':
  245. #Remove all white-space characters:
  246. txt = row['biogHist p']
  247. x = re.sub("\n", " ", txt)
  248. y = re.sub("\s\s", " ", x)
  249. note = re.sub("\"", "", x)
  250. line = triple(aspoPlaceHolder,
  251. cidocCoords.prefix + 'P3_has_note',
  252. '\"' + note + '\"') + closeLine
  253. output.write(line)
  254. if row['Variante'] != '':
  255. varianti = []
  256. pipe = "|"
  257. if pipe in row['Variante']:
  258. varianti = row['Variante'].split('|')
  259. for variante in varianti:
  260. line = triple(aspoPlaceHolder,
  261. owlCoords.prefix + 'sameAs',
  262. aspoCoords.prefix + str(variante)) + closeLine
  263. output.write(line)
  264. else:
  265. line = triple(aspoPlaceHolder,
  266. owlCoords.prefix + 'sameAs',
  267. aspoCoords.prefix + row['Variante']) + closeLine
  268. output.write(line)
  269. output.write('\n')
  270. #
  271. #
  272. # Limit number of entries processed (if desired)
  273. if (ii > max_entries):
  274. break