123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- # 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/'
- 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('<http://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
- autCoords = RDFcoords('<http://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
- cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
- aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
- nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
- schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
- xsdCoords = RDFcoords('<http://www.w3.org/2001/XMLSchema#>', 'xsd:')
- iconCoords = RDFcoords('<http://iconclass.org/>', 'ico:')
- foafCoords = RDFcoords('<http://xmlns.com/foaf/0.1/>', 'foaf:')
- owlCoords = RDFcoords('<http://www.w3.org/2002/07/owl#>', 'owl:')
- # 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 ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
- output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
- output.write('\n')
- filePrefix = 'OA'
- fileType = '_catalogo'
- max_entries = 1000000000
- def get_aut_url(code):
- aut_file = open(import_dir + 'AR20AUT_' + fileType + '.csv', newline="")
- reader = csv.DictReader(aut_file)
- for row in reader:
- auth = int(row['AUTH'])
- cod = int(code)
- role = ''
- if row['AUTQ'] != '':
- role = row['AUTQ']
- else:
- role = ''
- if auth == cod:
- return [row['URL'], role]
- def get_role(role):
- role_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_RUOLI.csv', newline="")
- reader = csv.DictReader(role_file)
- for row in reader:
- if row['Label'] == role:
- return row['AAT']
- def get_elem(mtc):
- mtc_file = open('/Users/federicaspinelli/Google Drive/OVI-CNR/CSV/MPP/AAT_MTC.csv', newline="")
- reader = csv.DictReader(mtc_file)
- for row in reader:
- if row['MTC'] == mtc:
- return [row['AAT'], row['Type']]
- 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
- e55invplaceHolder = "<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/INV1979>"
- line = triple(e55invplaceHolder, schemaCoords.prefix + 'label', '\"Inventario 1970\"') + closeLine
- output.write(line)
- e55AplaceHolder = "<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/ARCHIVE>"
- line = triple(e55AplaceHolder, schemaCoords.prefix + 'label', '\"Riferimento bibliografico\"') + closeLine
- output.write(line)
- for row in reader:
- # The index ii is used to process a limited number of entries for testing purposes
- ii = ii + 1
- # placeHolders
- urlplaceHolder = '<' + row['Url OPERA'] + '>'
- e36placeHolder = '<' + row['Url OPERA'] + '_E36>'
- e36e42placeHolder = '<' + row['Url OPERA'] + '_E36_E42>'
- e36placeHolder2 = '<' + row['Url OPERA'] + '_E36_2>'
- e36e42placeHolder2 = '<' + row['Url OPERA'] + '_E36_2_E42>'
- e36placeHolder3 = '<' + row['Url OPERA'] + '_E36_3>'
- e36e42placeHolder3 = '<' + row['Url OPERA'] + '_E36_3_E42>'
- e31placeHolder = '<' + row['Url OPERA'] + '_E31>'
- e3142placeHolder = '<' + row['Url OPERA'] + '_E31_E42>'
- e31e36rplaceHolder = '<' + row['Url OPERA'] + '_E31_E36_r>'
- e31e36re42placeHolder = '<' + row['Url OPERA'] + '_E31_E36_r_E42>'
- e31e36vplaceHolder = '<' + row['Url OPERA'] + '_E31_E36_v>'
- e31e36ve42placeHolder = '<' + row['Url OPERA'] + '_E31_E36_v_E42>'
- e31AplaceHolder = '<' + row['LINK ARCHIVE.org'] + '>'
- e31Ae42placeHolder = '<' + row['LINK ARCHIVE.org'] + '_E42>'
- e13placeHolder = '<' + row['Url OPERA'] + '_E13_FA>'
- e31Ae55placeHolder = '<' + row['AAT (Attribution)'] + '_E31_E55>'
- e12placeHolder = '<' + row['Url OPERA'] + '_E12>'
- e12FplaceHolder = '<' + row['Url OPERA'] + '_E12F>'
-
- line = triple(e36placeHolder, cidocCoords.prefix + 'P138_represents', urlplaceHolder) + closeLine
- output.write(line)
- line = triple(e36placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
- output.write(line)
- line = triple(e36placeHolder, schemaCoords.prefix + 'label', '\"Immagine digitale dell\'Opera ' + '\'' + row['NOME 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, schemaCoords.prefix + 'label', '\"'+row['ID Immagine opera 1']+ '\"') + closeLine
- output.write(line)
-
- if (row['ID Immagine opera 2']!= ''):
- line = triple(e36placeHolder2, cidocCoords.prefix + 'P138_represents', urlplaceHolder) + closeLine
- output.write(line)
- line = triple(e36placeHolder2, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
- output.write(line)
- line = triple(e36placeHolder2, schemaCoords.prefix + 'label', '\"Immagine digitale dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
- output.write(line)
- line = triple(e36placeHolder2, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder2) + closeLine
- output.write(line)
- line = triple(e36e42placeHolder2, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
- output.write(line)
- line = triple(e36e42placeHolder2, schemaCoords.prefix + 'label', '\"'+row['ID Immagine opera 2']+ '\"') + closeLine
- output.write(line)
- if (row['ID Immagine opera 3']!= ''):
- line = triple(e36placeHolder3, cidocCoords.prefix + 'P138_represents', urlplaceHolder) + closeLine
- output.write(line)
- line = triple(e36placeHolder3, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
- output.write(line)
- line = triple(e36placeHolder3, schemaCoords.prefix + 'label', '\"Immagine digitale dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
- output.write(line)
- line = triple(e36placeHolder3, cidocCoords.prefix + 'P1_is_identified_by', e36e42placeHolder3) + closeLine
- output.write(line)
- line = triple(e36e42placeHolder3, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
- output.write(line)
- line = triple(e36e42placeHolder3, schemaCoords.prefix + 'label', '\"'+row['ID Immagine opera 3']+ '\"') + closeLine
- output.write(line)
-
- if (row['ID Inventario 1970']!= ''):
- line = triple(e31placeHolder, cidocCoords.prefix + 'P70_documents', urlplaceHolder) + closeLine
- output.write(line)
- line = triple(e31placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E31_Document') + closeLine
- output.write(line)
- line = triple(e31placeHolder, schemaCoords.prefix + 'label', '\"Inventario 1970\"') + closeLine
- output.write(line)
- line = triple(e31placeHolder, cidocCoords.prefix + 'P2_has_type', e55invplaceHolder) + closeLine
- output.write(line)
- line = triple(e31placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e3142placeHolder) + closeLine
- output.write(line)
- line = triple(e3142placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
- output.write(line)
- line = triple(e3142placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Inventario 1970']+ '\"') + closeLine
- output.write(line)
- if (row['ID Immagine Inventario 1970 recto']!= ''):
- line = triple(e31e36rplaceHolder, cidocCoords.prefix + 'P138_represents', e31placeHolder) + closeLine
- output.write(line)
- line = triple(e31e36rplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
- output.write(line)
- line = triple(e31e36rplaceHolder, schemaCoords.prefix + 'label', '\"Rappresentazione digitale della scheda opera d\'arte su ' + "\'Inventario 1970\' - recto" + '\"' ) + closeLine
- output.write(line)
- line = triple(e31e36rplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e31e36re42placeHolder) + closeLine
- output.write(line)
- line = triple(e31e36re42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
- output.write(line)
- line = triple(e31e36re42placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Immagine Inventario 1970 recto']+ '\"') + closeLine
- output.write(line)
- if (row['ID Immagine Inventario 1970 verso']!= ''):
- line = triple(e31e36vplaceHolder, cidocCoords.prefix + 'P138_represents', e31placeHolder) + closeLine
- output.write(line)
- line = triple(e31e36vplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E36_Visual_Item') + closeLine
- output.write(line)
- line = triple(e31e36vplaceHolder, schemaCoords.prefix + 'label', '\"Rappresentazione digitale della scheda opera d\'arte su ' + "\'Inventario 1970\' - verso" + '\"' ) + closeLine
- output.write(line)
- line = triple(e31e36vplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e31e36ve42placeHolder) + closeLine
- output.write(line)
- line = triple(e31e36ve42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
- output.write(line)
- line = triple(e31e36ve42placeHolder, schemaCoords.prefix + 'label', '\"'+row['ID Immagine Inventario 1970 verso']+ '\"') + closeLine
- output.write(line)
-
- if (row['LINK ARCHIVE.org']!= ''):
- line = triple(e31AplaceHolder, cidocCoords.prefix + 'P70_documents', urlplaceHolder) + closeLine
- output.write(line)
- line = triple(e31AplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E31_Document') + closeLine
- output.write(line)
- line = triple(e31AplaceHolder, schemaCoords.prefix + 'label', '\"Riferimento bibliografico della scheda opera d\'arte su ARCHIVE.org\"') + closeLine
- output.write(line)
- line = triple(e31AplaceHolder, cidocCoords.prefix + 'P2_has_type', e55AplaceHolder) + closeLine
- output.write(line)
- line = triple(e31AplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e31Ae42placeHolder) + closeLine
- output.write(line)
- line = triple(e31Ae42placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E42_Identifier') + closeLine
- output.write(line)
- line = triple(e31Ae42placeHolder, schemaCoords.prefix + 'label', '\"'+row['BIBH']+ '\"') + closeLine
- output.write(line)
- if (row['FORMER ATTRIBUTION']!= '' and row['FORMER ATTRIBUTION']!= ' ' and row['FORMER ATTRIBUTION'] is not None):
- line = triple(e12placeHolder, cidocCoords.prefix + 'P141_assigned', e13placeHolder) + closeLine
- output.write(line)
- line = triple(e12FplaceHolder, cidocCoords.prefix + 'P141_assigned', e13placeHolder) + closeLine
- output.write(line)
- if (row['LINK ARCHIVE.org']!= ''):
- line = triple(e13placeHolder, cidocCoords.prefix + 'P42_assigned', e31AplaceHolder) + closeLine
- output.write(line)
- line = triple(e13placeHolder, cidocCoords.prefix + 'P2_has_type', e31Ae55placeHolder) + closeLine
- output.write(line)
- line = triple(e31Ae55placeHolder, schemaCoords.prefix + 'label', '\"AAT Attribution\"') + closeLine
- output.write(line)
- line = triple(e13placeHolder, schemaCoords.prefix + 'label', '\"Former Attribution dell\'Opera ' + '\'' + row['NOME OPERA'] + '\''+'\"') + closeLine
- output.write(line)
- e21ULANplaceHolder = '<'+row['ULAN']+'>'
- if (row['LINK SCHEDA AUT']!= ''):
- e21AUTplaceHolder = '<'+row['LINK SCHEDA AUT']+'>'
- line = triple(e13placeHolder, cidocCoords.prefix + 'P141_assigned', e21AUTplaceHolder) + closeLine
- output.write(line)
- if (row['ULAN']!= ''):
- line = triple(e21AUTplaceHolder, owlCoords.prefix + 'sameAs', e21ULANplaceHolder) + closeLine
- output.write(line)
- if (row['VIAF']!= ''):
- e21VIAFplaceHolder = '<'+row['VIAF']+'>'
- line = triple(e21AUTplaceHolder, owlCoords.prefix + 'sameAs', e21VIAFplaceHolder) + closeLine
- output.write(line)
- else:
- line = triple(e13placeHolder, cidocCoords.prefix + 'P141_assigned', e21ULANplaceHolder) + closeLine
- output.write(line)
- line = triple(e21ULANplaceHolder, schemaCoords.prefix + 'label', '\"' + row['FORMER ATTRIBUTION']+ '\"') + closeLine
- output.write(line)
- line = triple(e21ULANplaceHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E21_Person') + closeLine
- output.write(line)
- line = triple(e21ULANplaceHolder, nsCoords.prefix + 'type', foafCoords.prefix + 'person') + closeLine
- output.write(line)
- if (row['VIAF']!= ''):
- e21VIAFplaceHolder = '<'+row['VIAF']+'>'
- line = triple(e21ULANplaceHolder, owlCoords.prefix + 'sameAs', e21VIAFplaceHolder) + closeLine
- output.write(line)
-
- output.write('\n')
- #
- #
- # Limit number of entries processed (if desired)
- if (ii > max_entries):
- break
|