CSV_to_RDF_Datini_cataloghi.py 5.7 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 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/Google Drive/OVI-CNR/CSV/MPP/corretti/'
  20. export_dir = '/Users/federicaspinelli/Google Drive/OVI-CNR/RDF/MPP/'
  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. # Basic functions for triples / shortened triples in TTL format
  37. def triple(subject, predicate, object1):
  38. line = subject + ' ' + predicate + ' ' + object1
  39. return line
  40. def doublet(predicate, object1):
  41. line = ' ' + predicate + ' ' + object1
  42. return line
  43. def singlet(object1):
  44. line = ' ' + object1
  45. return line
  46. # Line endings in TTL format
  47. continueLine1 = ' ;\n'
  48. continueLine2 = ' ,\n'
  49. closeLine = ' .\n'
  50. def writeTTLHeader(output):
  51. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  52. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  53. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  54. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  55. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  56. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  57. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  58. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  59. output.write('\n')
  60. filePrefix = '00_SR20OA_'
  61. fileType = 'Datini'
  62. max_entries = 1000000000
  63. def get_aut_url(code):
  64. aut_file = open(import_dir + 'AR20AUT_' + fileType + '.csv', newline="")
  65. reader = csv.DictReader(aut_file)
  66. for row in reader:
  67. auth = int(row['AUTH'])
  68. cod = int(code)
  69. role = ''
  70. if row['AUTQ'] != '':
  71. role = row['AUTQ']
  72. else:
  73. role = ''
  74. if auth == cod:
  75. return [row['URL'], role]
  76. def get_role(role):
  77. role_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_RUOLI.csv', newline="")
  78. reader = csv.DictReader(role_file)
  79. for row in reader:
  80. if row['Label'] == role:
  81. return row['AAT']
  82. def get_elem(mtc):
  83. mtc_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_MTC.csv', newline="")
  84. reader = csv.DictReader(mtc_file)
  85. for row in reader:
  86. if row['MTC'] == mtc:
  87. return [row['AAT'], row['Type']]
  88. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  89. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  90. reader = csv.DictReader(csv_file)
  91. writeTTLHeader(output)
  92. first = True
  93. ii = 0
  94. for row in reader:
  95. # The index ii is used to process a limited number of entries for testing purposes
  96. ii = ii + 1
  97. sb = ''
  98. subj = ''
  99. pp = row['OGTD'] + ' (' + row['ACC'] + ') '
  100. if row['SGTI'] != '':
  101. sb = pp + row['SGTI']
  102. if row['LDCN'] != '':
  103. subj = sb + ' in ' + row['LDCN']
  104. else:
  105. subj = sb
  106. # Triplify the 'codice' -- should exist for every entry
  107. codice = ''
  108. if (row['NCTR'] != '' and row['NCTN'] != ''):
  109. codice = row['NCTR'] + row['NCTN']
  110. codiceP = ''
  111. if (row['AUTH'] != ''):
  112. codiceP = row['AUTH']
  113. place = ''
  114. if (row['PRVC'] != ''):
  115. place = row['PRVC']
  116. columnName = list(row)
  117. url = row['URL']
  118. # placeHolders
  119. datplaceHolder = museoCoords.prefix + url
  120. e1placeHolder = museoCoords.prefix + url + '_E1'
  121. e3placeHolder = museoCoords.prefix + url + 'E3'
  122. e10placeHolder = museoCoords.prefix + url + '_E10'
  123. e12placeHolder = museoCoords.prefix + url + '_E12'
  124. e13placeHolder = museoCoords.prefix + url + '_E13'
  125. e21placeHolder = museoCoords.prefix + url + '_InE21'
  126. e25placeHolder = museoCoords.prefix + url + '_E25'
  127. e34placeHolder = museoCoords.prefix + url + '_E34'
  128. e35placeHolder1 = museoCoords.prefix + url + '_E35'
  129. e42placeHolder = museoCoords.prefix + url + '_E42'
  130. e42CplaceHolder = museoCoords.prefix + url + '_E42_MPP'
  131. e65placeHolder = museoCoords.prefix + url + '_InE65'
  132. e73placeHolder = museoCoords.prefix + url + '_E73'
  133. e74placeHolder = museoCoords.prefix + url + '_E74'
  134. if (codice != ''):
  135. line = triple(datplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', '\"' + codice + '\"') + closeLine
  136. output.write(line)
  137. output.write('\n')
  138. #
  139. #
  140. # Limit number of entries processed (if desired)
  141. if (ii > max_entries):
  142. break