CSV_to_RDF_OA_IMG.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # Basic functions for triples / shortened triples in TTL format
  44. def triple(subject, predicate, object1):
  45. line = subject + ' ' + predicate + ' ' + object1
  46. return line
  47. def doublet(predicate, object1):
  48. line = ' ' + predicate + ' ' + object1
  49. return line
  50. def singlet(object1):
  51. line = ' ' + object1
  52. return line
  53. # Line endings in TTL format
  54. continueLine1 = ' ;\n'
  55. continueLine2 = ' ,\n'
  56. closeLine = ' .\n'
  57. def writeTTLHeader(output):
  58. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  59. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  60. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  61. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  62. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  63. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  64. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  65. output.write('@prefix ' + documentsCoords.prefix + ' ' + documentsCoords.uri + closeLine)
  66. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  67. output.write('@prefix ' + hasNoteCoords.prefix + ' ' + hasNoteCoords.uri + closeLine)
  68. output.write('\n')
  69. filePrefix = 'AR20IMR'
  70. fileType = '_Datini'
  71. max_entries = 1000000000
  72. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  73. export_dir + filePrefix + fileType + '_LICENZA.ttl', 'w') as output:
  74. reader = csv.DictReader(csv_file)
  75. writeTTLHeader(output)
  76. first = True
  77. ii = 0
  78. for row in reader:
  79. # The index ii is used to process a limited number of entries for testing purposes
  80. ii = ii + 1
  81. # columnName = list(row)
  82. url = row['URL']
  83. # placeHolders
  84. # if row['FTA0']!= '':
  85. # datplaceHolder = museoCoords.prefix + url
  86. id = row['FTA0'].replace(".jpeg","")
  87. e36placeHolder = museoCoords.prefix + url + "_" + id
  88. # e36e42placeHolder = museoCoords.prefix + url + "_" + id + "_E36_E42"
  89. # line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', datplaceHolder) + closeLine
  90. # output.write(line)
  91. # line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  92. # output.write(line)
  93. # line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Tavola\"') + closeLine
  94. # output.write(line)
  95. # line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Tavola\"') + closeLine
  96. # output.write(line)
  97. # line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  98. # output.write(line)
  99. # line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  100. # output.write(line)
  101. # line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['FTA0']+ '\"') + closeLine
  102. # output.write(line)
  103. # if row['NOTE']!= '':
  104. # line = triple(e36placeHolder, hasNoteCoords.prefix, '\"'+ row['NOTE']+'"') + closeLine
  105. # output.write(line)
  106. e62placeHolder = museoCoords.prefix + url + "_" + id + "_E36_E62"
  107. e62e42placeHolder = museoCoords.prefix + url + "_" + id + "_E62_E42"
  108. e36e30placeHolder = "<https://creativecommons.org/licenses/by-nc-sa/4.0/deed.it>"
  109. e36e30e42placeHolder = "<https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nc-sa.png>"
  110. line = triple(e36placeHolder, cidocCoords.prefix + 'P3_has_note',
  111. e62placeHolder) + closeLine
  112. output.write(line)
  113. line = triple(e62placeHolder, nsCoords.prefix + 'type',
  114. cidocCoords.prefix + 'E62_String') + closeLine
  115. output.write(line)
  116. line = triple(e62placeHolder, nsCoords.prefix + 'label',
  117. '\"Museo di Palazzo Pretorio di Prato\"') + closeLine
  118. output.write(line)
  119. line = triple(e62placeHolder, cidocCoords.prefix + 'P2_has_type',
  120. '\"Provenienza immagine\"') + closeLine
  121. output.write(line)
  122. line = triple(e62placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  123. "<https://palazzopretorio.prato.it/>") + closeLine
  124. output.write(line)
  125. line = triple(e62e42placeHolder, nsCoords.prefix + 'type',
  126. cidocCoords.prefix + 'E42_Identifier') + closeLine
  127. output.write(line)
  128. line = triple(e36placeHolder, cidocCoords.prefix + 'P104_is_subject_to',
  129. e36e30placeHolder) + closeLine
  130. output.write(line)
  131. line = triple(e36e30placeHolder, nsCoords.prefix + 'type',
  132. cidocCoords.prefix + 'E30_Right') + closeLine
  133. output.write(line)
  134. line = triple(e36e30placeHolder, nsCoords.prefix + 'label',
  135. '\"CC BY NC SA\"') + closeLine
  136. output.write(line)
  137. line = triple(e36e30placeHolder, cidocCoords.prefix + 'P1_is_identified_by',
  138. e36e30e42placeHolder) + closeLine
  139. output.write(line)
  140. line = triple(e36e30e42placeHolder, nsCoords.prefix + 'type',
  141. cidocCoords.prefix + 'E42_Identifier') + closeLine
  142. output.write(line)
  143. line = triple(e36e30e42placeHolder, nsCoords.prefix + 'label',
  144. '\"CC BY NC SA\"') + closeLine
  145. output.write(line)
  146. output.write('\n')
  147. #
  148. #
  149. # Limit number of entries processed (if desired)
  150. if (ii > max_entries):
  151. break