CSV_to_RDF_Provincia_SKOS.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. repettiCoords = RDFcoords('<http://193.205.4.99/repetti/tester.php?idx=>', 'rpt:')
  33. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  34. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  35. devCoords = RDFcoords('<http://dev.restore.ovi.cnr.it/vocabularies/places/>', 'dev:')
  36. # SKOS
  37. skosCoords = RDFcoords('<http://www.w3.org/2008/05/skos#>', 'skos:')
  38. # prefLabelCoords = RDFcoords('<http://www.w3.org/2008/05/skos#prefLabel>', 'pref:')
  39. # narrowerCoords = RDFcoords('<http://www.w3.org/2008/05/skos#narrower>', 'narr:')
  40. # broaderCoords = RDFcoords('<http://www.w3.org/2008/05/skos#broader>', 'bro:')
  41. # Basic functions for triples / shortened triples in TTL format
  42. def triple(subject, predicate, object1):
  43. line = subject + ' ' + predicate + ' ' + object1
  44. return line
  45. def doublet(predicate, object1):
  46. line = ' ' + predicate + ' ' + object1
  47. return line
  48. def singlet(object1):
  49. line = ' ' + object1
  50. return line
  51. # Line endings in TTL format
  52. continueLine1 = ' ;\n'
  53. continueLine2 = ' ,\n'
  54. closeLine = ' .\n'
  55. def writeTTLHeader(output):
  56. output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)
  57. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  58. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  59. output.write('@prefix ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
  60. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  61. output.write('@prefix ' + repettiCoords.prefix + ' ' + repettiCoords.uri + closeLine)
  62. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  63. output.write('@prefix ' + devCoords.prefix + ' ' + devCoords.uri + closeLine)
  64. output.write('@prefix ' + skosCoords.prefix + ' ' + skosCoords.uri + closeLine)
  65. output.write('\n')
  66. file = "toponimi_DEV"
  67. max_entries = 1000000000
  68. with open(import_dir + file + '.csv', newline="") as csv_file, open(
  69. export_dir + file + '.ttl', 'w') as output:
  70. reader = csv.DictReader(csv_file)
  71. writeTTLHeader(output)
  72. first = True
  73. ii = 0
  74. for row in reader:
  75. # The index ii is used to process a limited number of entries for testing purposes
  76. ii = ii + 1
  77. tgnPlaceHolder = tgnCoords.prefix + row['TGN']
  78. line = triple(tgnPlaceHolder,
  79. schemaCoords.prefix + 'type',
  80. 'Province') + closeLine
  81. output.write(line)
  82. line = triple(tgnPlaceHolder,
  83. skosCoords.prefix + 'narrower',
  84. 'Province') + closeLine
  85. output.write(line)
  86. line = triple(tgnPlaceHolder,
  87. skosCoords.prefix + 'broader',
  88. 'Province') + closeLine
  89. output.write(line)
  90. #placeHolders
  91. # devPlaceHolder = devCoords.prefix + row['ID RESTORE']
  92. # line = triple(devPlaceHolder,
  93. # nsCoords.prefix + 'type',
  94. # cidocCoords.prefix + 'E53_Place') + closeLine
  95. # output.write(line)
  96. # label = string.capwords(row['TOPONIMO'])
  97. # line = triple(devPlaceHolder, schemaCoords.prefix + 'label', '\"' + label + '\"') + closeLine
  98. # output.write(line)
  99. # line = triple(devPlaceHolder,
  100. # cidocCoords.prefix + 'P1_is_identified_by',
  101. # '\"' + row['ID RESTORE'] + '\"') + closeLine
  102. # output.write(line)
  103. # if row['LAT'] != '' and row['LONG'] != '':
  104. # line = triple(devPlaceHolder,
  105. # cidocCoords.prefix + "P168_place_is_defined_by",
  106. # '\"' + row['LAT'] + ' , ' + row['LONG'] + '\"') + closeLine
  107. # output.write(line)
  108. # if row['TGN'] != '' and row['TGN'] != 'FALSE':
  109. # tgnPlaceHolder = tgnCoords.prefix + row['TGN']
  110. # line = triple(devPlaceHolder,
  111. # owlCoords.prefix + 'sameAs',
  112. # tgnPlaceHolder) + closeLine
  113. # output.write(line)
  114. # line = triple(tgnPlaceHolder,
  115. # schemaCoords.prefix + 'label',
  116. # '\"' + label + ' (TGN)\"') + closeLine
  117. # output.write(line)
  118. # line = triple(tgnPlaceHolder,
  119. # cidocCoords.prefix + 'P1_is_identified_by',
  120. # '\"' + row['TGN'] + '\"') + closeLine
  121. # output.write(line)
  122. # if row['REPETTI'] != '' and row['REPETTI'] != 'FALSE':
  123. # repettiPlaceHolder = repettiCoords.prefix + row['REPETTI']
  124. # line = triple(devPlaceHolder,
  125. # owlCoords.prefix + 'sameAs',
  126. # repettiPlaceHolder) + closeLine
  127. # output.write(line)
  128. # line = triple(repettiPlaceHolder,
  129. # schemaCoords.prefix + 'label',
  130. # '\"' + label + ' (REPETTI)\"') + closeLine
  131. # output.write(line)
  132. # line = triple(repettiPlaceHolder,
  133. # cidocCoords.prefix + 'P1_is_identified_by',
  134. # '\"' + row['REPETTI'] + '\"') + closeLine
  135. # output.write(line)
  136. output.write('\n')
  137. #
  138. #
  139. # Limit number of entries processed (if desired)
  140. if (ii > max_entries):
  141. break