CSV_to_RDF_cataloghi.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. from urllib.request import urlopen
  8. from bs4 import BeautifulSoup
  9. import json
  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/MPP/CSV/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/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. museoCoords = RDFcoords('<http://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  29. autCoords = RDFcoords('<http://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
  30. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  31. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  32. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  33. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  34. xsdCoords = RDFcoords('<http://www.w3.org/2001/XMLSchema#>', 'xsd:')
  35. iconCoords = RDFcoords('<http://iconclass.org/>', 'ico:')
  36. foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
  37. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  38. # Basic functions for triples / shortened triples in TTL format
  39. def triple(subject, predicate, object1):
  40. line = subject + ' ' + predicate + ' ' + object1
  41. return line
  42. def doublet(predicate, object1):
  43. line = ' ' + predicate + ' ' + object1
  44. return line
  45. def singlet(object1):
  46. line = ' ' + object1
  47. return line
  48. # Line endings in TTL format
  49. continueLine1 = ' ;\n'
  50. continueLine2 = ' ,\n'
  51. closeLine = ' .\n'
  52. def writeTTLHeader(output):
  53. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  54. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  55. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  56. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  57. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  58. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  59. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  60. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  61. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  62. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  63. output.write('\n')
  64. filePrefix = 'OA'
  65. fileType = '_catalogo'
  66. max_entries = 1000000000
  67. def get_aut_url(code):
  68. aut_file = open(import_dir + 'AR20AUT_' + fileType + '.csv', newline="")
  69. reader = csv.DictReader(aut_file)
  70. for row in reader:
  71. auth = int(row['AUTH'])
  72. cod = int(code)
  73. role = ''
  74. if row['AUTQ'] != '':
  75. role = row['AUTQ']
  76. else:
  77. role = ''
  78. if auth == cod:
  79. return [row['URL'], role]
  80. def get_role(role):
  81. role_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_RUOLI.csv', newline="")
  82. reader = csv.DictReader(role_file)
  83. for row in reader:
  84. if row['Label'] == role:
  85. return row['AAT']
  86. def get_elem(mtc):
  87. mtc_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_MTC.csv', newline="")
  88. reader = csv.DictReader(mtc_file)
  89. for row in reader:
  90. if row['MTC'] == mtc:
  91. return [row['AAT'], row['Type']]
  92. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  93. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  94. reader = csv.DictReader(csv_file)
  95. writeTTLHeader(output)
  96. first = True
  97. ii = 0
  98. e55invplaceHolder = "<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/INV1979>"
  99. line = triple(e55invplaceHolder, schemaCoords.prefix + 'label', '\"Inventario 1970\"') + closeLine
  100. output.write(line)
  101. e55AplaceHolder = "<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/ARCHIVE>"
  102. line = triple(e55AplaceHolder, schemaCoords.prefix + 'label', '\"Riferimento bibliografico\"') + closeLine
  103. output.write(line)
  104. for row in reader:
  105. # The index ii is used to process a limited number of entries for testing purposes
  106. ii = ii + 1
  107. # placeHolders
  108. urlplaceHolder = '<' + row['Url OPERA'] + '>'
  109. e36placeHolder = '<' + row['Url OPERA'] + '_E36>'
  110. e36e42placeHolder = '<' + row['Url OPERA'] + '_E36_E42>'
  111. e36placeHolder2 = '<' + row['Url OPERA'] + '_E36_2>'
  112. e36e42placeHolder2 = '<' + row['Url OPERA'] + '_E36_2_E42>'
  113. e36placeHolder3 = '<' + row['Url OPERA'] + '_E36_3>'
  114. e36e42placeHolder3 = '<' + row['Url OPERA'] + '_E36_3_E42>'
  115. e31placeHolder = '<' + row['Url OPERA'] + '_E31>'
  116. e3142placeHolder = '<' + row['Url OPERA'] + '_E31_E42>'
  117. e31e36rplaceHolder = '<' + row['Url OPERA'] + '_E31_E36_r>'
  118. e31e36re42placeHolder = '<' + row['Url OPERA'] + '_E31_E36_r_E42>'
  119. e31e36vplaceHolder = '<' + row['Url OPERA'] + '_E31_E36_v>'
  120. e31e36ve42placeHolder = '<' + row['Url OPERA'] + '_E31_E36_v_E42>'
  121. e31AplaceHolder = '<' + row['LINK ARCHIVE.org'] + '>'
  122. e31Ae42placeHolder = '<' + row['LINK ARCHIVE.org'] + '_E42>'
  123. e13placeHolder = '<' + row['Url OPERA'] + '_E13_FA>'
  124. e31Ae55placeHolder = '<' + row['AAT (Attribution)'] + '_E31_E55>'
  125. e12placeHolder = '<' + row['Url OPERA'] + '_E12>'
  126. e12FplaceHolder = '<' + row['Url OPERA'] + '_E12F>'
  127. line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', urlplaceHolder) + closeLine
  128. output.write(line)
  129. line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  130. output.write(line)
  131. line = triple(e36placeHolder, schemaCoords.prefix + 'label', '\"Immagine digitale dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
  132. output.write(line)
  133. line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  134. output.write(line)
  135. line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  136. output.write(line)
  137. line = triple(e36e42placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Immagine opera 1']+ '\"') + closeLine
  138. output.write(line)
  139. if (row['ID Immagine opera 2']!= ''):
  140. line = triple(e36placeHolder2, cidocCoords.prefix + 'P138_represents', urlplaceHolder) + closeLine
  141. output.write(line)
  142. line = triple(e36placeHolder2, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  143. output.write(line)
  144. line = triple(e36placeHolder2, schemaCoords.prefix + 'label', '\"Immagine digitale dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
  145. output.write(line)
  146. line = triple(e36placeHolder2, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder2) + closeLine
  147. output.write(line)
  148. line = triple(e36e42placeHolder2, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  149. output.write(line)
  150. line = triple(e36e42placeHolder2, schemaCoords.prefix + 'label', '\"'+row['ID Immagine opera 2']+ '\"') + closeLine
  151. output.write(line)
  152. if (row['ID Immagine opera 3']!= ''):
  153. line = triple(e36placeHolder3, cidocCoords.prefix + 'P138_represents', urlplaceHolder) + closeLine
  154. output.write(line)
  155. line = triple(e36placeHolder3, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  156. output.write(line)
  157. line = triple(e36placeHolder3, schemaCoords.prefix + 'label', '\"Immagine digitale dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
  158. output.write(line)
  159. line = triple(e36placeHolder3, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder3) + closeLine
  160. output.write(line)
  161. line = triple(e36e42placeHolder3, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  162. output.write(line)
  163. line = triple(e36e42placeHolder3, schemaCoords.prefix + 'label', '\"'+row['ID Immagine opera 3']+ '\"') + closeLine
  164. output.write(line)
  165. if (row['ID Inventario 1970']!= ''):
  166. line = triple(e31placeHolder, cidocCoords.prefix + 'P70_documents', urlplaceHolder) + closeLine
  167. output.write(line)
  168. line = triple(e31placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E31_Document') + closeLine
  169. output.write(line)
  170. line = triple(e31placeHolder, schemaCoords.prefix + 'label', '\"Inventario 1970\"') + closeLine
  171. output.write(line)
  172. line = triple(e31placeHolder, cidocCoords.prefix + 'P2_has_type', e55invplaceHolder) + closeLine
  173. output.write(line)
  174. line = triple(e31placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e3142placeHolder) + closeLine
  175. output.write(line)
  176. line = triple(e3142placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  177. output.write(line)
  178. line = triple(e3142placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Inventario 1970']+ '\"') + closeLine
  179. output.write(line)
  180. if (row['ID Immagine Inventario 1970 recto']!= ''):
  181. line = triple(e31e36rplaceHolder, cidocCoords.prefix + 'P138_represents', e31placeHolder) + closeLine
  182. output.write(line)
  183. line = triple(e31e36rplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  184. output.write(line)
  185. line = triple(e31e36rplaceHolder, schemaCoords.prefix + 'label', '\"Rappresentazione digitale della scheda opera d\'arte su ' + "\'Inventario 1970\' - recto" + '\"' ) + closeLine
  186. output.write(line)
  187. line = triple(e31e36rplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e31e36re42placeHolder) + closeLine
  188. output.write(line)
  189. line = triple(e31e36re42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  190. output.write(line)
  191. line = triple(e31e36re42placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Immagine Inventario 1970 recto']+ '\"') + closeLine
  192. output.write(line)
  193. if (row['ID Immagine Inventario 1970 verso']!= ''):
  194. line = triple(e31e36vplaceHolder, cidocCoords.prefix + 'P138_represents', e31placeHolder) + closeLine
  195. output.write(line)
  196. line = triple(e31e36vplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  197. output.write(line)
  198. line = triple(e31e36vplaceHolder, schemaCoords.prefix + 'label', '\"Rappresentazione digitale della scheda opera d\'arte su ' + "\'Inventario 1970\' - verso" + '\"' ) + closeLine
  199. output.write(line)
  200. line = triple(e31e36vplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e31e36ve42placeHolder) + closeLine
  201. output.write(line)
  202. line = triple(e31e36ve42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  203. output.write(line)
  204. line = triple(e31e36ve42placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Immagine Inventario 1970 verso']+ '\"') + closeLine
  205. output.write(line)
  206. if (row['LINK ARCHIVE.org']!= ''):
  207. line = triple(e31AplaceHolder, cidocCoords.prefix + 'P70_documents', urlplaceHolder) + closeLine
  208. output.write(line)
  209. line = triple(e31AplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E31_Document') + closeLine
  210. output.write(line)
  211. line = triple(e31AplaceHolder, schemaCoords.prefix + 'label', '\"Riferimento bibliografico della scheda opera d\'arte su ARCHIVE.org\"') + closeLine
  212. output.write(line)
  213. line = triple(e31AplaceHolder, cidocCoords.prefix + 'P2_has_type', e55AplaceHolder) + closeLine
  214. output.write(line)
  215. line = triple(e31AplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e31Ae42placeHolder) + closeLine
  216. output.write(line)
  217. line = triple(e31Ae42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  218. output.write(line)
  219. line = triple(e31Ae42placeHolder, schemaCoords.prefix + 'label', '\"'+row['BIBH']+ '\"') + closeLine
  220. output.write(line)
  221. if (row['FORMER ATTRIBUTION']!= '' and row['FORMER ATTRIBUTION']!= ' ' and row['FORMER ATTRIBUTION'] is not None):
  222. line = triple(e12placeHolder, cidocCoords.prefix + 'P141_assigned', e13placeHolder) + closeLine
  223. output.write(line)
  224. line = triple(e12FplaceHolder, cidocCoords.prefix + 'P141_assigned', e13placeHolder) + closeLine
  225. output.write(line)
  226. if (row['LINK ARCHIVE.org']!= ''):
  227. line = triple(e13placeHolder, cidocCoords.prefix + 'P42_assigned', e31AplaceHolder) + closeLine
  228. output.write(line)
  229. line = triple(e13placeHolder, cidocCoords.prefix + 'P2_has_type', e31Ae55placeHolder) + closeLine
  230. output.write(line)
  231. line = triple(e31Ae55placeHolder, schemaCoords.prefix + 'label', '\"AAT Attribution\"') + closeLine
  232. output.write(line)
  233. line = triple(e13placeHolder, schemaCoords.prefix + 'label', '\"Former Attribution dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
  234. output.write(line)
  235. e21ULANplaceHolder = '<'+row['ULAN']+'>'
  236. if (row['LINK SCHEDA AUT']!= ''):
  237. e21AUTplaceHolder = '<'+row['LINK SCHEDA AUT']+'>'
  238. line = triple(e13placeHolder, cidocCoords.prefix + 'P141_assigned', e21AUTplaceHolder) + closeLine
  239. output.write(line)
  240. if (row['ULAN']!= ''):
  241. line = triple(e21AUTplaceHolder, owlCoords.prefix + 'sameAs', e21ULANplaceHolder) + closeLine
  242. output.write(line)
  243. if (row['VIAF']!= ''):
  244. e21VIAFplaceHolder = '<'+row['VIAF']+'>'
  245. line = triple(e21AUTplaceHolder, owlCoords.prefix + 'sameAs', e21VIAFplaceHolder) + closeLine
  246. output.write(line)
  247. else:
  248. line = triple(e13placeHolder, cidocCoords.prefix + 'P141_assigned', e21ULANplaceHolder) + closeLine
  249. output.write(line)
  250. line = triple(e21ULANplaceHolder, schemaCoords.prefix + 'label', '\"' + row['FORMER ATTRIBUTION']+ '\"') + closeLine
  251. output.write(line)
  252. line = triple(e21ULANplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E21_Person') + closeLine
  253. output.write(line)
  254. line = triple(e21ULANplaceHolder, nsCoords.prefix + 'type', foafCoords.prefix + 'person') + closeLine
  255. output.write(line)
  256. if (row['VIAF']!= ''):
  257. e21VIAFplaceHolder = '<'+row['VIAF']+'>'
  258. line = triple(e21ULANplaceHolder, owlCoords.prefix + 'sameAs', e21VIAFplaceHolder) + closeLine
  259. output.write(line)
  260. output.write('\n')
  261. #
  262. #
  263. # Limit number of entries processed (if desired)
  264. if (ii > max_entries):
  265. break