#Parser to convert the Datini onomastics CSV file into TTL format
# Utilities to read/write csv files
import csv
# Utilities to handle character encodings
import unicodedata
# Ordered Dicts
from collections import OrderedDict
import json
import re
# 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/ASPO/CSV/ospedale/'
export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/RDF/ospedale/'
# 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
aspoCoords = RDFcoords('', 'aspo:')
foafCoords = RDFcoords('', 'foaf:')
cidocCoords = RDFcoords('', 'crm:')
schemaCoords = RDFcoords('', 'schema:')
personCoords = RDFcoords('', 'person:')
nsCoords = RDFcoords('', 'rdf:')
rdfsCoords = RDFcoords('', 'rdfs:')
owlCoords = RDFcoords('', '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 ' + aspoCoords.prefix + ' ' + aspoCoords.uri + closeLine)
output.write('@prefix ' + foafCoords.prefix + ' ' + foafCoords.uri + closeLine)
output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)
output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
output.write('@prefix ' + rdfsCoords.prefix + ' ' + rdfsCoords.uri + closeLine)
output.write('@prefix ' + owlCoords.prefix + ' ' + owlCoords.uri + closeLine)
output.write('\n')
filePrefix = 'OSPEDALE - onomastica '
fileType = '- corporate_body'
max_entries = 10000000000000
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
if row['entityType'] == 'corporateBody':
id_aspo = row['recordId']
aspoPlaceHolder = aspoCoords.prefix + id_aspo
line = triple(aspoPlaceHolder,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E74_Group') + closeLine
output.write(line)
line = triple(aspoPlaceHolder,
nsCoords.prefix + 'type',
foafCoords.prefix + 'Group') + closeLine
output.write(line)
line = triple(aspoPlaceHolder,
cidocCoords.prefix + 'P1_is_identified_by',
aspoPlaceHolder + "_E42") + closeLine
output.write(line)
line = triple(aspoPlaceHolder + "_E42",
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E42_Identifier') + closeLine
output.write(line)
line = triple(aspoPlaceHolder + "_E42",
rdfsCoords.prefix + 'label',
'\"' + id_aspo + '\"') + closeLine
output.write(line)
line = triple(aspoPlaceHolder,
foafCoords.prefix + 'name',
'\"' + row['nameEntry@normal'].replace("\"", "") + '\"') + closeLine
output.write(line)
line = triple(aspoPlaceHolder,
rdfsCoords.prefix + 'label',
'\"' + row['nameEntry@normal'].replace("\"", "") + '\"') + closeLine
output.write(line)
if row['occupation'] != '' and row['occupation'] != ' ' :
occupazioni = []
pipe = "|"
if pipe in row['occupation']:
occupazioni = row['occupation'].split('|')
for occupazione in occupazioni:
#Remove all white-space characters:
txt = occupazione
x = re.sub("\n", " ", txt)
y = re.sub("\s\s", "", x)
occ = re.sub(r'[^A-Za-z]','', y)
occupationPlaceHolder = ''
line = triple(aspoPlaceHolder,
schemaCoords.prefix + 'hasOccupation',
occupationPlaceHolder) + closeLine
output.write(line)
line = triple(occupationPlaceHolder,
nsCoords.prefix + 'type',
schemaCoords.prefix + 'Occupation') + closeLine
output.write(line)
line = triple(occupationPlaceHolder,
rdfsCoords.prefix + 'label',
'\"' + y + '\"') + closeLine
output.write(line)
else:
#Remove all white-space characters:
txt = row['occupation']
x = re.sub("\n", " ", txt)
y = re.sub("\s\s", "", x)
occ = re.sub(r'[^A-Za-z]','', y)
occupationPlaceHolder = ''
line = triple(aspoPlaceHolder,
schemaCoords.prefix + 'hasOccupation',
occupationPlaceHolder) + closeLine
output.write(line)
line = triple(occupationPlaceHolder,
nsCoords.prefix + 'type',
schemaCoords.prefix + 'Occupation') + closeLine
output.write(line)
line = triple(occupationPlaceHolder,
rdfsCoords.prefix + 'label',
'\"' + y + '\"') + closeLine
output.write(line)
if row['tipologia'] != '':
line = triple(aspoPlaceHolder,
cidocCoords.prefix + 'P2_has_type',
'\"' + row['tipologia'] + '\"') + closeLine
output.write(line)
if row['intestatario principale: nome o ID se lo abbiamo'] != '':
E13placeHolder = '"
line = triple(E13placeHolder,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
output.write(line)
line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
output.write(line)
line = triple(E13placeHolder,
rdfsCoords.prefix + 'label',
'\"Relazione: ' + row['intestatario principale: nome o ID se lo abbiamo'] + ' intestatario principale del gruppo ' + row['recordId'] + '\"') + closeLine
output.write(line)
E55placeHolder = ''
line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
output.write(line)
line = triple(E55placeHolder, rdfsCoords.prefix + 'label', '\"Socio\"') + closeLine
output.write(line)
line = triple(E55placeHolder, cidocCoords.prefix + 'P3_has_note', '\"Intestatario principale\"') + closeLine
output.write(line)
id = row['intestatario principale: nome o ID se lo abbiamo']
if re.match(r'IT-ASPO', id):
socioid = '"
line = triple(aspoPlaceHolder, cidocCoords.prefix + 'P107_has_current_or_former_member', socioid) + closeLine
output.write(line)
line = triple(socioid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
output.write(line)
else:
socionoid = '"
line = triple(aspoPlaceHolder, cidocCoords.prefix + 'P107_has_current_or_former_member', socionoid) + closeLine
output.write(line)
line = triple(socionoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
output.write(line)
line = triple(socionoid,
rdfsCoords.prefix + 'label',
'\"' + row['intestatario principale: nome o ID se lo abbiamo'] + '\"') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E21_Person') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
personCoords.prefix + 'Person') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
foafCoords.prefix + 'person') + closeLine
output.write(line)
if row['socio 2'] != '':
E13placeHolder = '"
line = triple(E13placeHolder,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
output.write(line)
line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
output.write(line)
line = triple(E13placeHolder,
rdfsCoords.prefix + 'label',
'\"Relazione: ' + row['socio 2'] + ' socio del gruppo ' + row['recordId'] + '\"') + closeLine
output.write(line)
E55placeHolder = ''
line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
output.write(line)
line = triple(E55placeHolder, rdfsCoords.prefix + 'label', '\"Socio\"') + closeLine
output.write(line)
id = row['socio 2']
if re.match(r'IT-ASPO', id):
socioid = '"
line = triple(aspoPlaceHolder, cidocCoords.prefix + 'P107_has_current_or_former_member', socioid) + closeLine
output.write(line)
line = triple(socioid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
output.write(line)
else:
socionoid = '"
line = triple(aspoPlaceHolder, cidocCoords.prefix + 'P107_has_current_or_former_member', socionoid) + closeLine
output.write(line)
line = triple(socionoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
output.write(line)
line = triple(socionoid,
rdfsCoords.prefix + 'label',
'\"' + row['socio 2'] + '\"') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E21_Person') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
personCoords.prefix + 'Person') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
foafCoords.prefix + 'person') + closeLine
output.write(line)
E13placeHolder = '"
line = triple(E13placeHolder,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
output.write(line)
line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
output.write(line)
line = triple(E13placeHolder,
rdfsCoords.prefix + 'label',
'\"Relazione: ' + row['socio 4'] + ' socio del gruppo ' + row['recordId'] + '\"') + closeLine
output.write(line)
E55placeHolder = ''
line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
output.write(line)
line = triple(E55placeHolder, rdfsCoords.prefix + 'label', '\"Socio\"') + closeLine
output.write(line)
id = row['socio 4']
if re.match(r'IT-ASPO', id):
socioid = '"
line = triple(aspoPlaceHolder, cidocCoords.prefix + 'P107_has_current_or_former_member', socioid) + closeLine
output.write(line)
line = triple(socioid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
output.write(line)
else:
socionoid = '"
line = triple(aspoPlaceHolder, cidocCoords.prefix + 'P107_has_current_or_former_member', socionoid) + closeLine
output.write(line)
line = triple(socionoid, cidocCoords.prefix + 'P141_assigned', E13placeHolder) + closeLine
output.write(line)
line = triple(socionoid,
rdfsCoords.prefix + 'label',
'\"' + row['socio 4'] + '\"') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E21_Person') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
personCoords.prefix + 'Person') + closeLine
output.write(line)
line = triple(socionoid,
nsCoords.prefix + 'type',
foafCoords.prefix + 'person') + closeLine
output.write(line)
if row['sede operativa'] != '':
if row['id sede operativa'] != '':
E13placeHolder = '"
line = triple(E13placeHolder,
nsCoords.prefix + 'type',
cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
output.write(line)
line = triple(E13placeHolder, cidocCoords.prefix + 'P141_assigned', aspoPlaceHolder) + closeLine
output.write(line)
line = triple(E13placeHolder,
rdfsCoords.prefix + 'label',
'\" ' + row['sede operativa'] + ' sede operativa del gruppo ' + row['recordId'] + '\"') + closeLine
output.write(line)
E55placeHolder = ''
line = triple(E13placeHolder, cidocCoords.prefix + 'P42_assigned', E55placeHolder) + closeLine
output.write(line)
line = triple(E55placeHolder, rdfsCoords.prefix + 'label', '\"Sede operativa\"') + closeLine
output.write(line)
e53placeHolder = ""
line = triple(aspoPlaceHolder,
cidocCoords.prefix + 'P74_has_current_or_former_residence',
e53placeHolder) + closeLine
output.write(line)
output.write('\n')
#
# Limit number of entries processed (if desired)
if (ii > max_entries):
break