ASPO_CSV_to_RDF_onomastica_ceppo.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #Parser to convert the Ceppo Vecchio onomastics CSV file into TTL format
  2. # Utilities to read/write csv files
  3. import csv
  4. # Utilities to handle character encodings
  5. import unicodedata
  6. # Ordered Dicts
  7. from collections import OrderedDict
  8. import json
  9. import re
  10. # OPZIONAL IMPORTS
  11. # For timestamping/simple speed tests
  12. from datetime import datetime
  13. # Random number generator
  14. from random import *
  15. # System & command line utilities
  16. import sys
  17. # Json for the dictionary
  18. import json
  19. import_dir = '/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/ASPO/ceppo/'
  20. export_dir = '/Users/federicaspinelli/Google Drive/OVI-CNR/RDF/ASPO/ceppo/'
  21. # Custom class to store URIs + related infos for the ontologies/repositories
  22. class RDFcoords:
  23. def __init__(self, uri, prefix, code = None):
  24. self.uri = uri
  25. self.prefix = prefix
  26. self.code = code
  27. # Repositories
  28. aspoCoords = RDFcoords('<http://www.archiviodistato.prato.it/patrimonio/complessi-archivistici-e-soggetti-produttori/>', 'aspo:')
  29. foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
  30. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  31. schemaCoords = RDFcoords('<http://schema.org/>', 'schema:')
  32. personCoords = RDFcoords('<http://www.w3.org/ns/person#>', 'person:')
  33. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  34. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  35. # Basic functions for triples / shortened triples in TTL format
  36. def triple(subject, predicate, object1):
  37. line = subject + ' ' + predicate + ' ' + object1
  38. return line
  39. def doublet(predicate, object1):
  40. line = ' ' + predicate + ' ' + object1
  41. return line
  42. def singlet(object1):
  43. line = ' ' + object1
  44. return line
  45. # Line endings in TTL format
  46. continueLine1 = ' ;\n'
  47. continueLine2 = ' ,\n'
  48. closeLine = ' .\n'
  49. def writeTTLHeader(output):
  50. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  51. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  52. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  53. output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)
  54. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  55. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  56. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  57. output.write('\n')
  58. filePrefix = 'onomastica_'
  59. fileType = 'ceppo_vecchio'
  60. max_entries = 1000000000
  61. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  62. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  63. reader = csv.DictReader(csv_file)
  64. writeTTLHeader(output)
  65. first = True
  66. ii = 0
  67. for row in reader:
  68. # The index ii is used to process a limited number of entries for testing purposes
  69. ii = ii + 1
  70. if row['nameEntry@normal'] != '':
  71. id_aspo = row['recordId']
  72. #placeHolders
  73. aspoPlaceHolder = aspoCoords.prefix + id_aspo
  74. id_aspo = row['recordId']
  75. line = triple(aspoPlaceHolder,
  76. cidocCoords.prefix + 'P1_is_identified_by',
  77. aspoPlaceHolder + "_E42") + closeLine
  78. output.write(line)
  79. line = triple(aspoPlaceHolder + "_E42",
  80. nsCoords.prefix + 'type',
  81. cidocCoords.prefix + 'E42_Identifier') + closeLine
  82. output.write(line)
  83. line = triple(aspoPlaceHolder + "_E42",
  84. rdfsCoords.prefix + 'label',
  85. '\"' + id_aspo + '\"') + closeLine
  86. output.write(line)
  87. line = triple(aspoPlaceHolder,
  88. nsCoords.prefix + 'type',
  89. foafCoords.prefix + 'person') + closeLine
  90. output.write(line)
  91. line = triple(aspoPlaceHolder,
  92. foafCoords.prefix + 'name',
  93. '\"' + row['nameEntry@normal'] + '\"') + closeLine
  94. output.write(line)
  95. if row['nome proprio'] != '':
  96. #Remove all white-space characters:
  97. txt = row['nome proprio']
  98. x = re.sub(" \n", "", txt)
  99. y = re.sub("\s\s", "", x)
  100. line = triple(aspoPlaceHolder,
  101. foafCoords.prefix + 'givenName',
  102. '\"' + y + '\"') + closeLine
  103. output.write(line)
  104. if row['nome di famiglia'] != '':
  105. #Remove all white-space characters:
  106. txt = row['nome di famiglia']
  107. x = re.sub("\n", " ", txt)
  108. y = re.sub("\s\s", "", x)
  109. line = triple(aspoPlaceHolder,
  110. foafCoords.prefix + 'familyName',
  111. '\"' + y + '\"') + closeLine
  112. output.write(line)
  113. if row['Alias'] != '' and row['Alias'] != ' ':
  114. #Remove all white-space characters:
  115. txt = row['Alias']
  116. x = re.sub("\n", " ", txt)
  117. y = re.sub("\s\s", "", x)
  118. line = triple(aspoPlaceHolder,
  119. schemaCoords.prefix + 'alternateName',
  120. '\"' + y + '\"') + closeLine
  121. output.write(line)
  122. if row['genere'] != '':
  123. #Remove all white-space characters:
  124. txt = row['genere']
  125. x = re.sub("\n", " ", txt)
  126. y = re.sub("\s\s", "", x)
  127. line = triple(aspoPlaceHolder,
  128. foafCoords.prefix + 'gender',
  129. '\"' + y + '\"') + closeLine
  130. output.write(line)
  131. if row['patronimico/matronimico'] != '':
  132. #Remove all white-space characters:
  133. txt = row['patronimico/matronimico']
  134. x = re.sub("\n", " ", txt)
  135. y = re.sub("\s\s", "", x)
  136. line = triple(aspoPlaceHolder,
  137. personCoords.prefix + 'patronymicName',
  138. '\"' + y + '\"') + closeLine
  139. output.write(line)
  140. if row['occupation'] != '' and row['occupation'] != ' ' :
  141. occupationPlaceHolder = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/occupation>"
  142. #Remove all white-space characters:
  143. txt = row['occupation']
  144. x = re.sub("\n", " ", txt)
  145. y = re.sub("\s\s", "", x)
  146. line = triple(aspoPlaceHolder,
  147. schemaCoords.prefix + 'hasOccupation',
  148. occupationPlaceHolder) + closeLine
  149. output.write(line)
  150. line = triple(occupationPlaceHolder,
  151. nsCoords.prefix + 'type',
  152. schemaCoords.prefix + 'Occupation') + closeLine
  153. output.write(line)
  154. line = triple(occupationPlaceHolder,
  155. rdfsCoords.prefix + 'label',
  156. '\"' + y + '\"') + closeLine
  157. output.write(line)
  158. if row['avo 1'] != '':
  159. avo1 = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/avo1>"
  160. line = triple(aspoPlaceHolder,
  161. schemaCoords.prefix + 'relatedTo',
  162. avo1) + closeLine
  163. output.write(line)
  164. line = triple(avo1,
  165. nsCoords.prefix + 'type',
  166. foafCoords.prefix + 'Person') + closeLine
  167. output.write(line)
  168. line = triple(avo1,
  169. rdfsCoords.prefix + 'label',
  170. '\"' + row['avo 1'] + '\"') + closeLine
  171. output.write(line)
  172. if row['avo 2'] != '':
  173. avo2 = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/avo2>"
  174. line = triple(aspoPlaceHolder,
  175. schemaCoords.prefix + 'relatedTo',
  176. avo2) + closeLine
  177. output.write(line)
  178. line = triple(avo2,
  179. nsCoords.prefix + 'type',
  180. foafCoords.prefix + 'Person') + closeLine
  181. output.write(line)
  182. line = triple(avo2,
  183. rdfsCoords.prefix + 'label',
  184. '\"' + row['avo 2'] + '\"') + closeLine
  185. output.write(line)
  186. if row['Qualifica'] != '':
  187. #Remove all white-space characters:
  188. txt = row['Qualifica']
  189. x = re.sub("\n", " ", txt)
  190. y = re.sub("\s\s", " ", x)
  191. line = triple(aspoPlaceHolder,
  192. schemaCoords.prefix + 'honorificPrefix',
  193. '\"' + y + '\"') + closeLine
  194. output.write(line)
  195. if row['place_occupation_Qualifica'] != '':
  196. #Remove all white-space characters:
  197. txt = row['place_occupation_Qualifica']
  198. x = re.sub("\n", " ", txt)
  199. y = re.sub("\s\s", "", x)
  200. line = triple(aspoPlaceHolder,
  201. schemaCoords.prefix + 'workLocation',
  202. '\"' + row['place_occupation_Qualifica'].replace('\\','\\\\').replace('"','\\"') + '\"') + closeLine
  203. output.write(line)
  204. if row['biogHist p'] != '':
  205. #Remove all white-space characters:
  206. txt = row['biogHist p']
  207. x = re.sub("\n", " ", txt)
  208. y = re.sub("\s\s", " ", x)
  209. line = triple(aspoPlaceHolder,
  210. cidocCoords.prefix + 'P3_has_note',
  211. '\"' + y + '\"') + closeLine
  212. output.write(line)
  213. output.write('\n')
  214. #
  215. #
  216. # Limit number of entries processed (if desired)
  217. if (ii > max_entries):
  218. break