{ "cells": [ { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "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", "import re\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" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/CSV/ospedale/new/'\n", "export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/RDF/ospedale/new/'" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "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", "personAuthCoords = RDFcoords('', 'pa:')\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", "wasBroughtCoords = RDFcoords('', 'wb:')\n", "carriedByCoords = RDFcoords('', 'cb:')\n", "hasCurrentPermanentLocationCoords = RDFcoords('', 'ap:')\n", "hasCurrentOwnerCoords = RDFcoords('', 'ow:')\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')\n", "creationCoords = RDFcoords('', 'cr:', 'E65')\n", "personCoords = RDFcoords('', 'ps:', 'E21')\n", "placeCoords = RDFcoords('', 'pl:', 'E53')\n", "groupCoords = RDFcoords('', 'gp:', 'E74')\n", "\n", "materialCoords = RDFcoords('', 'mt:', 'E57')\n", "hasTypeNCoords = RDFcoords('', 'tn:')\n", "hasNoteCoords = RDFcoords('', 'no:')\n", "stringCoords = RDFcoords('', 'sr:', 'E62')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "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'" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def writeTTLHeader(output):\n", " output.write('@prefix ' + datiniCoords.prefix + ' ' + datiniCoords.uri + closeLine)\n", " output.write('@prefix ' + personAuthCoords.prefix + ' ' + personAuthCoords.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 ' + wasBroughtCoords.prefix + ' ' + wasBroughtCoords.uri + closeLine)\n", " output.write('@prefix ' + typeCoords.prefix + ' ' + typeCoords.uri + closeLine)\n", " output.write('@prefix ' + carriedByCoords.prefix + ' ' + carriedByCoords.uri + closeLine)\n", " output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)\n", " output.write('@prefix ' + hasCurrentPermanentLocationCoords.prefix + ' ' + hasCurrentPermanentLocationCoords.uri + closeLine)\n", " output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)\n", " output.write('@prefix ' + hasCurrentOwnerCoords.prefix + ' ' + hasCurrentOwnerCoords.uri + closeLine)\n", " output.write('@prefix ' + groupCoords.prefix + ' ' + groupCoords.uri + closeLine)\n", " output.write('@prefix ' + materialCoords.prefix + ' ' + materialCoords.uri + closeLine)\n", " output.write('@prefix ' + hasTypeNCoords.prefix + ' ' + hasTypeNCoords.uri + closeLine)\n", " output.write('@prefix ' + stringCoords.prefix + ' ' + stringCoords.uri + closeLine)\n", " output.write('@prefix ' + hasNoteCoords.prefix + ' ' + hasNoteCoords.uri + closeLine)\n", " output.write('\\n')\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [] }, "outputs": [ { "ename": "KeyError", "evalue": "'segnatura_attuale'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/Users/federicaspinelli/TEAMOVI/Parser/ASPO/CSV_to_RDF/ospedale/CSV_to_RDF_ospedale_all.ipynb Cella 6\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 74\u001b[0m \u001b[39m'''if(row['supporto_note'] != ''):\u001b[39;00m\n\u001b[1;32m 75\u001b[0m \u001b[39m txt = row['supporto_note']\u001b[39;00m\n\u001b[1;32m 76\u001b[0m \u001b[39m x = re.sub(\"\\n\", \" \", txt)\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 90\u001b[0m \u001b[39m line = triple(e55placeHolder, labelCoords.prefix, '\\\"Nota supporto\\\"') + closeLine\u001b[39;00m\n\u001b[1;32m 91\u001b[0m \u001b[39m output.write(line)'''\u001b[39;00m\n\u001b[1;32m 92\u001b[0m segnatura \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m---> 93\u001b[0m \u001b[39mif\u001b[39;00m(row[\u001b[39m'\u001b[39;49m\u001b[39msegnatura_attuale\u001b[39;49m\u001b[39m'\u001b[39;49m] \u001b[39m!=\u001b[39m \u001b[39m'\u001b[39m\u001b[39m'\u001b[39m):\n\u001b[1;32m 94\u001b[0m segnatura \u001b[39m=\u001b[39m row[\u001b[39m'\u001b[39m\u001b[39msegnatura_attuale\u001b[39m\u001b[39m'\u001b[39m]\n\u001b[1;32m 95\u001b[0m \u001b[39mif\u001b[39;00m(segnatura \u001b[39m!=\u001b[39m \u001b[39m'\u001b[39m\u001b[39m'\u001b[39m):\n", "\u001b[0;31mKeyError\u001b[0m: 'segnatura_attuale'" ] } ], "source": [ "filePrefix = 'data_'\n", "# inserire i livelli otherlevel, collection, fonds, recordgrp, series, subfonds, subgrp, subseries\n", "fileType = 'collection'\n", "max_entries = 10000000000000000000000000000000\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", " E74placeHolder = ''\n", " line = triple(E74placeHolder, hasTypeCoords.prefix, groupCoords.prefix ) + closeLine\n", " output.write(line) \n", " line = triple(E74placeHolder, labelCoords.prefix, \"\\\"Archivio di Stato di Prato\\\"\") + closeLine\n", " output.write(line)\n", " currentLocation = \"\\\"Fondo Ospedale della Misericordia e Dolce, Archivio di Stato di Prato, Prato (PO)\\\"\"\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", " # E22 Man Made Object\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), hasTypeCoords.prefix, manMadeObjectCoords.prefix) + closeLine\n", " # output.write(line)\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), labelCoords.prefix, '\\\"' + row['titolo_aspo'].replace('\\\\','\\\\\\\\').replace('\"','\\\\\"')+ '\\\"') + closeLine\n", " # output.write(line)\n", " # # E73 Information Object\n", " # e73placeHolder = \"\"\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), carriesCoords.prefix, e73placeHolder) + closeLine\n", " # output.write(line)\n", " # line = triple(e73placeHolder, hasTypeCoords.prefix, informationObjectCoords.prefix) + closeLine\n", " # output.write(line)\n", " # line = triple(e73placeHolder, labelCoords.prefix, '\\\"' + row['titolo_aspo'].replace('\\\\','\\\\\\\\').replace('\"','\\\\\"')+ '\\\"') + closeLine\n", " # output.write(line)\n", " # # E35 Title\n", " # if(row['titolo_aspo'] != 'None'):\n", " # e35placeHolder1 = \"\"\n", " # line = triple(e73placeHolder, 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", " # # E55 Type\n", " # if(fileType != 'collection' and fileType != 'fonds' and fileType != 'recordgrp' and fileType != 'subgrp' and fileType != 'subfonds' and row['genere'] != ''):\n", " # tipologie = []\n", " # pipe = \"|\" \n", " # if pipe in row['genere']:\n", " # tipologie = row['genere'].split('|')\n", " # e55placeHolder = \"\"\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), hasTypePCoords.prefix, e55placeHolder) + closeLine\n", " # output.write(line)\n", " # else:\n", " # e55placeHolder = \"\"\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), hasTypePCoords.prefix, e55placeHolder) + closeLine\n", " # output.write(line)\n", " # # E42 Identifier\n", " # e42placeHolderID = \"\"\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), identifiedByCoords.prefix, e42placeHolderID) + closeLine\n", " # output.write(line)\n", " # line = triple(e42placeHolderID, hasTypeCoords.prefix, identifierCoords.prefix) + closeLine\n", " # output.write(line)\n", " # line = triple(e42placeHolderID, labelCoords.prefix, '\\\"' + row['id'].replace(\" \", \"\") + '\\\"') + closeLine \n", " # output.write(line)\n", " # # E22 - P52 - E74\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), hasCurrentOwnerCoords.prefix, E74placeHolder) + closeLine\n", " # output.write(line)\n", " # # E22 - P54 - E53\n", " # line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), hasCurrentPermanentLocationCoords.prefix, currentLocation) + closeLine\n", " # output.write(line)\n", "\n", " '''if(row['supporto_note'] != ''):\n", " txt = row['supporto_note']\n", " x = re.sub(\"\\n\", \" \", txt)\n", " y = re.sub(\"\\s\\s\", \"\", x)\n", " occ = re.sub(r'[^A-Za-z]','', y)\n", " y = y.replace(\"\\\"\",\"\")\n", " e62placeHolder = \"\"\n", " line = triple(\"\", hasNoteCoords.prefix, e62placeHolder) + closeLine\n", " output.write(line) \n", " line = triple(e62placeHolder, labelCoords.prefix, '\\\"' + y + '\\\"') + closeLine\n", " output.write(line)\n", " line = triple(e62placeHolder, hasTypeCoords.prefix, stringCoords.prefix) + closeLine\n", " output.write(line)\n", " e55placeHolder =\"\"\n", " line = triple(e62placeHolder, hasTypeNCoords.prefix, e55placeHolder) + closeLine\n", " output.write(line)\n", " line = triple(e55placeHolder, labelCoords.prefix, '\\\"Nota supporto\\\"') + closeLine\n", " output.write(line)'''\n", " segnatura = ''\n", " if(row['segnatura_attuale'] != ''):\n", " segnatura = row['segnatura_attuale']\n", " if(segnatura != ''):\n", " e42placeHolder = \"\"\n", " line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), identifiedByCoords.prefix, e42placeHolder) + closeLine\n", " output.write(line)\n", " line = triple(e42placeHolder, hasTypeCoords.prefix, identifierCoords.prefix) + closeLine\n", " output.write(line)\n", " line = triple(e42placeHolder, labelCoords.prefix, '\\\"Fondo Ospedale della Misericordia e Dolce, ' + segnatura + '\\\"') + closeLine\n", " output.write(line)\n", " line = triple(e42placeHolder, hasTypePCoords.prefix, \"\\\"Segnatura\\\"\") + closeLine\n", " output.write(line)\n", " segnatura_precedente = ''\n", " if(row['segnatura_precedente'] != ''):\n", " segnatura_precedente = row['segnatura_precedente']\n", " if(segnatura_precedente != ''):\n", " e42placeHolder = \"\"\n", " line = triple(datiniCoords.prefix + row['id'].replace('IT-ASPO-ST00005-', ''), identifiedByCoords.prefix, e42placeHolder) + closeLine\n", " output.write(line)\n", " line = triple(e42placeHolder, hasTypeCoords.prefix, identifierCoords.prefix) + closeLine\n", " output.write(line)\n", " line = triple(e42placeHolder, labelCoords.prefix, '\\\"Fondo Ospedale della Misericordia e Dolce, ' + segnatura_precedente + '\\\"') + closeLine\n", " output.write(line)\n", " line = triple(e42placeHolder, hasTypePCoords.prefix, \"\\\"Segnatura precedente\\\"\") + closeLine\n", " output.write(line)\n", " output.write('\\n')\n", " # Limit number of entries processed (if desired)\n", " if(ii>max_entries):\n", " break " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 64-bit", "language": "python", "name": "python3" }, "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.10.4" }, "metadata": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } }, "vscode": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" } } }, "nbformat": 4, "nbformat_minor": 2 }