CSV_to_RDF_Ruoli.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/Ospedale/mod/'
  18. export_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/Carica/PC14/'
  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. autCoords = RDFcoords('<http://palazzopretorio.comune.prato.it/it/opere/autori/>', 'aut:')
  28. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  29. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  30. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  31. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  32. # Basic functions for triples / shortened triples in TTL format
  33. def triple(subject, predicate, object1):
  34. line = subject + ' ' + predicate + ' ' + object1
  35. return line
  36. def doublet(predicate, object1):
  37. line = ' ' + predicate + ' ' + object1
  38. return line
  39. def singlet(object1):
  40. line = ' ' + object1
  41. return line
  42. # Line endings in TTL format
  43. continueLine1 = ' ;\n'
  44. continueLine2 = ' ,\n'
  45. closeLine = ' .\n'
  46. def writeTTLHeader(output):
  47. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  48. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  49. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  50. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  51. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  52. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  53. output.write('\n')
  54. filePrefix = 'AR20AUT_'
  55. fileType = 'Ospedale'
  56. max_entries = 1000000000
  57. def get_role(role):
  58. role_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/AAT_RUOLI.csv', newline="")
  59. reader = csv.DictReader(role_file)
  60. for row in reader:
  61. if row['Label'] == role:
  62. return row['AAT']
  63. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  64. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  65. reader = csv.DictReader(csv_file)
  66. writeTTLHeader(output)
  67. first = True
  68. ii = 0
  69. for row in reader:
  70. # The index ii is used to process a limited number of entries for testing purposes
  71. ii = ii + 1
  72. url = row['URL']
  73. # placeHolders
  74. datplaceHolder = museoCoords.prefix + url
  75. # E12 - PC14 - E21
  76. if row['AUTH'] != '':
  77. aut_role = ''
  78. if row['AUTQ'] != '':
  79. aut_role = row['AUTQ']
  80. else:
  81. aut_role = ''
  82. ll = row['AUTN'] + '_' + aut_role
  83. lab = ll.replace(' ', '')
  84. label = lab.replace(',', '')
  85. AuthorPlaceholder = autCoords.prefix + row['URL']
  86. line = triple(museoCoords.prefix + '_' + label,
  87. nsCoords.prefix + 'type',
  88. cidocCoords.prefix + 'PC14_carried_out_by') + closeLine
  89. output.write(line)
  90. line = triple(museoCoords.prefix + '_' + label,
  91. schemaCoords.prefix + 'label',
  92. '\"' + row['AUTN'] + ' nel ruolo di ' + aut_role + '\"') + closeLine
  93. output.write(line)
  94. line = triple(museoCoords.prefix + '_' + label,
  95. cidocCoords.prefix + 'P02_has_range',
  96. AuthorPlaceholder) + closeLine
  97. output.write(line)
  98. if aut_role != '':
  99. role = get_role(aut_role)
  100. line = triple(museoCoords.prefix + '_' + label,
  101. cidocCoords.prefix + 'P14.1_in_the_role_of',
  102. aatCoords.prefix + role) + closeLine
  103. output.write(line)
  104. line = triple(aatCoords.prefix + role,
  105. nsCoords.prefix + 'type',
  106. cidocCoords.prefix + 'E55_Type') + closeLine
  107. output.write(line)
  108. line = triple(aatCoords.prefix + role,
  109. schemaCoords.prefix + 'label',
  110. '\"' + aut_role + '\"') + closeLine
  111. output.write(line)
  112. output.write('\n')
  113. #
  114. #
  115. # Limit number of entries processed (if desired)
  116. if (ii > max_entries):
  117. break