CSV_to_RDF_Luoghi.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_ovi/prove/toponimi/'
  18. export_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_ovi/prove/toponimi/'
  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. aspoCoords = RDFcoords('<http://datini.archiviodistato.prato.it/la-ricerca/scheda/>', 'dat:')
  27. placeCoords = RDFcoords('<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/>', 'pl:')
  28. oviCoords = RDFcoords('<http://www.ovi.cnr.it/>', 'ovi:')
  29. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  30. tgnCoords = RDFcoords('<http://vocab.getty.edu/tgn/>', 'tgn:')
  31. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  32. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  33. repettiCoords = RDFcoords('<http://193.205.4.99/repetti/tester.php?idx=>', 'rpt:')
  34. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  35. devCoords = RDFcoords('<http://dev.restore.ovi.cnr.it/vocabularies/places/>', 'dev:')
  36. # Basic functions for triples / shortened triples in TTL format
  37. def triple(subject, predicate, object1):
  38. line = subject + ' ' + predicate + ' ' + object1
  39. return line
  40. def doublet(predicate, object1):
  41. line = ' ' + predicate + ' ' + object1
  42. return line
  43. def singlet(object1):
  44. line = ' ' + object1
  45. return line
  46. # Line endings in TTL format
  47. continueLine1 = ' ;\n'
  48. continueLine2 = ' ,\n'
  49. closeLine = ' .\n'
  50. def writeTTLHeader(output):
  51. output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)
  52. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  53. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  54. output.write('@prefix ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
  55. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  56. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  57. output.write('@prefix ' + repettiCoords.prefix + ' ' + repettiCoords.uri + closeLine)
  58. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  59. output.write('@prefix ' + devCoords.prefix + ' ' + devCoords.uri + closeLine)
  60. output.write('\n')
  61. file = "Luoghi"
  62. max_entries = 1000000000
  63. with open(import_dir + file + '.csv', newline="") as csv_file, open(
  64. export_dir + file + '.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. #placeHolders
  73. devPlaceHolder = devCoords.prefix + row['id']
  74. placePlaceHolder = tgnCoords.prefix + row['tgn']
  75. line = triple(devPlaceHolder,
  76. nsCoords.prefix + 'type',
  77. cidocCoords.prefix + 'E53_Place') + closeLine
  78. output.write(line)
  79. line = triple(devPlaceHolder,
  80. schemaCoords.prefix + 'label',
  81. '\"' + row['name_place'] + '\"') + closeLine
  82. output.write(line)
  83. line = triple(devPlaceHolder,
  84. cidocCoords.prefix + 'P1_is_identified_by',
  85. '\"' + row['id'] + '\"') + closeLine
  86. output.write(line)
  87. if row['coordinates'] != '':
  88. line = triple(devPlaceHolder,
  89. cidocCoords.prefix + "P168_place_is_defined_by",
  90. '\"' + row['coordinates'] + '\"') + closeLine
  91. output.write(line)
  92. line = triple(devPlaceHolder,
  93. owlCoords.prefix + 'sameAs',
  94. placePlaceHolder) + closeLine
  95. output.write(line)
  96. line = triple(placePlaceHolder,
  97. owlCoords.prefix + 'sameAs',
  98. devPlaceHolder) + closeLine
  99. output.write(line)
  100. if row['Repetti'] != '':
  101. line = triple(devPlaceHolder,
  102. owlCoords.prefix + 'sameAs',
  103. repettiCoords.prefix + row['Repetti']) + closeLine
  104. output.write(line)
  105. line = triple(repettiCoords.prefix + row['Repetti'],
  106. owlCoords.prefix + 'sameAs',
  107. devPlaceHolder) + 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