CSV_to_RDF_mpp_MaterialAndTechnique.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. import json
  8. # OPZIONAL IMPORTS
  9. # For timestamping/simple speed tests
  10. from datetime import datetime
  11. # Random number generator
  12. from random import *
  13. # System & command line utilities
  14. import sys
  15. # Json for the dictionary
  16. import json
  17. import_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/Datini/mod/'
  18. export_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/Datini/mod/E57_'
  19. # Custom class to store URIs + related infos for the ontologies/repositories
  20. class RDFcoords:
  21. def __init__(self, uri, prefix, code = None):
  22. self.uri = uri
  23. self.prefix = prefix
  24. self.code = code
  25. # Repositories
  26. museoCoords = RDFcoords('<http://palazzopretorio.comune.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  27. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  28. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  29. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  30. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  31. # Basic functions for triples / shortened triples in TTL format
  32. def triple(subject, predicate, object1):
  33. line = subject + ' ' + predicate + ' ' + object1
  34. return line
  35. def doublet(predicate, object1):
  36. line = ' ' + predicate + ' ' + object1
  37. return line
  38. def singlet(object1):
  39. line = ' ' + object1
  40. return line
  41. # Line endings in TTL format
  42. continueLine1 = ' ;\n'
  43. continueLine2 = ' ,\n'
  44. closeLine = ' .\n'
  45. def writeTTLHeader(output):
  46. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  47. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  48. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  49. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  50. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  51. output.write('\n')
  52. filePrefix = 'SR20OA_'
  53. fileType = 'Datini'
  54. max_entries = 1000000000
  55. def get_elem(mtc):
  56. mtc_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/AAT_MTC.csv', newline="")
  57. reader = csv.DictReader(mtc_file)
  58. for row in reader:
  59. if row['MTC'] == mtc:
  60. return [row['AAT'], row['Type']]
  61. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  62. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  63. reader = csv.DictReader(csv_file)
  64. writeTTLHeader(output)
  65. first = True
  66. ii = 0
  67. for row in reader:
  68. # The index ii is used to process a limited number of entries for testing purposes
  69. ii = ii + 1
  70. url = row['URL']
  71. #placeHolders
  72. datplaceHolder = museoCoords.prefix + url
  73. e12placeHolder = museoCoords.prefix + url + '_E12'
  74. if row['MTC'] != '':
  75. mtcs = []
  76. if '/' in row['MTC']:
  77. mtcs = row['MTC'].split('/')
  78. else:
  79. mtcs.append(row['MTC'])
  80. for tc in mtcs:
  81. mtc = tc.lstrip()
  82. el = get_elem(mtc)
  83. if el[1] == 'MTC/M':
  84. line = triple(datplaceHolder,
  85. cidocCoords.prefix + 'P45_consists_of',
  86. aatCoords.prefix + el[0]) + closeLine
  87. output.write(line)
  88. line = triple(aatCoords.prefix + el[0],
  89. nsCoords.prefix + 'type',
  90. cidocCoords.prefix + 'E57_Material') + closeLine
  91. output.write(line)
  92. line = triple(aatCoords.prefix + el[0],
  93. schemaCoords.prefix + 'label',
  94. '\"' + mtc + '\"') + closeLine
  95. output.write(line)
  96. else: #E12 Production - P32 used technique - E55 Type
  97. line = triple(e12placeHolder,
  98. cidocCoords.prefix + 'P32_used_general_technique',
  99. aatCoords.prefix + el[0]) + closeLine
  100. output.write(line)
  101. line = triple(aatCoords.prefix + el[0],
  102. nsCoords.prefix + 'type',
  103. cidocCoords.prefix + 'E55_Type') + closeLine
  104. output.write(line)
  105. line = triple(aatCoords.prefix + el[0],
  106. schemaCoords.prefix + 'label',
  107. '\"' + mtc + '\"') + 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