# Utilities to read/write csv files import csv # Utilities to handle character encodings import unicodedata # Ordered Dicts from collections import OrderedDict from urllib.request import urlopen from bs4 import BeautifulSoup import json # OPZIONAL IMPORTS # For timestamping/simple speed tests from datetime import datetime # Random number generator from random import * # System & command line utilities import sys # Json for the dictionary import json import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/corretti/' export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/DATE/' # Custom class to store URIs + related infos for the ontologies/repositories class RDFcoords: def __init__(self, uri, prefix, code=None): self.uri = uri self.prefix = prefix self.code = code # Repositories museoCoords = RDFcoords('', 'mpp:') autCoords = RDFcoords('', 'aut:') cidocCoords = RDFcoords('', 'crm:') aatCoords = RDFcoords('', 'aat:') nsCoords = RDFcoords('', 'rdf:') schemaCoords = RDFcoords('', 'rdfs:') xsdCoords = RDFcoords('', 'xsd:') iconCoords = RDFcoords('', 'ico:') yearCoords = RDFcoords('', 'year:') monthCoords = RDFcoords('', 'month:') dayCoords = RDFcoords('', 'day:') beginningCoords = RDFcoords('', 'beg:') endCoords = RDFcoords('', 'end:') rdfsCoords = RDFcoords('', 'rdfs:') # Basic functions for triples / shortened triples in TTL format def triple(subject, predicate, object1): line = subject + ' ' + predicate + ' ' + object1 return line def doublet(predicate, object1): line = ' ' + predicate + ' ' + object1 return line def singlet(object1): line = ' ' + object1 return line # Line endings in TTL format continueLine1 = ' ;\n' continueLine2 = ' ,\n' closeLine = ' .\n' def writeTTLHeader(output): output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine) output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine) output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine) output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine) output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine) output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine) output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine) output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine) output.write('@prefix ' + beginningCoords.prefix + ' ' + beginningCoords.uri + closeLine) output.write('@prefix ' + endCoords.prefix + ' ' + endCoords.uri + closeLine) output.write('@prefix ' + yearCoords.prefix + ' ' + yearCoords.uri + closeLine) output.write('@prefix ' + monthCoords.prefix + ' ' + monthCoords.uri + closeLine) output.write('@prefix ' + dayCoords.prefix + ' ' + dayCoords.uri + closeLine) output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine) output.write('\n') filePrefix = 'AR20AUT_' fileType = 'DATINI' max_entries = 1000000000 with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(export_dir + filePrefix + fileType + '_normalizzate.ttl', 'w') as output: reader = csv.DictReader(csv_file) writeTTLHeader(output) first = True ii = 0 for row in reader: # The index ii is used to process a limited number of entries for testing purposes ii = ii + 1 # if row['RVEL'] == '' or row['RVEL'] == '0': # E12 - P4 - E52 # if row['DTSI'] != '': # line = triple(museoCoords.prefix + row['DTSI'], yearCoords.prefix, '\"'+row['DTSI'] +'\"^^xsd:integer')+ closeLine # output.write(line) # # line = triple(museoCoords.prefix + row['DTSI'], monthCoords.prefix, '\"'+ '01' +'\"^^xsd:integer') + closeLine # # output.write(line) # # line = triple(museoCoords.prefix + row['DTSI'], dayCoords.prefix, '\"'+ '01' +'\"^^xsd:integer') + closeLine # # output.write(line) # line = triple(museoCoords.prefix + row['DTSI'], beginningCoords.prefix, '\"'+row['DTSI']+'0101'+'\"^^xsd:date') + closeLine # output.write(line) # e12FplaceHolder = '' # if row['DTSI'] != row['DTSF']: # e12FplaceHolder = museoCoords.prefix + row['URL'] + '_E12F' # if e12FplaceHolder != '': # line = triple(museoCoords.prefix + row['DTSF'], yearCoords.prefix, '\"'+row['DTSF'] +'\"^^xsd:integer') + closeLine # output.write(line) # # line = triple(museoCoords.prefix + row['DTSF'], monthCoords.prefix, '\"'+ '12' +'\"^^xsd:integer') + closeLine # # output.write(line) # # line = triple(museoCoords.prefix + row['DTSF'], dayCoords.prefix, '\"'+ '31' +'\"^^xsd:integer') + closeLine # # output.write(line) # line = triple(museoCoords.prefix + row['DTSF'], endCoords.prefix, '\"'+row['DTSF']+'1231'+'\"^^xsd:date') + closeLine # output.write(line) if row['AUTD'] != '': tt = row['AUTD'].replace(' ', '') tim = tt.replace('/', '') time = tim.replace('.', '') # line = triple(museoCoords.prefix + time, yearCoords.prefix, '\"'+row['AUTD'] +'\"^^xsd:integer') + closeLine # output.write(line) # line = triple(museoCoords.prefix + time, beginningCoords.prefix, '\"'+row['AUTD']+'0101'+'\"^^xsd:date') + closeLine # output.write(line) line = triple(museoCoords.prefix + time, endCoords.prefix, '\"'+row['AUTD']+'1231'+'\"^^xsd:date') + closeLine output.write(line) if row['AUTT'] != '': tt = row['AUTT'].replace(' ', '') tim = tt.replace('/', '') time = tim.replace('.', '') # line = triple(museoCoords.prefix + time, yearCoords.prefix, '\"'+row['AUTT'] +'\"^^xsd:integer') + closeLine # output.write(line) # line = triple(museoCoords.prefix + time, endCoords.prefix, '\"'+row['AUTT']+'1231'+'\"^^xsd:date') + closeLine # output.write(line) line = triple(museoCoords.prefix + time, beginningCoords.prefix, '\"'+row['AUTT']+'0101'+'\"^^xsd:date') + closeLine output.write(line) output.write('\n') # # # Limit number of entries processed (if desired) if(ii>max_entries): break # if row['PRDI' + f] != '': # timespan = row['PRDI' + f] # tt = timespan.replace(' ', '') # tp = tt.replace('.', '') # ts = tp.replace('/', '') # timespanPlaceholder = museoCoords.prefix + '_' + ts # # E10 P4 E52 # line = triple(newe10placeHolder, # cidocCoords.prefix + 'P4_has_time-span', # timespanPlaceholder) + closeLine # output.write(line) # line = triple(timespanPlaceholder, # nsCoords.prefix + 'type', # cidocCoords.prefix + 'E52_Time-Span') + closeLine # output.write(line) # line = triple(timespanPlaceholder, # schemaCoords.prefix + 'label', # '\"' + timespan + '\"') + closeLine # output.write(line) # if row['PRDU' + last] != '': # timespan = row['PRDU' + last] # tt = timespan.replace(' ', '') # ts = tt.replace('/', '') # timespanPlaceholder = museoCoords.prefix + '_' + ts # # E10 P4 E52 # line = triple(e10placeHolder, # cidocCoords.prefix + 'P4_has_time-span', # timespanPlaceholder) + closeLine # output.write(line) # line = triple(timespanPlaceholder, # nsCoords.prefix + 'type', # cidocCoords.prefix + 'E52_Time-Span') + closeLine # output.write(line) # line = triple(timespanPlaceholder, # schemaCoords.prefix + 'label', # '\"' + timespan + '\"') + closeLine # output.write(line)