CSV_to_RDF_OA_IMG.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 http.cookiejar import CookieJar
  8. from urllib.request import urlopen
  9. #from bs4 import BeautifulSoup
  10. import urllib
  11. import json
  12. from socket import error as SocketError
  13. import html.parser
  14. # OPZIONAL IMPORTS
  15. # For timestamping/simple speed tests
  16. from datetime import datetime
  17. # Random number generator
  18. from random import *
  19. # System & command line utilities
  20. import sys
  21. # Json for the dictionary
  22. import json
  23. import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/corretti/'
  24. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/RDF/'
  25. # Custom class to store URIs + related infos for the ontologies/repositories
  26. class RDFcoords:
  27. def __init__(self, uri, prefix, code=None):
  28. self.uri = uri
  29. self.prefix = prefix
  30. self.code = code
  31. # Repositories
  32. museoCoords = RDFcoords('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  33. autCoords = RDFcoords('<https://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
  34. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  35. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  36. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  37. xsdCoords = RDFcoords('<http://www.w3.org/2001/XMLSchema#>', 'xsd:')
  38. iconCoords = RDFcoords('<http://iconclass.org/>', 'ico:')
  39. documentsCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P70_documents>', 'ds:')
  40. schemaCoords = RDFcoords('<http://www.schema.org/>', 'schema:')
  41. rdfCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdf:')
  42. hasNoteCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P3_has_note>', 'no:')
  43. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  44. # Basic functions for triples / shortened triples in TTL format
  45. def triple(subject, predicate, object1):
  46. line = subject + ' ' + predicate + ' ' + object1
  47. return line
  48. def doublet(predicate, object1):
  49. line = ' ' + predicate + ' ' + object1
  50. return line
  51. def singlet(object1):
  52. line = ' ' + object1
  53. return line
  54. # Line endings in TTL format
  55. continueLine1 = ' ;\n'
  56. continueLine2 = ' ,\n'
  57. closeLine = ' .\n'
  58. def writeTTLHeader(output):
  59. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  60. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  61. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  62. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  63. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  64. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  65. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  66. output.write('@prefix ' + documentsCoords.prefix + ' ' + documentsCoords.uri + closeLine)
  67. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  68. output.write('@prefix ' + hasNoteCoords.prefix + ' ' + hasNoteCoords.uri + closeLine)
  69. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  70. output.write('\n')
  71. filePrefix = 'AR20IMR'
  72. fileType = '_Ospedale'
  73. max_entries = 1000000000
  74. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  75. export_dir + filePrefix + fileType + '_LICENZA.ttl', 'w') as output:
  76. reader = csv.DictReader(csv_file)
  77. writeTTLHeader(output)
  78. first = True
  79. ii = 0
  80. for row in reader:
  81. # The index ii is used to process a limited number of entries for testing purposes
  82. ii = ii + 1
  83. # columnName = list(row)
  84. url = row['URL']
  85. # placeHolders
  86. # if row['FTA0']!= '':
  87. # datplaceHolder = museoCoords.prefix + url
  88. id = row['FTA0'].replace(".jpeg","")
  89. e36placeHolder = museoCoords.prefix + url + "_" + id
  90. # e36e42placeHolder = museoCoords.prefix + url + "_" + id + "_E36_E42"
  91. # line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', datplaceHolder) + closeLine
  92. # output.write(line)
  93. # line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  94. # output.write(line)
  95. # line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Tavola\"') + closeLine
  96. # output.write(line)
  97. # line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Tavola\"') + closeLine
  98. # output.write(line)
  99. # line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  100. # output.write(line)
  101. # line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  102. # output.write(line)
  103. # line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['FTA0']+ '\"') + closeLine
  104. # output.write(line)
  105. # if row['NOTE']!= '':
  106. # line = triple(e36placeHolder, hasNoteCoords.prefix, '\"'+ row['NOTE']+'"') + closeLine
  107. # output.write(line)
  108. e62placeHolder = museoCoords.prefix + url + "_" + id + "_E36_E62"
  109. e62e42placeHolder = museoCoords.prefix + url + "_" + id + "_E62_E42"
  110. e36e30placeHolder = "<https://creativecommons.org/licenses/by-nc-sa/4.0/deed.it>"
  111. e36e30e42placeHolder = "<https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nc-sa.png>"
  112. line = triple(e36placeHolder, cidocCoords.prefix + 'P3_has_note',
  113. e62placeHolder) + closeLine
  114. output.write(line)
  115. line = triple(e62placeHolder, nsCoords.prefix + 'type',
  116. cidocCoords.prefix + 'E62_String') + closeLine
  117. output.write(line)
  118. line = triple(e62placeHolder, rdfsCoords.prefix + 'label',
  119. '\"Museo di Palazzo Pretorio di Prato\"') + closeLine
  120. output.write(line)
  121. line = triple(e62placeHolder, cidocCoords.prefix + 'P2_has_type',
  122. '\"Provenienza immagine\"') + closeLine
  123. output.write(line)
  124. line = triple(e62placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  125. "<https://palazzopretorio.prato.it/>") + closeLine
  126. output.write(line)
  127. line = triple(e62e42placeHolder, nsCoords.prefix + 'type',
  128. cidocCoords.prefix + 'E42_Identifier') + closeLine
  129. output.write(line)
  130. line = triple(e36placeHolder, cidocCoords.prefix + 'P104_is_subject_to',
  131. e36e30placeHolder) + closeLine
  132. output.write(line)
  133. line = triple(e36e30placeHolder, nsCoords.prefix + 'type',
  134. cidocCoords.prefix + 'E30_Right') + closeLine
  135. output.write(line)
  136. line = triple(e36e30placeHolder, rdfsCoords.prefix + 'label',
  137. '\"CC BY NC SA\"') + closeLine
  138. output.write(line)
  139. line = triple(e36e30placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  140. e36e30e42placeHolder) + closeLine
  141. output.write(line)
  142. line = triple(e36e30e42placeHolder, nsCoords.prefix + 'type',
  143. cidocCoords.prefix + 'E42_Identifier') + closeLine
  144. output.write(line)
  145. line = triple(e36e30e42placeHolder, rdfsCoords.prefix + 'label',
  146. '\"CC BY NC SA\"') + closeLine
  147. output.write(line)
  148. output.write('\n')
  149. #
  150. #
  151. # Limit number of entries processed (if desired)
  152. if (ii > max_entries):
  153. break