123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- # Utilities to read/write csv files
- import csv
- # Utilities to handle character encodings
- import unicodedata
- # Ordered Dicts
- from collections import OrderedDict
- 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/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/'
- export_dir = '/Users/alessiaspadi/Documents/RESTORE/temp_MPP/tabelle/Carica/'
- # 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.comune.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
- tgnCoords = RDFcoords('<http://vocab.getty.edu/tgn/>', 'tgn:')
- placeCoords = RDFcoords('<http://www.archiviodistato.prato.it/accedi-e-consulta/aspoMV001/scheda/>', 'pl:')
- repettiCoords = RDFcoords('<http://193.205.4.99/repetti/tester.php?idx=>', 'rpt:')
- 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:')
- 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 ' + tgnCoords.prefix + ' ' + tgnCoords.uri + closeLine)
- output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)
- output.write('@prefix ' + repettiCoords.prefix + ' ' + repettiCoords.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 ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
- output.write('\n')
- file = "Luoghi"
- max_entries = 1000000000
- with open(import_dir + file + '.csv', newline="") as csv_file, open(
- export_dir + file + '.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
- pl = row['Places']
- place = pl.replace(' ', '')
- #placeHolders
- locplaceHolder = museoCoords.prefix + place
- line = triple(locplaceHolder,
- nsCoords.prefix + 'type',
- cidocCoords.prefix + 'E53_Place') + closeLine
- output.write(line)
- line = triple(locplaceHolder,
- schemaCoords.prefix + 'label',
- '\"' + row['Places'] + '\"') + closeLine
- output.write(line)
- if row['TGN'] != '':
- line = triple(locplaceHolder,
- owlCoords.prefix + 'sameAs',
- tgnCoords.prefix + row['TGN']) + closeLine
- output.write(line)
- if row['EAC-ID'] != '':
- line = triple(locplaceHolder,
- owlCoords.prefix + 'sameAs',
- placeCoords.prefix + row['EAC-ID']) + closeLine
- output.write(line)
- if row['Repetti (solo Toscana)'] != '':
- line = triple(locplaceHolder,
- owlCoords.prefix + 'sameAs',
- repettiCoords.prefix + row['Repetti (solo Toscana)']) + closeLine
- if row['TGN'] != '':
- line = triple(tgnCoords.prefix + row['TGN'],
- owlCoords.prefix + 'sameAs',
- locplaceHolder) + closeLine
- output.write(line)
- line = triple(tgnCoords.prefix + row['TGN'],
- nsCoords.prefix + 'type',
- cidocCoords.prefix + 'E53_Place') + closeLine
- output.write(line)
- line = triple(tgnCoords.prefix + row['TGN'],
- schemaCoords.prefix + 'label',
- '\"' + row['Places'] + '\"') + closeLine
- output.write(line)
- if row['EAC-ID'] != '':
- line = triple(tgnCoords.prefix + row['TGN'],
- owlCoords.prefix + 'sameAs',
- placeCoords.prefix + row['EAC-ID']) + closeLine
- output.write(line)
- if row['Repetti (solo Toscana)'] != '':
- line = triple(tgnCoords.prefix + row['TGN'],
- owlCoords.prefix + 'sameAs',
- repettiCoords.prefix + row['Repetti (solo Toscana)']) + closeLine
- output.write(line)
- if row['EAC-ID'] != '':
- line = triple(placeCoords.prefix + row['EAC-ID'],
- owlCoords.prefix + 'sameAs',
- locplaceHolder) + closeLine
- output.write(line)
- line = triple(placeCoords.prefix + row['EAC-ID'],
- nsCoords.prefix + 'type',
- cidocCoords.prefix + 'E53_Place') + closeLine
- output.write(line)
- if row['TGN'] != '':
- line = triple(placeCoords.prefix + row['EAC-ID'],
- owlCoords.prefix + 'sameAs',
- tgnCoords.prefix + row['TGN']) + closeLine
- output.write(line)
- if row['Repetti (solo Toscana)'] != '':
- line = triple(placeCoords.prefix + row['EAC-ID'],
- owlCoords.prefix + 'sameAs',
- repettiCoords.prefix + row['Repetti (solo Toscana)']) + closeLine
- output.write(line)
- if row['Repetti (solo Toscana)'] != '':
- line = triple(repettiCoords.prefix + row['Repetti (solo Toscana)'],
- owlCoords.prefix + 'sameAs',
- locplaceHolder) + closeLine
- output.write(line)
- line = triple(repettiCoords.prefix + row['Repetti (solo Toscana)'],
- nsCoords.prefix + 'type',
- cidocCoords.prefix + 'E53_Place') + closeLine
- output.write(line)
- line = triple(repettiCoords.prefix + row['Repetti (solo Toscana)'],
- schemaCoords.prefix + 'label',
- '\"' + row['Places'] + '\"') + closeLine
- output.write(line)
- if row['TGN'] != '':
- line = triple(repettiCoords.prefix + row['Repetti (solo Toscana)'],
- owlCoords.prefix + 'sameAs',
- tgnCoords.prefix + row['TGN']) + closeLine
- output.write(line)
- if row['EAC-ID'] != '':
- line = triple(repettiCoords.prefix + row['Repetti (solo Toscana)'],
- owlCoords.prefix + 'sameAs',
- placeCoords.prefix + row['EAC-ID']) + closeLine
- output.write(line)
|