CSV_to_RDF_Autori.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # Utilities to read/write csv files
  2. import csv
  3. # Utilities to handle character encodings
  4. import unicodedata
  5. # Ordered Dicts
  6. from collections import OrderedDict
  7. import json
  8. # OPZIONAL IMPORTS
  9. # For timestamping/simple speed tests
  10. from datetime import datetime
  11. # Random number generator
  12. from random import *
  13. # System & command line utilities
  14. import sys
  15. # Json for the dictionary
  16. import json
  17. import_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/Datini/mod/'
  18. export_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/Carica/AUT/'
  19. # Custom class to store URIs + related infos for the ontologies/repositories
  20. class RDFcoords:
  21. def __init__(self, uri, prefix, code = None):
  22. self.uri = uri
  23. self.prefix = prefix
  24. self.code = code
  25. # Repositories
  26. museoCoords = RDFcoords('<http://palazzopretorio.comune.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  27. autCoords = RDFcoords('<http://palazzopretorio.comune.prato.it/it/opere/autori/>', 'aut:')
  28. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  29. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  30. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  31. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  32. # Basic functions for triples / shortened triples in TTL format
  33. def triple(subject, predicate, object1):
  34. line = subject + ' ' + predicate + ' ' + object1
  35. return line
  36. def doublet(predicate, object1):
  37. line = ' ' + predicate + ' ' + object1
  38. return line
  39. def singlet(object1):
  40. line = ' ' + object1
  41. return line
  42. # Line endings in TTL format
  43. continueLine1 = ' ;\n'
  44. continueLine2 = ' ,\n'
  45. closeLine = ' .\n'
  46. def writeTTLHeader(output):
  47. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  48. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  49. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  50. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  51. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  52. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  53. output.write('\n')
  54. filePrefix = 'AR20AUT_'
  55. fileType = 'Datini'
  56. max_entries = 1000000000
  57. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  58. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  59. reader = csv.DictReader(csv_file)
  60. writeTTLHeader(output)
  61. first = True
  62. ii = 0
  63. for row in reader:
  64. # The index ii is used to process a limited number of entries for testing purposes
  65. ii = ii + 1
  66. url = row['URL']
  67. #placeHolders
  68. e21placeHolder = autCoords.prefix + url
  69. e62placeHolder = autCoords.prefix + url + '_E62'
  70. e41placeHolder = autCoords.prefix + url + '_E41'
  71. e42placeHolder = autCoords.prefix + row['AUTH']
  72. e67placeHolder = autCoords.prefix + url + '_E67'
  73. e69placeHolder = autCoords.prefix + url + '_E69'
  74. line = triple(e21placeHolder, nsCoords.prefix + 'type',
  75. cidocCoords.prefix + 'E21_Person') + closeLine
  76. output.write(line)
  77. line = triple(e21placeHolder, schemaCoords.prefix + 'label',
  78. '\"' + row['AUTN'] + ', ' + row['AUTA'] + '\"') + closeLine
  79. output.write(line)
  80. line = triple(e21placeHolder, cidocCoords.prefix + 'P3_has_note',
  81. e62placeHolder) + closeLine
  82. output.write(line)
  83. line = triple(e62placeHolder, nsCoords.prefix + 'type',
  84. cidocCoords.prefix + 'E62_String') + closeLine
  85. output.write(line)
  86. line = triple(e62placeHolder, schemaCoords.prefix + 'label',
  87. '\"Fonte: Museo di Palazzo Pretorio - Collezione Ospedale\"') + closeLine
  88. output.write(line)
  89. #E21 - P1 - E42
  90. line = triple(e21placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  91. e42placeHolder) + closeLine
  92. output.write(line)
  93. line = triple(e42placeHolder, nsCoords.prefix + 'type',
  94. cidocCoords.prefix + 'E42_Identifier') + closeLine
  95. output.write(line)
  96. line = triple(e42placeHolder, schemaCoords.prefix + 'label',
  97. '\"' + row['AUTH'] + '\"') + closeLine
  98. output.write(line)
  99. #E21 - P1 - E41
  100. '''line = triple(e21placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  101. e41placeHolder) + closeLine
  102. output.write(line)
  103. line = triple(e41placeHolder, nsCoords.prefix + 'type',
  104. cidocCoords.prefix + 'E41_Appellation') + closeLine
  105. output.write(line)
  106. line = triple(e41placeHolder, schemaCoords.prefix + 'label',
  107. '\"' + row['AUTN'] + '\"') + closeLine
  108. output.write(line)'''
  109. # E21 - P107i - E74
  110. if row['AUTU'] != '':
  111. group = []
  112. if '/' in row['AUTU']:
  113. group = row['AUTU'].split('/')
  114. else:
  115. group.append(row['AUTU'])
  116. for gr in group:
  117. gg = gr.replace(' ', '')
  118. e74placeHolder = museoCoords.prefix + gg
  119. line = triple(e21placeHolder,
  120. cidocCoords.prefix + 'P107i_is_current_or_former_member_of',
  121. e74placeHolder) + closeLine
  122. output.write(line)
  123. line = triple(e74placeHolder,
  124. nsCoords.prefix + 'type',
  125. cidocCoords.prefix + 'E74_Group') + closeLine
  126. output.write(line)
  127. line = triple(e74placeHolder,
  128. schemaCoords.prefix + 'label',
  129. '\"' + row['AUTU'] + '\"') + closeLine
  130. output.write(line)
  131. #E21 - P98i - E67
  132. line = triple(e21placeHolder,
  133. cidocCoords.prefix + 'P98i_was_born',
  134. e67placeHolder) + closeLine
  135. output.write(line)
  136. line = triple(e67placeHolder,
  137. nsCoords.prefix + 'type',
  138. cidocCoords.prefix + 'E67_Birth') + closeLine
  139. output.write(line)
  140. line = triple(e67placeHolder,
  141. schemaCoords.prefix + 'label',
  142. '\"Nascita di ' + row['AUTN'] + '\"') + closeLine
  143. output.write(line)
  144. line = triple(e21placeHolder,
  145. cidocCoords.prefix + 'P100i_died_in',
  146. e69placeHolder) + closeLine
  147. output.write(line)
  148. line = triple(e69placeHolder,
  149. nsCoords.prefix + 'type',
  150. cidocCoords.prefix + 'E69_Death') + closeLine
  151. output.write(line)
  152. line = triple(e69placeHolder,
  153. schemaCoords.prefix + 'label',
  154. '\"Morte di ' + row['AUTN'] + '\"') + closeLine
  155. output.write(line)
  156. #E67 - P7 - E53
  157. if row['AUTL'] != '':
  158. line = triple(e67placeHolder,
  159. cidocCoords.prefix + 'P7_took_place_at',
  160. museoCoords.prefix + row['AUTL']) + closeLine
  161. output.write(line)
  162. line = triple(museoCoords.prefix + row['AUTL'],
  163. nsCoords.prefix + 'type',
  164. cidocCoords.prefix + 'E53_Place') + closeLine
  165. output.write(line)
  166. line = triple(museoCoords.prefix + row['AUTL'],
  167. schemaCoords.prefix + 'label',
  168. '\"' + row['AUTL'] + '\"') + closeLine
  169. output.write(line)
  170. # E67 - P4 - E52
  171. if row['AUTD'] != '':
  172. tt = row['AUTD'].replace(' ', '')
  173. tim = tt.replace('/', '')
  174. time = tim.replace('.', '')
  175. line = triple(e67placeHolder,
  176. cidocCoords.prefix + 'P4_has_time-span',
  177. museoCoords.prefix + time) + closeLine
  178. output.write(line)
  179. line = triple(museoCoords.prefix + time,
  180. nsCoords.prefix + 'type',
  181. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  182. output.write(line)
  183. line = triple(museoCoords.prefix + time,
  184. schemaCoords.prefix + 'label',
  185. '\"' + row['AUTD'] + '\"') + closeLine
  186. output.write(line)
  187. # E69 - P7 - E53
  188. if row['AUTX'] != '':
  189. line = triple(e69placeHolder,
  190. cidocCoords.prefix + 'P7_took_place_at',
  191. museoCoords.prefix + row['AUTX']) + closeLine
  192. output.write(line)
  193. line = triple(museoCoords.prefix + row['AUTX'],
  194. nsCoords.prefix + 'type',
  195. cidocCoords.prefix + 'E53_Place') + closeLine
  196. output.write(line)
  197. line = triple(museoCoords.prefix + row['AUTX'],
  198. schemaCoords.prefix + 'label',
  199. '\"' + row['AUTX'] + '\"') + closeLine
  200. output.write(line)
  201. # E69 - P4 - E52
  202. if row['AUTT'] != '':
  203. tt = row['AUTT'].replace(' ', '')
  204. tim = tt.replace('/', '')
  205. time = tim.replace('.', '')
  206. line = triple(e69placeHolder,
  207. cidocCoords.prefix + 'P4_has_time-span',
  208. museoCoords.prefix + time) + closeLine
  209. output.write(line)
  210. line = triple(museoCoords.prefix + time,
  211. nsCoords.prefix + 'type',
  212. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  213. output.write(line)
  214. line = triple(museoCoords.prefix + time,
  215. schemaCoords.prefix + 'label',
  216. '\"' + row['AUTT'] + '\"') + closeLine
  217. output.write(line)
  218. output.write('\n')
  219. #
  220. #
  221. # Limit number of entries processed (if desired)
  222. if (ii > max_entries):
  223. break