# 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/Google Drive/OVI-CNR/CSV/MPP/corretti/'
export_dir = '/Users/federicaspinelli/Google Drive/OVI-CNR/RDF/MPP/'
# 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:')
# 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('\n')
filePrefix = '00_SR20OA_'
fileType = 'Datini'
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
for row in reader:
# The index ii is used to process a limited number of entries for testing purposes
ii = ii + 1
sb = ''
subj = ''
pp = row['OGTD'] + ' (' + row['ACC'] + ') '
if row['SGTI'] != '':
sb = pp + row['SGTI']
if row['LDCN'] != '':
subj = sb + ' in ' + row['LDCN']
else:
subj = sb
# Triplify the 'codice' -- should exist for every entry
codice = ''
if (row['NCTR'] != '' and row['NCTN'] != ''):
codice = row['NCTR'] + row['NCTN']
codiceP = ''
if (row['AUTH'] != ''):
codiceP = row['AUTH']
place = ''
if (row['PRVC'] != ''):
place = row['PRVC']
columnName = list(row)
url = row['URL']
# placeHolders
datplaceHolder = museoCoords.prefix + url
e1placeHolder = museoCoords.prefix + url + '_E1'
e3placeHolder = museoCoords.prefix + url + 'E3'
e10placeHolder = museoCoords.prefix + url + '_E10'
e12placeHolder = museoCoords.prefix + url + '_E12'
e13placeHolder = museoCoords.prefix + url + '_E13'
e21placeHolder = museoCoords.prefix + url + '_InE21'
e25placeHolder = museoCoords.prefix + url + '_E25'
e34placeHolder = museoCoords.prefix + url + '_E34'
e35placeHolder1 = museoCoords.prefix + url + '_E35'
e42placeHolder = museoCoords.prefix + url + '_E42'
e42CplaceHolder = museoCoords.prefix + url + '_E42_MPP'
e65placeHolder = museoCoords.prefix + url + '_InE65'
e73placeHolder = museoCoords.prefix + url + '_E73'
e74placeHolder = museoCoords.prefix + url + '_E74'
if (codice != ''):
line = triple(datplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', '\"' + codice + '\"') + closeLine
output.write(line)
output.write('\n')
#
#
# Limit number of entries processed (if desired)
if (ii > max_entries):
break