CSV_to_RDF_Luoghi_DEV.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 + '.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. line = triple(devPlaceHolder,
  76. nsCoords.prefix + 'type',
  77. cidocCoords.prefix + 'E53_Place') + closeLine
  78. output.write(line)
  79. label = string.capwords(row['TOPONIMO'])
  80. line = triple(devPlaceHolder, schemaCoords.prefix + 'label', '\"' + label + '\"') + closeLine
  81. output.write(line)
  82. line = triple(devPlaceHolder,
  83. cidocCoords.prefix + 'P1_is_identified_by',
  84. '\"' + row['ID RESTORE'] + '\"') + closeLine
  85. output.write(line)
  86. if row['LAT'] != '' and row['LONG'] != '':
  87. line = triple(devPlaceHolder,
  88. cidocCoords.prefix + "P168_place_is_defined_by",
  89. '\"' + row['LAT'] + ' , ' + row['LONG'] + '\"') + closeLine
  90. output.write(line)
  91. if row['TGN'] != '' and row['TGN'] != 'FALSE':
  92. tgnPlaceHolder = tgnCoords.prefix + row['TGN']
  93. line = triple(devPlaceHolder,
  94. owlCoords.prefix + 'sameAs',
  95. tgnPlaceHolder) + closeLine
  96. output.write(line)
  97. line = triple(tgnPlaceHolder,
  98. schemaCoords.prefix + 'label',
  99. '\"' + label + ' (TGN)\"') + closeLine
  100. output.write(line)
  101. line = triple(tgnPlaceHolder,
  102. cidocCoords.prefix + 'P1_is_identified_by',
  103. '\"' + row['TGN'] + '\"') + closeLine
  104. output.write(line)
  105. if row['REPETTI'] != '' and row['REPETTI'] != 'FALSE':
  106. repettiPlaceHolder = repettiCoords.prefix + row['REPETTI']
  107. line = triple(devPlaceHolder,
  108. owlCoords.prefix + 'sameAs',
  109. repettiPlaceHolder) + closeLine
  110. output.write(line)
  111. line = triple(repettiPlaceHolder,
  112. schemaCoords.prefix + 'label',
  113. '\"' + label + ' (REPETTI)\"') + closeLine
  114. output.write(line)
  115. line = triple(repettiPlaceHolder,
  116. cidocCoords.prefix + 'P1_is_identified_by',
  117. '\"' + row['REPETTI'] + '\"') + closeLine
  118. output.write(line)
  119. output.write('\n')
  120. #
  121. #
  122. # Limit number of entries processed (if desired)
  123. if (ii > max_entries):
  124. break