CSV_to_RDF_Autori.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/corretti/'
  18. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/RDF/'
  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('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  27. autCoords = RDFcoords('<https://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
  28. foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
  29. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  30. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  31. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  32. schemaCoords = RDFcoords('<http://www.schema.org/>', 'schema:')
  33. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  34. # Basic functions for triples / shortened triples in TTL format
  35. def triple(subject, predicate, object1):
  36. line = subject + ' ' + predicate + ' ' + object1
  37. return line
  38. def doublet(predicate, object1):
  39. line = ' ' + predicate + ' ' + object1
  40. return line
  41. def singlet(object1):
  42. line = ' ' + object1
  43. return line
  44. # Line endings in TTL format
  45. continueLine1 = ' ;\n'
  46. continueLine2 = ' ,\n'
  47. closeLine = ' .\n'
  48. def writeTTLHeader(output):
  49. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  50. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  51. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  52. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  53. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  54. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  55. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  56. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  57. output.write('\n')
  58. filePrefix = 'AR20AUT_'
  59. fileType = 'Datini'
  60. max_entries = 1000000000
  61. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  62. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  63. reader = csv.DictReader(csv_file)
  64. writeTTLHeader(output)
  65. first = True
  66. ii = 0
  67. for row in reader:
  68. # The index ii is used to process a limited number of entries for testing purposes
  69. ii = ii + 1
  70. url = row['URL']
  71. #placeHolders
  72. e21placeHolder = autCoords.prefix + url
  73. e62placeHolder = autCoords.prefix + url + '_nota'
  74. e41placeHolder = autCoords.prefix + url + '_E41'
  75. e42placeHolder = autCoords.prefix + row['AUTH']
  76. e67placeHolder = autCoords.prefix + url + '_E67'
  77. e69placeHolder = autCoords.prefix + url + '_E69'
  78. line = triple(e21placeHolder, nsCoords.prefix + 'type',
  79. cidocCoords.prefix + 'E21_Person') + closeLine
  80. output.write(line)
  81. line = triple(e21placeHolder,
  82. nsCoords.prefix + 'type',
  83. foafCoords.prefix + 'person') + closeLine
  84. output.write(line)
  85. if row['AUTN'] != '':
  86. line = triple(e21placeHolder,
  87. foafCoords.prefix + 'name',
  88. '\"' + row['AUTN'] + '\"') + closeLine
  89. output.write(line)
  90. if row['AUTC'] != '':
  91. line = triple(e21placeHolder,
  92. foafCoords.prefix + 'familyName',
  93. '\"' + row['AUTC'] + '\"') + closeLine
  94. output.write(line)
  95. if row['AUTO'] != '':
  96. line = triple(e21placeHolder,
  97. foafCoords.prefix + 'givenName',
  98. '\"' + row['AUTO'] + '\"') + closeLine
  99. output.write(line)
  100. if row['AUTZ'] != '':
  101. line = triple(e21placeHolder,
  102. foafCoords.prefix + 'gender',
  103. '\"' + row['AUTZ'] + '\"') + closeLine
  104. output.write(line)
  105. line = triple(e21placeHolder, rdfsCoords.prefix + 'label',
  106. '\"' + row['AUTN'] + ', ' + row['AUTA'] + '\"') + closeLine
  107. output.write(line)
  108. line = triple(e21placeHolder, cidocCoords.prefix + 'P3_has_note',
  109. e62placeHolder) + closeLine
  110. output.write(line)
  111. line = triple(e62placeHolder, nsCoords.prefix + 'type',
  112. cidocCoords.prefix + 'E62_String') + closeLine
  113. output.write(line)
  114. line = triple(e62placeHolder, rdfsCoords.prefix + 'label',
  115. '\"Fonte: Museo di Palazzo Pretorio - Collezione Datini\"') + closeLine
  116. output.write(line)
  117. #E21 - P1 - E42
  118. line = triple(e21placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  119. e42placeHolder) + closeLine
  120. output.write(line)
  121. line = triple(e42placeHolder, nsCoords.prefix + 'type',
  122. cidocCoords.prefix + 'E42_Identifier') + closeLine
  123. output.write(line)
  124. line = triple(e42placeHolder, rdfsCoords.prefix + 'label',
  125. '\"' + row['AUTH'] + '\"') + closeLine
  126. output.write(line)
  127. #E21 - P1 - E41
  128. '''line = triple(e21placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  129. e41placeHolder) + closeLine
  130. output.write(line)
  131. line = triple(e41placeHolder, nsCoords.prefix + 'type',
  132. cidocCoords.prefix + 'E41_Appellation') + closeLine
  133. output.write(line)
  134. line = triple(e41placeHolder, rdfsCoords.prefix + 'label',
  135. '\"' + row['AUTN'] + '\"') + closeLine
  136. output.write(line)'''
  137. # E21 - P107i - E74
  138. if row['AUTU'] != '':
  139. group = []
  140. if '/' in row['AUTU']:
  141. group = row['AUTU'].split('/')
  142. else:
  143. group.append(row['AUTU'])
  144. for gr in group:
  145. gg = gr.replace(' ', '')
  146. e74placeHolder = museoCoords.prefix + gg
  147. line = triple(e21placeHolder,
  148. cidocCoords.prefix + 'P107i_is_current_or_former_member_of',
  149. e74placeHolder) + closeLine
  150. output.write(line)
  151. line = triple(e74placeHolder,
  152. nsCoords.prefix + 'type',
  153. cidocCoords.prefix + 'E74_Group') + closeLine
  154. output.write(line)
  155. line = triple(e74placeHolder,
  156. rdfsCoords.prefix + 'label',
  157. '\"' + row['AUTU'] + '\"') + closeLine
  158. output.write(line)
  159. #E21 - P98i - E67
  160. line = triple(e21placeHolder,
  161. cidocCoords.prefix + 'P98i_was_born',
  162. e67placeHolder) + closeLine
  163. output.write(line)
  164. line = triple(e67placeHolder,
  165. nsCoords.prefix + 'type',
  166. cidocCoords.prefix + 'E67_Birth') + closeLine
  167. output.write(line)
  168. line = triple(e67placeHolder,
  169. rdfsCoords.prefix + 'label',
  170. '\"Nascita di ' + row['AUTN'] + '\"') + closeLine
  171. output.write(line)
  172. line = triple(e21placeHolder,
  173. cidocCoords.prefix + 'P100i_died_in',
  174. e69placeHolder) + closeLine
  175. output.write(line)
  176. line = triple(e69placeHolder,
  177. nsCoords.prefix + 'type',
  178. cidocCoords.prefix + 'E69_Death') + closeLine
  179. output.write(line)
  180. line = triple(e69placeHolder,
  181. rdfsCoords.prefix + 'label',
  182. '\"Morte di ' + row['AUTN'] + '\"') + closeLine
  183. output.write(line)
  184. #E67 - P7 - E53
  185. if row['AUTL'] != '':
  186. line = triple(e67placeHolder,
  187. cidocCoords.prefix + 'P7_took_place_at',
  188. museoCoords.prefix + row['AUTL']) + closeLine
  189. output.write(line)
  190. line = triple(museoCoords.prefix + row['AUTL'],
  191. nsCoords.prefix + 'type',
  192. cidocCoords.prefix + 'E53_Place') + closeLine
  193. output.write(line)
  194. line = triple(museoCoords.prefix + row['AUTL'],
  195. rdfsCoords.prefix + 'label',
  196. '\"' + row['AUTL'] + '\"') + closeLine
  197. output.write(line)
  198. # E67 - P4 - E52
  199. if row['AUTD'] != '':
  200. tt = row['AUTD'].replace(' ', '')
  201. tim = tt.replace('/', '')
  202. time = tim.replace('.', '')
  203. line = triple(e67placeHolder,
  204. cidocCoords.prefix + 'P4_has_time-span',
  205. museoCoords.prefix + time) + closeLine
  206. output.write(line)
  207. line = triple(museoCoords.prefix + time,
  208. nsCoords.prefix + 'type',
  209. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  210. output.write(line)
  211. line = triple(museoCoords.prefix + time,
  212. rdfsCoords.prefix + 'label',
  213. '\"' + row['AUTD'] + '\"') + closeLine
  214. output.write(line)
  215. # E69 - P7 - E53
  216. if row['AUTX'] != '':
  217. line = triple(e69placeHolder,
  218. cidocCoords.prefix + 'P7_took_place_at',
  219. museoCoords.prefix + row['AUTX']) + closeLine
  220. output.write(line)
  221. line = triple(museoCoords.prefix + row['AUTX'],
  222. nsCoords.prefix + 'type',
  223. cidocCoords.prefix + 'E53_Place') + closeLine
  224. output.write(line)
  225. line = triple(museoCoords.prefix + row['AUTX'],
  226. rdfsCoords.prefix + 'label',
  227. '\"' + row['AUTX'] + '\"') + closeLine
  228. output.write(line)
  229. # E69 - P4 - E52
  230. if row['AUTT'] != '':
  231. tt = row['AUTT'].replace(' ', '')
  232. tim = tt.replace('/', '')
  233. time = tim.replace('.', '')
  234. line = triple(e69placeHolder,
  235. cidocCoords.prefix + 'P4_has_time-span',
  236. museoCoords.prefix + time) + closeLine
  237. output.write(line)
  238. line = triple(museoCoords.prefix + time,
  239. nsCoords.prefix + 'type',
  240. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  241. output.write(line)
  242. line = triple(museoCoords.prefix + time,
  243. rdfsCoords.prefix + 'label',
  244. '\"' + row['AUTT'] + '\"') + closeLine
  245. output.write(line)
  246. # E21 - occupation
  247. if row['AUTQ'] != '':
  248. line = triple(e21placeHolder,
  249. schemaCoords.prefix + 'hasOccupation',
  250. museoCoords.prefix + row['AUTQ']) + closeLine
  251. output.write(line)
  252. line = triple(museoCoords.prefix + row['AUTQ'],
  253. nsCoords.prefix + 'type',
  254. schemaCoords.prefix + 'Occupation') + closeLine
  255. output.write(line)
  256. line = triple(museoCoords.prefix + row['AUTQ'],
  257. rdfsCoords.prefix + 'label',
  258. '\"' + row['AUTQ'] + '\"') + closeLine
  259. output.write(line)
  260. # E21 - P139 - E41
  261. if row['AUTV'] != '':
  262. autv = []
  263. if '/' in row['AUTV']:
  264. autv = row['AUTV'].split('/')
  265. else:
  266. autv.append(row['AUTV'])
  267. autvplaceHolder = museoCoords.prefix + row['AUTV'].replace(' ', '-').replace('\'', '')
  268. line = triple(e21placeHolder,
  269. cidocCoords.prefix + 'P139_has_alternative-form',
  270. '\"' + row['AUTV'] + '\"') + closeLine
  271. output.write(line)
  272. # E21 - P139 - E41
  273. # if row['AUTP'] != '':
  274. # autv = []
  275. # if '/' in row['AUTP']:
  276. # autv = row['AUTP'].split('/')
  277. # else:
  278. # autv.append(row['AUTP'])
  279. # line = triple(e21placeHolder,
  280. # schemaCoords.prefix + 'alternateName',
  281. # '\"' + row['AUTP'] + '\"') + closeLine
  282. #output.write(line)
  283. output.write('\n')
  284. #
  285. #
  286. # Limit number of entries processed (if desired)
  287. if (ii > max_entries):
  288. break