CSV_to_RDF_Edifici_Parrocchie_Micromicro.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. # OPZIONAL IMPORTS
  9. # For timestamping/simple speed tests
  10. from datetime import datetime
  11. # Random number generator
  12. from random import *
  13. # System & command line utilities
  14. import sys
  15. # Json for the dictionary
  16. import json
  17. import_dir = '/Users/leonardocanova/Library/CloudStorage/OneDrive-UniversityofPisa(1)/Documenti/Progetti università/OVI/Programmazione/ASPO/Luoghi/'
  18. export_dir = '/Users/leonardocanova/Library/CloudStorage/OneDrive-UniversityofPisa(1)/Documenti/Progetti università/OVI/Programmazione/ASPO/Luoghi/'
  19. # Custom class to store URIs + related infos for the ontologies/repositories
  20. class RDFcoords:
  21. def __init__(self, uri, prefix, code = None):
  22. self.uri = uri
  23. self.prefix = prefix
  24. self.code = code
  25. # Repositories
  26. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  27. tgnCoords = RDFcoords('<http://vocab.getty.edu/tgn/>', 'tgn:')
  28. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  29. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  30. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  31. devCoords = RDFcoords('<http://dev.restore.ovi.cnr.it/vocabularies/places/>', 'dev:')
  32. # Basic functions for triples / shortened triples in TTL format
  33. def triple(subject, predicate, object1):
  34. line = subject + ' ' + predicate + ' ' + object1
  35. return line
  36. def doublet(predicate, object1):
  37. line = ' ' + predicate + ' ' + object1
  38. return line
  39. def singlet(object1):
  40. line = ' ' + object1
  41. return line
  42. # Line endings in TTL format
  43. continueLine1 = ' ;\n'
  44. continueLine2 = ' ,\n'
  45. closeLine = ' .\n'
  46. def writeTTLHeader(output):
  47. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  48. output.write('@prefix ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
  49. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  50. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  51. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  52. output.write('@prefix ' + devCoords.prefix + ' ' + devCoords.uri + closeLine)
  53. output.write('\n')
  54. file = "luoghi_ASPO_tutti_ID"
  55. max_entries = 1000000000
  56. with open(import_dir + file + '.csv', newline="") as csv_file, open(
  57. export_dir + file + '.ttl', 'w') as output:
  58. reader = csv.DictReader(csv_file)
  59. writeTTLHeader(output)
  60. first = True
  61. ii = 0
  62. for row in reader:
  63. # The index ii is used to process a limited number of entries for testing purposes
  64. ii = ii + 1
  65. #placeHolders
  66. devPlaceHolder_provincia = devCoords.prefix + row['ID_PROVINCIA']
  67. devPlaceHolder_comune = devCoords.prefix + row['ID_RESTORE_comune']
  68. devPlaceHolder_microtoponimo = devCoords.prefix + row['ID_RESTORE_microtoponimo']
  69. devPlaceHolder_edificio = devCoords.prefix + row['ID_edificio']
  70. devPlaceHolder_parrocchia = devCoords.prefix + row['ID_parrocchia']
  71. devPlaceHolder_micromicro = devCoords.prefix + row['ID_micromicrotoponimo']
  72. if row['ID_edificio'] != "" and " ":
  73. line = triple(devPlaceHolder_edificio,
  74. nsCoords.prefix + 'type',
  75. cidocCoords.prefix + 'E53_Place') + closeLine
  76. output.write(line)
  77. line = triple(devPlaceHolder_edificio,
  78. schemaCoords.prefix + 'label',
  79. '\"' + row['EVENTO edificio upper'] + '\"') + closeLine
  80. output.write(line)
  81. line = triple(devPlaceHolder_edificio,
  82. cidocCoords.prefix + 'P1_is_identified_by',
  83. '\"' + row['ID_edificio'] + '\"') + closeLine
  84. output.write(line)
  85. if row['ID_RESTORE_microtoponimo'] != "" and " ":
  86. line = triple(devPlaceHolder_edificio,
  87. cidocCoords.prefix + 'P89_falls_within',
  88. devPlaceHolder_microtoponimo) + closeLine
  89. output.write(line)
  90. elif row ['ID_RESTORE_comune'] != "" and " ":
  91. line = triple(devPlaceHolder_edificio,
  92. cidocCoords.prefix + 'P89_falls_within',
  93. devPlaceHolder_comune) + closeLine
  94. output.write(line)
  95. elif row ['ID_PROVINCIA'] != "" and " ":
  96. line = triple(devPlaceHolder_edificio,
  97. cidocCoords.prefix + 'P89_falls_within',
  98. devPlaceHolder_provincia) + closeLine
  99. output.write(line)
  100. if row['ID_parrocchia'] != "" and " ":
  101. line = triple(devPlaceHolder_parrocchia,
  102. nsCoords.prefix + 'type',
  103. cidocCoords.prefix + 'E53_Place') + closeLine
  104. output.write(line)
  105. line = triple(devPlaceHolder_parrocchia,
  106. schemaCoords.prefix + 'label',
  107. '\"' + row['EVENTO PARROCCHIA upper'] + '\"') + closeLine
  108. output.write(line)
  109. line = triple(devPlaceHolder_parrocchia,
  110. cidocCoords.prefix + 'P1_is_identified_by',
  111. '\"' + row['ID_parrocchia'] + '\"') + closeLine
  112. output.write(line)
  113. if row ['ID_edificio'] != "" and " ":
  114. line = triple(devPlaceHolder_parrocchia,
  115. cidocCoords.prefix + 'P89_falls_within',
  116. devPlaceHolder_edificio) + closeLine
  117. output.write(line)
  118. elif row['ID_RESTORE_microtoponimo'] != "" and " ":
  119. line = triple(devPlaceHolder_parrocchia,
  120. cidocCoords.prefix + 'P89_falls_within',
  121. devPlaceHolder_microtoponimo) + closeLine
  122. output.write(line)
  123. elif row ['ID_RESTORE_comune'] != "" and " ":
  124. line = triple(devPlaceHolder_parrocchia,
  125. cidocCoords.prefix + 'P89_falls_within',
  126. devPlaceHolder_comune) + closeLine
  127. output.write(line)
  128. elif row ['ID_PROVINCIA'] != "" and " ":
  129. line = triple(devPlaceHolder_parrocchia,
  130. cidocCoords.prefix + 'P89_falls_within',
  131. devPlaceHolder_provincia) + closeLine
  132. output.write(line)
  133. if row['ID_micromicrotoponimo'] != "" and " ":
  134. line = triple(devPlaceHolder_micromicro,
  135. nsCoords.prefix + 'type',
  136. cidocCoords.prefix + 'E53_Place') + closeLine
  137. output.write(line)
  138. line = triple(devPlaceHolder_micromicro,
  139. schemaCoords.prefix + 'label',
  140. '\"' + row['EVENTO micro microtoponimo'] + '\"') + closeLine
  141. output.write(line)
  142. line = triple(devPlaceHolder_micromicro,
  143. cidocCoords.prefix + 'P1_is_identified_by',
  144. '\"' + row['ID_micromicrotoponimo'] + '\"') + closeLine
  145. output.write(line)
  146. if row ['ID_parrocchia'] != "" and " ":
  147. line = triple(devPlaceHolder_micromicro,
  148. cidocCoords.prefix + 'P89_falls_within',
  149. devPlaceHolder_parrocchia) + closeLine
  150. output.write(line)
  151. if row ['ID_edificio'] != "" and " ":
  152. line = triple(devPlaceHolder_micromicro,
  153. cidocCoords.prefix + 'P89_falls_within',
  154. devPlaceHolder_edificio) + closeLine
  155. output.write(line)
  156. elif row['ID_RESTORE_microtoponimo'] != "" and " ":
  157. line = triple(devPlaceHolder_micromicro,
  158. cidocCoords.prefix + 'P89_falls_within',
  159. devPlaceHolder_microtoponimo) + closeLine
  160. output.write(line)
  161. elif row ['ID_RESTORE_comune'] != "" and " ":
  162. line = triple(devPlaceHolder_micromicro,
  163. cidocCoords.prefix + 'P89_falls_within',
  164. devPlaceHolder_comune) + closeLine
  165. output.write(line)
  166. elif row ['ID_PROVINCIA'] != "" and " ":
  167. line = triple(devPlaceHolder_micromicro,
  168. cidocCoords.prefix + 'P89_falls_within',
  169. devPlaceHolder_provincia) + closeLine
  170. output.write(line)
  171. output.write('\n')
  172. #
  173. #
  174. # Limit number of entries processed (if desired)
  175. if (ii > max_entries):
  176. break