CSV_to_RDF_Ruoli.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/corretti/'
  18. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/RDF/'
  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('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  27. autCoords = RDFcoords('<https://palazzopretorio.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/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/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 + '_roles.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. ll = row['AUTN'] + '_' + aut_role
  81. lab = ll.replace(' ', '')
  82. label = lab.replace(',', '')
  83. line = triple(museoCoords.prefix + '_' + label,
  84. schemaCoords.prefix + 'label',
  85. '\"' + row['AUTN'] + ' nel ruolo di artista' + '\"') + closeLine
  86. output.write(line)
  87. else:
  88. aut_role = 'artista'
  89. ll = row['AUTN'] + '_' + 'artista'
  90. lab = ll.replace(' ', '')
  91. label = lab.replace(',', '')
  92. line = triple(museoCoords.prefix + '_' + label,
  93. schemaCoords.prefix + 'label',
  94. '\"' + row['AUTN'] + ' nel ruolo di artista' + '\"') + closeLine
  95. output.write(line)
  96. AuthorPlaceholder = autCoords.prefix + row['URL']
  97. line = triple(museoCoords.prefix + '_' + label,
  98. nsCoords.prefix + 'type',
  99. cidocCoords.prefix + 'PC14_carried_out_by') + closeLine
  100. output.write(line)
  101. line = triple(museoCoords.prefix + '_' + label,
  102. cidocCoords.prefix + 'P02_has_range',
  103. AuthorPlaceholder) + closeLine
  104. output.write(line)
  105. if aut_role != '' and aut_role != 'artista':
  106. role = get_role(aut_role)
  107. line = triple(museoCoords.prefix + '_' + label,
  108. cidocCoords.prefix + 'P14.1_in_the_role_of',
  109. aatCoords.prefix + role) + closeLine
  110. output.write(line)
  111. line = triple(aatCoords.prefix + role,
  112. nsCoords.prefix + 'type',
  113. cidocCoords.prefix + 'E55_Type') + closeLine
  114. output.write(line)
  115. line = triple(aatCoords.prefix + role,
  116. schemaCoords.prefix + 'label',
  117. '\"' + aut_role + '\"') + closeLine
  118. output.write(line)
  119. else:
  120. role = 'artista'
  121. line = triple(museoCoords.prefix + '_' + label,
  122. cidocCoords.prefix + 'P14.1_in_the_role_of',
  123. aatCoords.prefix + role) + closeLine
  124. output.write(line)
  125. line = triple(aatCoords.prefix + role,
  126. nsCoords.prefix + 'type',
  127. cidocCoords.prefix + 'E55_Type') + closeLine
  128. output.write(line)
  129. line = triple(aatCoords.prefix + role,
  130. schemaCoords.prefix + 'label',
  131. '\"' + aut_role + '\"') + closeLine
  132. output.write(line)
  133. output.write('\n')
  134. #
  135. #
  136. # Limit number of entries processed (if desired)
  137. if (ii > max_entries):
  138. break