CSV_to_RDF_onomastica_datini.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #Parser to convert the Datini 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/TEAMOVI/Parser/Data/DallASPO/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/Data/DallASPO/RDF/'
  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/accedi-e-consulta/aspoMV001/scheda/>', '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. owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
  36. # Basic functions for triples / shortened triples in TTL format
  37. def triple(subject, predicate, object1):
  38. line = subject + ' ' + predicate + ' ' + object1
  39. return line
  40. def doublet(predicate, object1):
  41. line = ' ' + predicate + ' ' + object1
  42. return line
  43. def singlet(object1):
  44. line = ' ' + object1
  45. return line
  46. # Line endings in TTL format
  47. continueLine1 = ' ;\n'
  48. continueLine2 = ' ,\n'
  49. closeLine = ' .\n'
  50. def writeTTLHeader(output):
  51. output.write('@prefix ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
  52. output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
  53. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  54. output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)
  55. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  56. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  57. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  58. output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
  59. output.write('\n')
  60. filePrefix = 'onomastica_'
  61. fileType = 'datini'
  62. max_entries = 10000000000000
  63. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  64. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  65. reader = csv.DictReader(csv_file)
  66. writeTTLHeader(output)
  67. first = True
  68. ii = 0
  69. for row in reader:
  70. # The index ii is used to process a limited number of entries for testing purposes
  71. ii = ii + 1
  72. if row['entityType'] == 'person':
  73. id_aspo = row['recordId']
  74. aspoPlaceHolder = aspoCoords.prefix + id_aspo
  75. line = triple(aspoPlaceHolder,
  76. nsCoords.prefix + 'type',
  77. cidocCoords.prefix + 'E21_Person') + closeLine
  78. output.write(line)
  79. line = triple(aspoPlaceHolder,
  80. nsCoords.prefix + 'type',
  81. personCoords.prefix + 'Person') + closeLine
  82. output.write(line)
  83. line = triple(aspoPlaceHolder,
  84. nsCoords.prefix + 'type',
  85. foafCoords.prefix + 'person') + closeLine
  86. output.write(line)
  87. line = triple(aspoPlaceHolder,
  88. cidocCoords.prefix + 'P1_is_identified_by',
  89. aspoPlaceHolder + "_E42") + closeLine
  90. output.write(line)
  91. line = triple(aspoPlaceHolder + "_E42",
  92. nsCoords.prefix + 'type',
  93. cidocCoords.prefix + 'E42_Identifier') + closeLine
  94. output.write(line)
  95. line = triple(aspoPlaceHolder + "_E42",
  96. rdfsCoords.prefix + 'label',
  97. '\"' + id_aspo + '\"') + closeLine
  98. output.write(line)
  99. line = triple(aspoPlaceHolder,
  100. foafCoords.prefix + 'name',
  101. '\"' + row['nameEntry@normal'] + '\"') + closeLine
  102. output.write(line)
  103. line = triple(aspoPlaceHolder,
  104. rdfsCoords.prefix + 'label',
  105. '\"' + row['nameEntry@normal'] + '\"') + closeLine
  106. output.write(line)
  107. if row['nome proprio'] != '':
  108. #Remove all white-space characters:
  109. txt = row['nome proprio']
  110. x = re.sub(" \n", "", txt)
  111. y = re.sub("\s\s", "", x)
  112. name = re.sub("\n", "", y)
  113. line = triple(aspoPlaceHolder,
  114. foafCoords.prefix + 'givenName',
  115. '\"' + name + '\"') + closeLine
  116. output.write(line)
  117. if row['nome di famiglia'] != '':
  118. #Remove all white-space characters:
  119. txt = row['nome di famiglia']
  120. x = re.sub("\n", " ", txt)
  121. y = re.sub("\s\s", "", x)
  122. line = triple(aspoPlaceHolder,
  123. foafCoords.prefix + 'familyName',
  124. '\"' + y + '\"') + closeLine
  125. output.write(line)
  126. if row['Alias'] != '' and row['Alias'] != ' ':
  127. #Remove all white-space characters:
  128. txt = row['Alias']
  129. x = re.sub("\n", " ", txt)
  130. y = re.sub("\s\s", "", x)
  131. line = triple(aspoPlaceHolder,
  132. schemaCoords.prefix + 'alternateName',
  133. '\"' + y + '\"') + closeLine
  134. output.write(line)
  135. if row['genere'] != '':
  136. #Remove all white-space characters:
  137. txt = row['genere']
  138. x = re.sub("\n", " ", txt)
  139. y = re.sub("\s\s", "", x)
  140. line = triple(aspoPlaceHolder,
  141. foafCoords.prefix + 'gender',
  142. '\"' + y + '\"') + closeLine
  143. output.write(line)
  144. if row['patronimico/matronimico'] != '':
  145. #Remove all white-space characters:
  146. txt = row['patronimico/matronimico']
  147. x = re.sub("\n", " ", txt)
  148. y = re.sub("\s\s", "", x)
  149. line = triple(aspoPlaceHolder,
  150. personCoords.prefix + 'patronymicName',
  151. '\"' + y + '\"') + closeLine
  152. output.write(line)
  153. if row['occupation'] != '' and row['occupation'] != ' ' :
  154. occupazioni = []
  155. pipe = "|"
  156. if pipe in row['occupation']:
  157. occupazioni = row['occupation'].split('|')
  158. for occupazione in occupazioni:
  159. #Remove all white-space characters:
  160. txt = occupazione
  161. x = re.sub("\n", " ", txt)
  162. y = re.sub("\s\s", "", x)
  163. occ = re.sub(r'[^A-Za-z]','', y)
  164. occupationPlaceHolder = '<http://www.archiviodistato.prato.it/' + occ.replace(" ","_") + '>'
  165. line = triple(aspoPlaceHolder,
  166. schemaCoords.prefix + 'hasOccupation',
  167. occupationPlaceHolder) + closeLine
  168. output.write(line)
  169. line = triple(occupationPlaceHolder,
  170. nsCoords.prefix + 'type',
  171. schemaCoords.prefix + 'Occupation') + closeLine
  172. output.write(line)
  173. line = triple(occupationPlaceHolder,
  174. rdfsCoords.prefix + 'label',
  175. '\"' + y + '\"') + closeLine
  176. output.write(line)
  177. else:
  178. #Remove all white-space characters:
  179. txt = row['occupation']
  180. x = re.sub("\n", " ", txt)
  181. y = re.sub("\s\s", "", x)
  182. occ = re.sub(r'[^A-Za-z]','', y)
  183. occupationPlaceHolder = '<http://www.archiviodistato.prato.it/' + occ.replace(" ","_") + '>'
  184. line = triple(aspoPlaceHolder,
  185. schemaCoords.prefix + 'hasOccupation',
  186. occupationPlaceHolder) + closeLine
  187. output.write(line)
  188. line = triple(occupationPlaceHolder,
  189. nsCoords.prefix + 'type',
  190. schemaCoords.prefix + 'Occupation') + closeLine
  191. output.write(line)
  192. line = triple(occupationPlaceHolder,
  193. rdfsCoords.prefix + 'label',
  194. '\"' + y + '\"') + closeLine
  195. output.write(line)
  196. if row['avo 1'] != '':
  197. avo1 = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/avo1>"
  198. line = triple(aspoPlaceHolder,
  199. schemaCoords.prefix + 'relatedTo',
  200. avo1) + closeLine
  201. output.write(line)
  202. line = triple(avo1,
  203. nsCoords.prefix + 'type',
  204. foafCoords.prefix + 'Person') + closeLine
  205. output.write(line)
  206. line = triple(avo1,
  207. rdfsCoords.prefix + 'label',
  208. '\"' + row['avo 1'] + '\"') + closeLine
  209. output.write(line)
  210. if row['avo 2'] != '':
  211. avo2 = '<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/' + id_aspo + "/avo2>"
  212. line = triple(aspoPlaceHolder,
  213. schemaCoords.prefix + 'relatedTo',
  214. avo2) + closeLine
  215. output.write(line)
  216. line = triple(avo2,
  217. nsCoords.prefix + 'type',
  218. foafCoords.prefix + 'Person') + closeLine
  219. output.write(line)
  220. line = triple(avo2,
  221. rdfsCoords.prefix + 'label',
  222. '\"' + row['avo 2'] + '\"') + closeLine
  223. output.write(line)
  224. if row['Qualifica'] != '':
  225. qualifiche = []
  226. pipe = "|"
  227. if pipe in row['Qualifica']:
  228. qualifiche = row['Qualifica'].split('|')
  229. for qualifica in qualifiche:
  230. #Remove all white-space characters:
  231. txt = qualifica
  232. x = re.sub("\n", " ", txt)
  233. y = re.sub("\s\s", " ", x)
  234. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + str(y) + '\"') + closeLine
  235. output.write(line)
  236. else:
  237. #Remove all white-space characters:
  238. txt = row['Qualifica']
  239. x = re.sub("\n", " ", txt)
  240. y = re.sub("\s\s", " ", x)
  241. line = triple(aspoPlaceHolder, schemaCoords.prefix + 'honorificPrefix', '\"' + y + '\"') + closeLine
  242. output.write(line)
  243. if row['place_occupation_Qualifica'] != '':
  244. #Remove all white-space characters:
  245. txt = row['place_occupation_Qualifica']
  246. x = re.sub("\n", " ", txt)
  247. y = re.sub("\s\s", "", x)
  248. line = triple(aspoPlaceHolder,
  249. schemaCoords.prefix + 'workLocation',
  250. '\"' + row['place_occupation_Qualifica'].replace('\\','\\\\').replace('"','\\"') + '\"') + closeLine
  251. output.write(line)
  252. if row['biogHist p'] != '':
  253. #Remove all white-space characters:
  254. txt = row['biogHist p']
  255. x = re.sub("\n", " ", txt)
  256. y = re.sub("\s\s", " ", x)
  257. note = re.sub("\"", "", x)
  258. line = triple(aspoPlaceHolder,
  259. cidocCoords.prefix + 'P3_has_note',
  260. '\"' + note + '\"') + closeLine
  261. output.write(line)
  262. if row['Variante'] != '':
  263. varianti = []
  264. pipe = "|"
  265. if pipe in row['Variante']:
  266. varianti = row['Variante'].split('|')
  267. for variante in varianti:
  268. line = triple(aspoPlaceHolder,
  269. owlCoords.prefix + 'sameAs',
  270. aspoCoords.prefix + str(variante)) + closeLine
  271. output.write(line)
  272. else:
  273. line = triple(aspoPlaceHolder,
  274. owlCoords.prefix + 'sameAs',
  275. aspoCoords.prefix + row['Variante']) + closeLine
  276. output.write(line)
  277. output.write('\n')
  278. #
  279. # Limit number of entries processed (if desired)
  280. if (ii > max_entries):
  281. break