{
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"source": [
"# Utilities to read/write csv files\n",
"import csv\n",
"# Utilities to handle character encodings\n",
"import unicodedata\n",
"# Ordered Dicts\n",
"from collections import OrderedDict\n",
"\n",
"import json\n",
"\n",
"\n",
"# OPZIONAL IMPORTS\n",
"\n",
"# For timestamping/simple speed tests\n",
"from datetime import datetime\n",
"# Random number generator\n",
"from random import *\n",
"# System & command line utilities\n",
"import sys\n",
"# Json for the dictionary\n",
"import json"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 20,
"source": [
"import_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/CSV/ASPO/datini/'\n",
"export_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/RDF/ASPO/datini/'"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 21,
"source": [
"# Custom class to store URIs + related infos for the ontologies/repositories\n",
"\n",
"class RDFcoords:\n",
" def __init__(self, uri, prefix, code = None):\n",
" self.uri = uri\n",
" self.prefix = prefix\n",
" self.code = code\n",
"\n",
"\n",
"# Repositories\n",
"datiniCoords = RDFcoords('', 'dt:')\n",
"# W3/CIDOC Predicates\n",
"hasTypeCoords = RDFcoords('', 'tp:')\n",
"hasTypePCoords = RDFcoords('', 'te:')\n",
"carriesCoords = RDFcoords('', 'ca:')\n",
"identifiedByCoords = RDFcoords('', 'ib:')\n",
"labelCoords = RDFcoords('', 'lb:')\n",
"\n",
"# CIDOC Objects\n",
"manMadeObjectCoords = RDFcoords('', 'mo:', 'E22')\n",
"informationObjectCoords = RDFcoords('', 'io:', 'E73')\n",
"titleCoords = RDFcoords('', 'ti:' ,'E35')\n",
"placeAppellationCoords = RDFcoords('', 'pa:', 'E44')\n",
"identifierCoords = RDFcoords('', 'id:', 'E42')\n",
"typeCoords = RDFcoords('', 'ty:', 'E55')"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 22,
"source": [
"# Basic functions for triples / shortened triples in TTL format\n",
"\n",
"def triple(subject, predicate, object1):\n",
" line = subject + ' ' + predicate + ' ' + object1\n",
" return line\n",
"\n",
"def doublet(predicate, object1):\n",
" line = ' ' + predicate + ' ' + object1\n",
" return line\n",
"\n",
"def singlet(object1):\n",
" line = ' ' + object1\n",
" return line\n",
"\n",
"# Line endings in TTL format\n",
"continueLine1 = ' ;\\n'\n",
"continueLine2 = ' ,\\n'\n",
"closeLine = ' .\\n'"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 23,
"source": [
"def writeTTLHeader(output):\n",
" output.write('@prefix ' + datiniCoords.prefix + ' ' + datiniCoords.uri + closeLine)\n",
" output.write('@prefix ' + hasTypeCoords.prefix + ' ' + hasTypeCoords.uri + closeLine)\n",
" output.write('@prefix ' + hasTypePCoords.prefix + ' ' + hasTypePCoords.uri + closeLine)\n",
" output.write('@prefix ' + manMadeObjectCoords.prefix + ' ' + manMadeObjectCoords.uri + closeLine)\n",
" output.write('@prefix ' + carriesCoords.prefix + ' ' + carriesCoords.uri + closeLine)\n",
" output.write('@prefix ' + informationObjectCoords.prefix + ' ' + informationObjectCoords.uri + closeLine)\n",
" output.write('@prefix ' + identifiedByCoords.prefix + ' ' + identifiedByCoords.uri + closeLine)\n",
" output.write('@prefix ' + titleCoords.prefix + ' ' + titleCoords.uri + closeLine)\n",
" output.write('@prefix ' + labelCoords.prefix + ' ' + labelCoords.uri + closeLine)\n",
" output.write('@prefix ' + identifierCoords.prefix + ' ' + identifierCoords.uri + closeLine)\n",
" output.write('@prefix ' + typeCoords.prefix + ' ' + typeCoords.uri + closeLine)\n",
" output.write('\\n')\n"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 24,
"source": [
"filePrefix = 'data_'\n",
"fileType = 'subfonds'\n",
"max_entries = 1000000000\n",
"\n",
"with open(import_dir + filePrefix + fileType + '.csv', newline=\"\") as csv_file, open(export_dir + filePrefix + fileType + '.ttl', 'w') as output:\n",
" reader = csv.DictReader(csv_file)\n",
" writeTTLHeader(output)\n",
" first = True\n",
" ii = 0\n",
" for row in reader:\n",
" # The index ii is used to process a limited number of entries for testing purposes\n",
" ii = ii+1\n",
" # Skip the first line as it carries info we don't want to triplify\n",
" if(first):\n",
" first = False\n",
" continue\n",
" # Write E22 Man Made Object & E73 Information Object -- should exist for every entry?\n",
" line = triple(datiniCoords.prefix + row['id'], hasTypeCoords.prefix, manMadeObjectCoords.prefix) + closeLine\n",
" output.write(line)\n",
" line = triple(datiniCoords.prefix + row['id'], labelCoords.prefix, '\\\"Documento fisico: ' + row['titolo_aspo'].replace('\\\\','\\\\\\\\').replace('\"','\\\\\"')+ '\\\"') + closeLine\n",
" output.write(line)\n",
" e37placeHolder = \"\"\n",
" line = triple(datiniCoords.prefix + row['id'], carriesCoords.prefix, e37placeHolder) + closeLine\n",
" output.write(line)\n",
" line = triple(e37placeHolder, hasTypeCoords.prefix, informationObjectCoords.prefix) + closeLine\n",
" output.write(line)\n",
" line = triple(e37placeHolder, labelCoords.prefix, '\\\"Contenuto informativo: ' + row['titolo_aspo'].replace('\\\\','\\\\\\\\').replace('\"','\\\\\"')+ '\\\"') + closeLine\n",
" output.write(line)\n",
" #\n",
" # If the 'titolo_aspo' property is not empty for the given entry, write down title-related triples\n",
" if(row['titolo_aspo'] != 'None'):\n",
" e35placeHolder1 = \"\"\n",
" line = triple(e37placeHolder, identifiedByCoords.prefix, e35placeHolder1) + closeLine\n",
" output.write(line)\n",
" line = triple(e35placeHolder1, hasTypeCoords.prefix, titleCoords.prefix) + closeLine\n",
" output.write(line)\n",
" line = triple(e35placeHolder1, labelCoords.prefix, '\\\"' + row['titolo_aspo'].replace('\\\\','\\\\\\\\').replace('\"','\\\\\"')+ '\\\"') + closeLine\n",
" output.write(line)\n",
" \n",
" output.write('\\n')\n",
" #\n",
" #\n",
" # Limit number of entries processed (if desired)\n",
" if(ii>max_entries):\n",
" break\n",
" "
],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.3 64-bit"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"metadata": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
},
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}