# Utilities to read/write csv files import csv # Utilities to handle character encodings import unicodedata # Ordered Dicts from collections import OrderedDict from http.cookiejar import CookieJar from urllib.request import urlopen #from bs4 import BeautifulSoup import urllib import json from socket import error as SocketError import html.parser # 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/RDF/' # 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:') documentsCoords = RDFcoords('', 'ds:') rdfsCoords = RDFcoords('', 'rdfs:') nsCoords = RDFcoords('', 'rdf:') schemaCoords = RDFcoords('', 'schema:') 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 ' + autCoords.prefix + ' ' + autCoords.uri + closeLine) output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine) output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine) output.write('@prefix ' + documentsCoords.prefix + ' ' + documentsCoords.uri + closeLine) output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine) output.write('\n') filePrefix = 'OA_BIB_' fileType = 'IMG' max_entries = 1000000000 with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open( export_dir + filePrefix + fileType + '.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 # columnName = list(row) # placeHolders pp = row['NCT'] if row['BIBN-SO-1']!= '': e73placeHolderEDPP = "" id = row['BIBN-SO-1'].replace(".jpg", "") e36placeHolder = "" e36e42placeHolder = "' line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolderEDPP) + closeLine output.write(line) line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine output.write(line) line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Opera\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Opera\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine output.write(line) line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine output.write(line) line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['BIBN-SO-1']+ '\"') + closeLine output.write(line) if row['BIBN-SO-2']!= '': e73placeHolderEDPP = "" id = row['BIBN-SO-2'].replace(".jpg", "") e36placeHolder = "" e36e42placeHolder = "' line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolderEDPP) + closeLine output.write(line) line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine output.write(line) line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Opera\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Opera\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine output.write(line) line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine output.write(line) line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['BIBN-SO-2']+ '\"') + closeLine output.write(line) if row['BIBN-SO-3']!= '': e73placeHolderEDPP = "" id = row['BIBN-SO-3'].replace(".jpg", "") e36placeHolder = "" e36e42placeHolder = "' line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolderEDPP) + closeLine output.write(line) line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine output.write(line) line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Opera\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Opera\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine output.write(line) line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine output.write(line) line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['BIBN-SO-3']+ '\"') + closeLine output.write(line) if row['FNTI-SS']!= '': e73placeHolder = "<"+ row['ID OPERA'].replace(" ","")+ "_E73>" id = row['FNTI-SS'].replace(".pdf", "") e36placeHolder = "" e36e42placeHolder = "" line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', e73placeHolder) + closeLine output.write(line) line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine output.write(line) line = triple(e36placeHolder, rdfsCoords.prefix + 'label', '\"Scheda Storica\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P2_has_type', '\"Scheda Storica\"') + closeLine output.write(line) line = triple(e36placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder) + closeLine output.write(line) line = triple(e36e42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine output.write(line) line = triple(e36e42placeHolder, rdfsCoords.prefix + 'label', '\"'+row['FNTI-SS'].replace("pdf","jpg")+ '\"') + closeLine output.write(line) output.write('\n') # # # Limit number of entries processed (if desired) if (ii > max_entries): break