CSV_to_RDF_Luoghi_ASPO_GETTATELLI.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import string
  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_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/TOPONIMI/'
  19. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/TOPONIMI/'
  20. # Custom class to store URIs + related infos for the ontologies/repositories
  21. class RDFcoords:
  22. def __init__(self, uri, prefix, code = None):
  23. self.uri = uri
  24. self.prefix = prefix
  25. self.code = code
  26. # Repositories
  27. aspoCoords = RDFcoords('<http://datini.archiviodistato.prato.it/la-ricerca/scheda/>', 'dat:')
  28. placeCoords = RDFcoords('<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/>', 'pl:')
  29. oviCoords = RDFcoords('<http://www.ovi.cnr.it/>', 'ovi:')
  30. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  31. tgnCoords = RDFcoords('<http://vocab.getty.edu/tgn/>', 'tgn:')
  32. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  33. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  34. repettiCoords = RDFcoords('<http://193.205.4.99/repetti/tester.php?idx=>', 'rpt:')
  35. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  36. devCoords = RDFcoords('<http://dev.restore.ovi.cnr.it/vocabularies/places/>', 'dev:')
  37. # Basic functions for triples / shortened triples in TTL format
  38. def triple(subject, predicate, object1):
  39. line = subject + ' ' + predicate + ' ' + object1
  40. return line
  41. def doublet(predicate, object1):
  42. line = ' ' + predicate + ' ' + object1
  43. return line
  44. def singlet(object1):
  45. line = ' ' + object1
  46. return line
  47. # Line endings in TTL format
  48. continueLine1 = ' ;\n'
  49. continueLine2 = ' ,\n'
  50. closeLine = ' .\n'
  51. def writeTTLHeader(output):
  52. output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)
  53. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  54. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  55. output.write('@prefix ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
  56. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  57. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  58. output.write('@prefix ' + repettiCoords.prefix + ' ' + repettiCoords.uri + closeLine)
  59. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  60. output.write('@prefix ' + devCoords.prefix + ' ' + devCoords.uri + closeLine)
  61. output.write('\n')
  62. file = "toponimi_DEV"
  63. max_entries = 1000000000
  64. with open(import_dir + file + '.csv', newline="") as csv_file, open(
  65. export_dir + file + '_GETT.ttl', 'w') as output:
  66. reader = csv.DictReader(csv_file)
  67. writeTTLHeader(output)
  68. first = True
  69. ii = 0
  70. for row in reader:
  71. # The index ii is used to process a limited number of entries for testing purposes
  72. ii = ii + 1
  73. #placeHolders
  74. devPlaceHolder = devCoords.prefix + row['ID RESTORE']
  75. topPlaceholder = row['TOPONIMO'].replace(' ','').replace('\'','').replace(',','').replace('ô','').replace('’','').replace('(','').replace(')','').replace('[','').replace('?','').replace(']','').lower()
  76. gettPlaceHolder = placeCoords.prefix + 'IT-ASPO-GEO0001-' + topPlaceholder
  77. if row['ID RESTORE'] != '':
  78. line = triple(devPlaceHolder,
  79. owlCoords.prefix + 'sameAs',
  80. gettPlaceHolder) + closeLine
  81. output.write(line)
  82. label = string.capwords(row['TOPONIMO'])
  83. line = triple(gettPlaceHolder, schemaCoords.prefix + 'label', '\"' + label + '\"') + closeLine
  84. output.write(line)
  85. output.write('\n')
  86. #
  87. #
  88. # Limit number of entries processed (if desired)
  89. if (ii > max_entries):
  90. break