CSV_to_RDF_ovi_toponimi.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 csv
  18. # Utilities to handle character encodings
  19. import unicodedata
  20. # Ordered Dicts
  21. from collections import OrderedDict
  22. import json
  23. import string
  24. import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/TOPONIMI/CSV/'
  25. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/TOPONIMI/RDF/'
  26. # Custom class to store URIs + related infos for the ontologies/repositories
  27. class RDFcoords:
  28. def __init__(self, uri, prefix, code = None):
  29. self.uri = uri
  30. self.prefix = prefix
  31. self.code = code
  32. # Repositories
  33. aspoCoords = RDFcoords('<http://datini.archiviodistato.prato.it/la-ricerca/scheda/>', 'dat:')
  34. placeCoords = RDFcoords('<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/>', 'pl:')
  35. oviCoords = RDFcoords('<http://www.ovi.cnr.it/>', 'ovi:')
  36. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  37. tgnCoords = RDFcoords('<http://vocab.getty.edu/tgn/>', 'tgn:')
  38. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  39. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  40. devCoords = RDFcoords('<http://dev.restore.ovi.cnr.it/vocabularies/places/>', 'dev:')
  41. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  42. # Basic functions for triples / shortened triples in TTL format
  43. def triple(subject, predicate, object1):
  44. line = subject + ' ' + predicate + ' ' + object1
  45. return line
  46. def doublet(predicate, object1):
  47. line = ' ' + predicate + ' ' + object1
  48. return line
  49. def singlet(object1):
  50. line = ' ' + object1
  51. return line
  52. # Line endings in TTL format
  53. continueLine1 = ' ;\n'
  54. continueLine2 = ' ,\n'
  55. closeLine = ' .\n'
  56. def writeTTLHeader(output):
  57. output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)
  58. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  59. output.write('@prefix ' + oviCoords.prefix + ' ' + oviCoords.uri + closeLine)
  60. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  61. output.write('@prefix ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
  62. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  63. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  64. output.write('@prefix ' + devCoords.prefix + ' ' + devCoords.uri + closeLine)
  65. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  66. output.write('\n')
  67. filePrefix = 'toponimi_'
  68. fileType = 'OVI_ASPO'
  69. max_entries = 1000000000
  70. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  71. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  72. reader = csv.DictReader(csv_file)
  73. writeTTLHeader(output)
  74. first = True
  75. ii = 0
  76. for row in reader:
  77. # The index ii is used to process a limited number of entries for testing purposes
  78. ii = ii + 1
  79. if row['ID ASPO'] != "":
  80. #placeHolders
  81. top = row['toponimo'].replace(" ", "_")
  82. toponimo = top.translate(str.maketrans('', '', string.punctuation))
  83. E73placeHolder = '<http://datini.archiviodistato.prato.it/la-ricerca/scheda/' + row['ID ASPO'] + '/E73_OVI>'
  84. topPlaceHolder = oviCoords.prefix + toponimo
  85. placePlaceHolder = devCoords.prefix + row['ID RESTORE']
  86. line = triple(E73placeHolder,
  87. cidocCoords.prefix + 'P67_refers_to',
  88. topPlaceHolder) + closeLine
  89. output.write(line)
  90. line = triple(topPlaceHolder,
  91. nsCoords.prefix + 'type',
  92. cidocCoords.prefix + 'E41_Appellation') + closeLine
  93. output.write(line)
  94. label = string.capwords(row['toponimo'])
  95. line = triple(topPlaceHolder,
  96. schemaCoords.prefix + 'label',
  97. '\"' + label + '\"') + closeLine
  98. output.write(line)
  99. line = triple(topPlaceHolder,
  100. cidocCoords.prefix + 'P2_has_type',
  101. '\"Toponimo\"') + closeLine
  102. output.write(line)
  103. line = triple(placePlaceHolder,
  104. cidocCoords.prefix + 'P1_is_identified_by',
  105. topPlaceHolder) + closeLine
  106. output.write(line)
  107. output.write('\n')
  108. #
  109. #
  110. # Limit number of entries processed (if desired)
  111. if (ii > max_entries):
  112. break