CSV_to_RDF_OA_BIB_IMG.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  38. xsdCoords = RDFcoords('<http://www.w3.org/2001/XMLSchema#>', 'xsd:')
  39. iconCoords = RDFcoords('<http://iconclass.org/>', 'ico:')
  40. documentsCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P70_documents>', 'ds:')
  41. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  42. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  43. schemaCoords = RDFcoords('<http://www.schema.org/>', 'schema:')
  44. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  45. # Basic functions for triples / shortened triples in TTL format
  46. def triple(subject, predicate, object1):
  47. line = subject + ' ' + predicate + ' ' + object1
  48. return line
  49. def doublet(predicate, object1):
  50. line = ' ' + predicate + ' ' + object1
  51. return line
  52. def singlet(object1):
  53. line = ' ' + object1
  54. return line
  55. # Line endings in TTL format
  56. continueLine1 = ' ;\n'
  57. continueLine2 = ' ,\n'
  58. closeLine = ' .\n'
  59. def writeTTLHeader(output):
  60. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  61. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  62. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  63. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  64. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  65. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  66. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  67. output.write('@prefix ' + documentsCoords.prefix + ' ' + documentsCoords.uri + closeLine)
  68. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  69. output.write('\n')
  70. filePrefix = 'OA_BIB_'
  71. fileType = 'IMG'
  72. max_entries = 1000000000
  73. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  74. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  75. reader = csv.DictReader(csv_file)
  76. writeTTLHeader(output)
  77. first = True
  78. ii = 0
  79. for row in reader:
  80. # The index ii is used to process a limited number of entries for testing purposes
  81. ii = ii + 1
  82. # columnName = list(row)
  83. # placeHolders
  84. pp = row['NCT']
  85. if row['BIBN-SO-1']!= '':
  86. e73placeHolderEDPP = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + pp + ">"
  87. id = row['BIBN-SO-1'].replace(".jpg", "")
  88. e36placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + ">"
  89. e36e42placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + '_E36_E42>'
  90. line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolderEDPP) + closeLine
  91. output.write(line)
  92. line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  93. output.write(line)
  94. line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Opera\"') + closeLine
  95. output.write(line)
  96. line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Opera\"') + closeLine
  97. output.write(line)
  98. line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  99. output.write(line)
  100. line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  101. output.write(line)
  102. line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['BIBN-SO-1']+ '\"') + closeLine
  103. output.write(line)
  104. if row['BIBN-SO-2']!= '':
  105. e73placeHolderEDPP = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + pp + ">"
  106. id = row['BIBN-SO-2'].replace(".jpg", "")
  107. e36placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + ">"
  108. e36e42placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + '_E36_E42>'
  109. line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolderEDPP) + closeLine
  110. output.write(line)
  111. line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  112. output.write(line)
  113. line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Opera\"') + closeLine
  114. output.write(line)
  115. line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Opera\"') + closeLine
  116. output.write(line)
  117. line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  118. output.write(line)
  119. line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  120. output.write(line)
  121. line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['BIBN-SO-2']+ '\"') + closeLine
  122. output.write(line)
  123. if row['BIBN-SO-3']!= '':
  124. e73placeHolderEDPP = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + pp + ">"
  125. id = row['BIBN-SO-3'].replace(".jpg", "")
  126. e36placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + ">"
  127. e36e42placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + '_E36_E42>'
  128. line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolderEDPP) + closeLine
  129. output.write(line)
  130. line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  131. output.write(line)
  132. line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Opera\"') + closeLine
  133. output.write(line)
  134. line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Opera\"') + closeLine
  135. output.write(line)
  136. line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  137. output.write(line)
  138. line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  139. output.write(line)
  140. line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['BIBN-SO-3']+ '\"') + closeLine
  141. output.write(line)
  142. if row['FNTI-SS']!= '':
  143. e73placeHolder = "<"+ row['ID OPERA'].replace(" ","")+ "_E73>"
  144. id = row['FNTI-SS'].replace(".pdf", "")
  145. e36placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + ">"
  146. e36e42placeHolder = "<https://palazzopretorio.prato.it/it/bib/" + row['BIBH'] + "/E73" + "_ED_" + id + "_E36_E42>"
  147. line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolder) + closeLine
  148. output.write(line)
  149. line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  150. output.write(line)
  151. line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Storica\"') + closeLine
  152. output.write(line)
  153. line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Storica\"') + closeLine
  154. output.write(line)
  155. line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  156. output.write(line)
  157. line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  158. output.write(line)
  159. line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['FNTI-SS'].replace("pdf","jpg")+ '\"') + closeLine
  160. output.write(line)
  161. output.write('\n')
  162. #
  163. #
  164. # Limit number of entries processed (if desired)
  165. if (ii > max_entries):
  166. break