CSV_to_RDF_VIAF_ULAN.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/TEAMOVI/Parser/DATA/MPP/CSV/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/RDF/'
  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('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  29. autCoords = RDFcoords('<https://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. foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
  37. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  38. # Basic functions for triples / shortened triples in TTL format
  39. def triple(subject, predicate, object1):
  40. line = subject + ' ' + predicate + ' ' + object1
  41. return line
  42. def doublet(predicate, object1):
  43. line = ' ' + predicate + ' ' + object1
  44. return line
  45. def singlet(object1):
  46. line = ' ' + object1
  47. return line
  48. # Line endings in TTL format
  49. continueLine1 = ' ;\n'
  50. continueLine2 = ' ,\n'
  51. closeLine = ' .\n'
  52. def writeTTLHeader(output):
  53. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  54. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  55. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  56. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  57. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  58. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  59. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  60. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  61. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  62. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  63. output.write('\n')
  64. filePrefix = 'OA'
  65. fileType = '_catalogo'
  66. max_entries = 1000000000
  67. def get_aut_url(code):
  68. aut_file = open(import_dir + 'AR20AUT_' + fileType + '.csv', newline="")
  69. reader = csv.DictReader(aut_file)
  70. for row in reader:
  71. auth = int(row['AUTH'])
  72. cod = int(code)
  73. role = ''
  74. if row['AUTQ'] != '':
  75. role = row['AUTQ']
  76. else:
  77. role = ''
  78. if auth == cod:
  79. return [row['URL'], role]
  80. def get_role(role):
  81. role_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_RUOLI.csv', newline="")
  82. reader = csv.DictReader(role_file)
  83. for row in reader:
  84. if row['Label'] == role:
  85. return row['AAT']
  86. def get_elem(mtc):
  87. mtc_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_MTC.csv', newline="")
  88. reader = csv.DictReader(mtc_file)
  89. for row in reader:
  90. if row['MTC'] == mtc:
  91. return [row['AAT'], row['Type']]
  92. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  93. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  94. reader = csv.DictReader(csv_file)
  95. writeTTLHeader(output)
  96. first = True
  97. ii = 0
  98. for row in reader:
  99. # The index ii is used to process a limited number of entries for testing purposes
  100. ii = ii + 1
  101. # placeHolders
  102. urlplaceHolder = '<' + row['Url OPERA'] + '>'
  103. e36placeHolder = '<' + row['Url OPERA'] + '_E36>'
  104. e36e42placeHolder = '<' + row['Url OPERA'] + '_E36_E42>'
  105. e36placeHolder2 = '<' + row['Url OPERA'] + '_E36_2>'
  106. e36e42placeHolder2 = '<' + row['Url OPERA'] + '_E36_2_E42>'
  107. e36placeHolder3 = '<' + row['Url OPERA'] + '_E36_3>'
  108. e36e42placeHolder3 = '<' + row['Url OPERA'] + '_E36_3_E42>'
  109. e31placeHolder = '<' + row['Url OPERA'] + '_E31>'
  110. e3142placeHolder = '<' + row['Url OPERA'] + '_E31_E42>'
  111. e31e36rplaceHolder = '<' + row['Url OPERA'] + '_E31_E36_r>'
  112. e31e36re42placeHolder = '<' + row['Url OPERA'] + '_E31_E36_r_E42>'
  113. e31e36vplaceHolder = '<' + row['Url OPERA'] + '_E31_E36_v>'
  114. e31e36ve42placeHolder = '<' + row['Url OPERA'] + '_E31_E36_v_E42>'
  115. e31AplaceHolder = '<' + row['LINK ARCHIVE.org'] + '>'
  116. e31Ae42placeHolder = '<' + row['LINK ARCHIVE.org'] + '_E42>'
  117. e13placeHolder = '<' + row['Url OPERA'] + '_E13_FA>'
  118. e31Ae55placeHolder = '<' + row['AAT (Attribution)'] + '_E31_E55>'
  119. e12placeHolder = '<' + row['Url OPERA'] + '_E12>'
  120. e12FplaceHolder = '<' + row['Url OPERA'] + '_E12F>'
  121. e21ULANplaceHolder = '<'+row['ULAN']+'>'
  122. if (row['LINK SCHEDA AUT']!= ''):
  123. e21AUTplaceHolder = '<'+row['LINK SCHEDA AUT']+'>'
  124. if (row['ULAN']!= ''):
  125. line = triple(e21AUTplaceHolder, owlCoords.prefix + 'sameAs', e21ULANplaceHolder) + closeLine
  126. output.write(line)
  127. if (row['VIAF']!= ''):
  128. e21VIAFplaceHolder = '<'+row['VIAF']+'>'
  129. line = triple(e21AUTplaceHolder, owlCoords.prefix + 'sameAs', e21VIAFplaceHolder) + closeLine
  130. output.write(line)
  131. output.write('\n')
  132. #
  133. #
  134. # Limit number of entries processed (if desired)
  135. if (ii > max_entries):
  136. break