CSV_to_RDF_Luoghi_ASPO_SAMEAS_DEV.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/TOPONIMI/CSV/'
  18. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/TOPONIMI/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. 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 = "toponimi_ASPO"
  62. max_entries = 1000000000
  63. with open(import_dir + file + '.csv', newline="") as csv_file, open(
  64. export_dir + file + '_SAMEAS_DEV.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 RESTORE']
  74. aspoPlaceHolder = placeCoords.prefix + row['ID ASPO']
  75. line = triple(aspoPlaceHolder,
  76. owlCoords.prefix + 'sameAs',
  77. devPlaceHolder) + closeLine
  78. output.write(line)
  79. output.write('\n')
  80. #
  81. #
  82. # Limit number of entries processed (if desired)
  83. if (ii > max_entries):
  84. break