CSV_to_RDF_OA_IMG.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. hasNoteCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P3_has_note>', 'no:')
  46. # Basic functions for triples / shortened triples in TTL format
  47. def triple(subject, predicate, object1):
  48. line = subject + ' ' + predicate + ' ' + object1
  49. return line
  50. def doublet(predicate, object1):
  51. line = ' ' + predicate + ' ' + object1
  52. return line
  53. def singlet(object1):
  54. line = ' ' + object1
  55. return line
  56. # Line endings in TTL format
  57. continueLine1 = ' ;\n'
  58. continueLine2 = ' ,\n'
  59. closeLine = ' .\n'
  60. def writeTTLHeader(output):
  61. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  62. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  63. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  64. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  65. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  66. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  67. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  68. output.write('@prefix ' + documentsCoords.prefix + ' ' + documentsCoords.uri + closeLine)
  69. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  70. output.write('@prefix ' + hasNoteCoords.prefix + ' ' + hasNoteCoords.uri + closeLine)
  71. output.write('\n')
  72. filePrefix = 'AR20IMR'
  73. fileType = '_Ospedale'
  74. max_entries = 1000000000
  75. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  76. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  77. reader = csv.DictReader(csv_file)
  78. writeTTLHeader(output)
  79. first = True
  80. ii = 0
  81. for row in reader:
  82. # The index ii is used to process a limited number of entries for testing purposes
  83. ii = ii + 1
  84. # columnName = list(row)
  85. url = row['URL']
  86. # placeHolders
  87. if row['FTA0']!= '':
  88. datplaceHolder = museoCoords.prefix + url
  89. id = row['FTA0'].replace(".jpeg","")
  90. e36placeHolder = museoCoords.prefix + url + "_" + id
  91. e36e42placeHolder = museoCoords.prefix + url + "_" + id + "_E36_E42"
  92. line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', datplaceHolder) + closeLine
  93. output.write(line)
  94. line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
  95. output.write(line)
  96. line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Tavola\"') + closeLine
  97. output.write(line)
  98. line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Tavola\"') + closeLine
  99. output.write(line)
  100. line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine
  101. output.write(line)
  102. line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
  103. output.write(line)
  104. line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['FTA0']+ '\"') + closeLine
  105. output.write(line)
  106. if row['NOTE']!= '':
  107. line = triple(e36placeHolder, hasNoteCoords.prefix, '\"'+ row['NOTE']+'"') + closeLine
  108. output.write(line)
  109. output.write('\n')
  110. #
  111. #
  112. # Limit number of entries processed (if desired)
  113. if (ii > max_entries):
  114. break