CSV_to_RDF_Luoghi_OVI_NON_ESEGUIRE.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/CSV/'
  19. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/TOPONIMI/RDF/'
  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. identifiedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by>', 'ib:')
  38. # Basic functions for triples / shortened triples in TTL format
  39. def triple(subject, predicate, object1):
  40. line = subject + ' ' + predicate + ' ' + object1
  41. return line
  42. def doublet(predicate, object1):
  43. line = ' ' + predicate + ' ' + object1
  44. return line
  45. def singlet(object1):
  46. line = ' ' + object1
  47. return line
  48. # Line endings in TTL format
  49. continueLine1 = ' ;\n'
  50. continueLine2 = ' ,\n'
  51. closeLine = ' .\n'
  52. def writeTTLHeader(output):
  53. output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)
  54. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  55. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  56. output.write('@prefix ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
  57. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  58. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  59. output.write('@prefix ' + repettiCoords.prefix + ' ' + repettiCoords.uri + closeLine)
  60. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  61. output.write('@prefix ' + devCoords.prefix + ' ' + devCoords.uri + closeLine)
  62. output.write('@prefix ' + oviCoords.prefix + ' ' + oviCoords.uri + closeLine)
  63. output.write('@prefix ' + identifiedByCoords.prefix + ' ' + identifiedByCoords.uri + closeLine)
  64. output.write('\n')
  65. file = "toponimi_OVI"
  66. max_entries = 1000000000
  67. with open(import_dir + file + '.csv', newline="") as csv_file, open(
  68. export_dir + file + '.ttl', 'w') as output:
  69. reader = csv.DictReader(csv_file)
  70. writeTTLHeader(output)
  71. first = True
  72. ii = 0
  73. for row in reader:
  74. # The index ii is used to process a limited number of entries for testing purposes
  75. ii = ii + 1
  76. #placeHolders
  77. devPlaceHolder = devCoords.prefix + row['ID RESTORE']
  78. oviPlaceHolder = oviCoords.prefix + row['toponimo'].replace(" ","").replace("(","").replace(")","").replace("[","").replace("]","").replace("\'","").replace("?","")
  79. '''
  80. line = triple(devPlaceHolder,
  81. cidocCoords.prefix + 'P1_is_identified_by',
  82. oviPlaceHolder) + closeLine
  83. output.write(line)'''
  84. #label = string.capwords(row['toponimo'])
  85. #line = triple(oviPlaceHolder, schemaCoords.prefix + 'label', '\"' + label + '\"') + closeLine
  86. #output.write(line)
  87. #line = triple(oviPlaceHolder,
  88. # cidocCoords.prefix + 'P1_is_identified_by',
  89. # '\"' + row['ID RESTORE'] + '\"') + closeLine
  90. #output.write(line)
  91. output.write('\n')
  92. #
  93. #
  94. # Limit number of entries processed (if desired)
  95. if (ii > max_entries):
  96. break