CSV_to_RDF_generico_composta.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ## IMPORTS
  2. # Utilities to read/write csv files
  3. import csv, json
  4. # Custom class to store URIs + related infos for the ontologies/repositories
  5. class RDFcoords:
  6. def __init__(self, uri, prefix, code = None):
  7. self.uri = uri
  8. self.prefix = prefix
  9. self.code = code
  10. # Repositories
  11. museoCoords = RDFcoords('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  12. autCoords = RDFcoords('<https://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
  13. foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
  14. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  15. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  16. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  17. schemaCoords = RDFcoords('<http://www.schema.org/>', 'schema:')
  18. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  19. # Basic utilities to format triples / shortened triples in TTL format
  20. #
  21. # Format full triple
  22. def triple(subject, predicate, object1):
  23. line = subject + ' ' + predicate + ' ' + object1
  24. return line
  25. # Format entry in predicate list (no subject)
  26. def doublet(predicate, object1):
  27. line = ' ' + predicate + ' ' + object1
  28. return line
  29. # Format entry in object list (object only)
  30. def singlet(object1):
  31. line = ' ' + object1
  32. return line
  33. # Line endings
  34. continueLine1 = ' ;\n' # Before a predicate list, that is if the FOLLOWING triple has the same subject
  35. continueLine2 = ' ,\n' # Before an object list, that is if the FOLLOWING triple has the same subject and predicate
  36. closeLine = ' .\n' # To end a triple / a triples block
  37. def writeTTLHeader(output):
  38. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  39. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  40. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  41. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  42. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  43. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  44. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  45. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  46. output.write('\n')
  47. max_entries = None
  48. def parsefromfile(mapfilename, infile, outfilename):
  49. inputFile = infile.decode()
  50. csv_dicts = [{k: v for k, v in row.items()} for row in csv.DictReader(inputFile.splitlines(), skipinitialspace=True)]
  51. with open (mapfilename) as mapfile:
  52. json_dicts = json.load(mapfile)
  53. parse(json_dicts, csv_dicts, outfilename)
  54. def parse(json_dicts, csv_dicts, outfilename):
  55. with open(outfilename, 'w') as outputfile:
  56. writeTTLHeader(outputfile)
  57. first = True # In case something needs processing only once for the whole CSV input
  58. for ii, csvrow in enumerate(csv_dicts):
  59. # The index ii is mainly used to limit the number of entries to process, for testing purposes
  60. for jj, node in enumerate(json_dicts):
  61. csvvalue = csvrow[node["colonna"]]
  62. line = triple(node["uri"].replace('$VALORE_CSV$', csvvalue), nsCoords.prefix + 'type', node["tipo"]) + closeLine
  63. outputfile.write(line)
  64. if node["sottoelementodi"] != '':
  65. parent = next (filter(lambda el: el["identificatore"]==node["sottoelementodi"], json_dicts), None)
  66. if parent is not None:
  67. subject = parent["uri"].replace('$VALORE_CSV$', parent["colonna"])
  68. property = node["relazione"]
  69. object = node["uri"].replace('$VALORE_CSV$', csvvalue)
  70. line = triple(subject, property,
  71. object) + closeLine
  72. outputfile.write(line)
  73. outputfile.write('\n')
  74. #
  75. #
  76. # To limit number of entries processed (if desired for testing purposes)
  77. if (max_entries is not None and ii > max_entries):
  78. break