CSV_to_RDF_Luoghi_ASPO_toponimi_OVI.py 5.1 KB

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