CSV_to_RDF_Date_normalizzazione.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. from urllib.request import urlopen
  8. from bs4 import BeautifulSoup
  9. import json
  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/MPP/CSV/corretti/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/DATE/'
  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. museoCoords = RDFcoords('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  29. autCoords = RDFcoords('<https://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
  30. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  31. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  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. xsdCoords = RDFcoords('<http://www.w3.org/2001/XMLSchema#>', 'xsd:')
  35. iconCoords = RDFcoords('<http://iconclass.org/>', 'ico:')
  36. yearCoords = RDFcoords('<https://www.w3.org/TR/owl-time#year>', 'year:')
  37. monthCoords = RDFcoords('<https://www.w3.org/TR/owl-time#month>', 'month:')
  38. dayCoords = RDFcoords('<https://www.w3.org/TR/owl-time#day>', 'day:')
  39. beginningCoords = RDFcoords('<https://www.w3.org/TR/owl-time#hasBeginning>', 'beg:')
  40. endCoords = RDFcoords('<https://www.w3.org/TR/owl-time#hasEnd>', 'end:')
  41. rdfsCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  42. # Basic functions for triples / shortened triples in TTL format
  43. def triple(subject, predicate, object1):
  44. line = subject + ' ' + predicate + ' ' + object1
  45. return line
  46. def doublet(predicate, object1):
  47. line = ' ' + predicate + ' ' + object1
  48. return line
  49. def singlet(object1):
  50. line = ' ' + object1
  51. return line
  52. # Line endings in TTL format
  53. continueLine1 = ' ;\n'
  54. continueLine2 = ' ,\n'
  55. closeLine = ' .\n'
  56. def writeTTLHeader(output):
  57. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  58. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  59. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  60. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  61. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  62. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  63. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  64. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  65. output.write('@prefix ' + beginningCoords.prefix + ' ' + beginningCoords.uri + closeLine)
  66. output.write('@prefix ' + endCoords.prefix + ' ' + endCoords.uri + closeLine)
  67. output.write('@prefix ' + yearCoords.prefix + ' ' + yearCoords.uri + closeLine)
  68. output.write('@prefix ' + monthCoords.prefix + ' ' + monthCoords.uri + closeLine)
  69. output.write('@prefix ' + dayCoords.prefix + ' ' + dayCoords.uri + closeLine)
  70. output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
  71. output.write('\n')
  72. filePrefix = 'AR20AUT_'
  73. fileType = 'DATINI'
  74. max_entries = 1000000000
  75. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(export_dir + filePrefix + fileType + '_normalizzate.ttl', 'w') as output:
  76. reader = csv.DictReader(csv_file)
  77. writeTTLHeader(output)
  78. first = True
  79. ii = 0
  80. for row in reader:
  81. # The index ii is used to process a limited number of entries for testing purposes
  82. ii = ii + 1
  83. # if row['RVEL'] == '' or row['RVEL'] == '0':
  84. # E12 - P4 - E52
  85. # if row['DTSI'] != '':
  86. # line = triple(museoCoords.prefix + row['DTSI'], yearCoords.prefix, '\"'+row['DTSI'] +'\"^^xsd:integer')+ closeLine
  87. # output.write(line)
  88. # # line = triple(museoCoords.prefix + row['DTSI'], monthCoords.prefix, '\"'+ '01' +'\"^^xsd:integer') + closeLine
  89. # # output.write(line)
  90. # # line = triple(museoCoords.prefix + row['DTSI'], dayCoords.prefix, '\"'+ '01' +'\"^^xsd:integer') + closeLine
  91. # # output.write(line)
  92. # line = triple(museoCoords.prefix + row['DTSI'], beginningCoords.prefix, '\"'+row['DTSI']+'0101'+'\"^^xsd:date') + closeLine
  93. # output.write(line)
  94. # e12FplaceHolder = ''
  95. # if row['DTSI'] != row['DTSF']:
  96. # e12FplaceHolder = museoCoords.prefix + row['URL'] + '_E12F'
  97. # if e12FplaceHolder != '':
  98. # line = triple(museoCoords.prefix + row['DTSF'], yearCoords.prefix, '\"'+row['DTSF'] +'\"^^xsd:integer') + closeLine
  99. # output.write(line)
  100. # # line = triple(museoCoords.prefix + row['DTSF'], monthCoords.prefix, '\"'+ '12' +'\"^^xsd:integer') + closeLine
  101. # # output.write(line)
  102. # # line = triple(museoCoords.prefix + row['DTSF'], dayCoords.prefix, '\"'+ '31' +'\"^^xsd:integer') + closeLine
  103. # # output.write(line)
  104. # line = triple(museoCoords.prefix + row['DTSF'], endCoords.prefix, '\"'+row['DTSF']+'1231'+'\"^^xsd:date') + closeLine
  105. # output.write(line)
  106. if row['AUTD'] != '':
  107. tt = row['AUTD'].replace(' ', '')
  108. tim = tt.replace('/', '')
  109. time = tim.replace('.', '')
  110. # line = triple(museoCoords.prefix + time, yearCoords.prefix, '\"'+row['AUTD'] +'\"^^xsd:integer') + closeLine
  111. # output.write(line)
  112. # line = triple(museoCoords.prefix + time, beginningCoords.prefix, '\"'+row['AUTD']+'0101'+'\"^^xsd:date') + closeLine
  113. # output.write(line)
  114. line = triple(museoCoords.prefix + time, endCoords.prefix, '\"'+row['AUTD']+'1231'+'\"^^xsd:date') + closeLine
  115. output.write(line)
  116. if row['AUTT'] != '':
  117. tt = row['AUTT'].replace(' ', '')
  118. tim = tt.replace('/', '')
  119. time = tim.replace('.', '')
  120. # line = triple(museoCoords.prefix + time, yearCoords.prefix, '\"'+row['AUTT'] +'\"^^xsd:integer') + closeLine
  121. # output.write(line)
  122. # line = triple(museoCoords.prefix + time, endCoords.prefix, '\"'+row['AUTT']+'1231'+'\"^^xsd:date') + closeLine
  123. # output.write(line)
  124. line = triple(museoCoords.prefix + time, beginningCoords.prefix, '\"'+row['AUTT']+'0101'+'\"^^xsd:date') + closeLine
  125. output.write(line)
  126. output.write('\n')
  127. #
  128. #
  129. # Limit number of entries processed (if desired)
  130. if(ii>max_entries):
  131. break
  132. # if row['PRDI' + f] != '':
  133. # timespan = row['PRDI' + f]
  134. # tt = timespan.replace(' ', '')
  135. # tp = tt.replace('.', '')
  136. # ts = tp.replace('/', '')
  137. # timespanPlaceholder = museoCoords.prefix + '_' + ts
  138. # # E10 P4 E52
  139. # line = triple(newe10placeHolder,
  140. # cidocCoords.prefix + 'P4_has_time-span',
  141. # timespanPlaceholder) + closeLine
  142. # output.write(line)
  143. # line = triple(timespanPlaceholder,
  144. # nsCoords.prefix + 'type',
  145. # cidocCoords.prefix + 'E52_Time-Span') + closeLine
  146. # output.write(line)
  147. # line = triple(timespanPlaceholder,
  148. # schemaCoords.prefix + 'label',
  149. # '\"' + timespan + '\"') + closeLine
  150. # output.write(line)
  151. # if row['PRDU' + last] != '':
  152. # timespan = row['PRDU' + last]
  153. # tt = timespan.replace(' ', '')
  154. # ts = tt.replace('/', '')
  155. # timespanPlaceholder = museoCoords.prefix + '_' + ts
  156. # # E10 P4 E52
  157. # line = triple(e10placeHolder,
  158. # cidocCoords.prefix + 'P4_has_time-span',
  159. # timespanPlaceholder) + closeLine
  160. # output.write(line)
  161. # line = triple(timespanPlaceholder,
  162. # nsCoords.prefix + 'type',
  163. # cidocCoords.prefix + 'E52_Time-Span') + closeLine
  164. # output.write(line)
  165. # line = triple(timespanPlaceholder,
  166. # schemaCoords.prefix + 'label',
  167. # '\"' + timespan + '\"') + closeLine
  168. # output.write(line)