{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Utilities to read/write csv files\n", "import csv\n", "import unicodedata\n", "# Ordered Dicts\n", "from collections import OrderedDict\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\n", "import re" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/OVI/CSV/'\n", "export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/OVI/DATE/'" ] }, { "cell_type": "code", "execution_count": 9, "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 Predicates\n", "hasTypeCoords = RDFcoords('', 'tp:')\n", "labelCoords = RDFcoords('', 'lb:')\n", "# CIDOC Predicates\n", "identifiedByCoords = RDFcoords('', 'ib:')\n", "hasTypePCoords = RDFcoords('', 'te:')\n", "hasTimeSpanCoords = RDFcoords('', 'hs:')\n", "tookPlaceCoords = RDFcoords('', 'tk:')\n", "carriedByCoords = RDFcoords('', 'cb:')\n", "movedByCoords = RDFcoords('', 'mb:')\n", "movedToCoords = RDFcoords('', 'mt:')\n", "movedFromCoords = RDFcoords('', 'mf:')\n", "wasBroughtCoords = RDFcoords('', 'wb:')\n", "hasProducedCoords = RDFcoords('', 'hp:')\n", "wasProducedCoords = RDFcoords('', 'wp:')\n", "carriesCoords = RDFcoords('', 'ca:')\n", "hasAlternativeFormCoords = RDFcoords('', 'af:')\n", "onGoingTCoords = RDFcoords('', 'gt:')\n", "nsCoords = RDFcoords('', 'rdf:')\n", "schemaCoords = RDFcoords('', 'rdfs:')\n", "# CIDOC Objects\n", "moveCoords = RDFcoords('', 'mv:', 'E9')\n", "productionCoords = RDFcoords('', 'pr:', 'E12')\n", "personCoords = RDFcoords('', 'ps:', 'E21')\n", "manMadeObjectCoords = RDFcoords('', 'mo:', 'E22')\n", "titleCoords = RDFcoords('', 'ti:' ,'E35')\n", "identifierCoords = RDFcoords('', 'id:', 'E42')\n", "#placeAppellationCoords = RDFcoords('', 'pa:', 'E44')\n", "timeSpanCoords = RDFcoords('', 'ts:', 'E52')\n", "placeCoords = RDFcoords('', 'pl:', 'E53')\n", "typeCoords = RDFcoords('', 'ty:', 'E55')\n", "creationCoords = RDFcoords('', 'cr:', 'E65')\n", "informationObjectCoords = RDFcoords('', 'io:', 'E73')\n", "# New classes (subclasses of E7 Activity) - Exchange, Sending, Recive Letters\n", "exchangeLettersCoords = RDFcoords('', 'el:', 'EL1')\n", "sendLetterCoords = RDFcoords('', 'sl:', 'EL2')\n", "receiveLetterCoords = RDFcoords('', 'rl:', 'EL3')\n", "xsdCoords = RDFcoords('', 'xsd:')\n", "cidocCoords = RDFcoords('', 'crm:')\n", "yearCoords = RDFcoords('', 'year:')\n", "monthCoords = RDFcoords('', 'month:')\n", "dayCoords = RDFcoords('', 'day:')\n", "beginningCoords = RDFcoords('', 'beg:')\n", "endCoords = RDFcoords('', 'end:')" ] }, { "cell_type": "code", "execution_count": 10, "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": 11, "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 ' + labelCoords.prefix + ' ' + labelCoords.uri + closeLine)\n", " output.write('@prefix ' + identifiedByCoords.prefix + ' ' + identifiedByCoords.uri + closeLine)\n", " output.write('@prefix ' + hasTypePCoords.prefix + ' ' + hasTypePCoords.uri + closeLine)\n", " output.write('@prefix ' + hasTimeSpanCoords.prefix + ' ' + hasTimeSpanCoords.uri + closeLine)\n", " output.write('@prefix ' + tookPlaceCoords.prefix + ' ' + tookPlaceCoords.uri + closeLine) \n", " output.write('@prefix ' + carriedByCoords.prefix + ' ' + carriedByCoords.uri + closeLine)\n", " output.write('@prefix ' + movedByCoords.prefix + ' ' + movedByCoords.uri + closeLine)\n", " output.write('@prefix ' + movedToCoords.prefix + ' ' + movedToCoords.uri + closeLine)\n", " output.write('@prefix ' + movedFromCoords.prefix + ' ' + movedFromCoords.uri + closeLine) \n", " output.write('@prefix ' + wasBroughtCoords.prefix + ' ' + wasBroughtCoords.uri + closeLine)\n", " output.write('@prefix ' + hasProducedCoords.prefix + ' ' + hasProducedCoords.uri + closeLine)\n", " output.write('@prefix ' + wasProducedCoords.prefix + ' ' + wasProducedCoords.uri + closeLine)\n", " output.write('@prefix ' + carriesCoords.prefix + ' ' + carriesCoords.uri + closeLine)\n", " output.write('@prefix ' + hasAlternativeFormCoords.prefix + ' ' + hasAlternativeFormCoords.uri + closeLine)\n", " output.write('@prefix ' + moveCoords.prefix + ' ' + moveCoords.uri + closeLine) \n", " output.write('@prefix ' + productionCoords.prefix + ' ' + productionCoords.uri + closeLine)\n", " output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)\n", " output.write('@prefix ' + manMadeObjectCoords.prefix + ' ' + manMadeObjectCoords.uri + closeLine)\n", " output.write('@prefix ' + titleCoords.prefix + ' ' + titleCoords.uri + closeLine)\n", " output.write('@prefix ' + identifierCoords.prefix + ' ' + identifierCoords.uri + closeLine)\n", " output.write('@prefix ' + timeSpanCoords.prefix + ' ' + timeSpanCoords.uri + closeLine)\n", " output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)\n", " output.write('@prefix ' + typeCoords.prefix + ' ' + typeCoords.uri + closeLine)\n", " output.write('@prefix ' + creationCoords.prefix + ' ' + creationCoords.uri + closeLine)\n", " output.write('@prefix ' + informationObjectCoords.prefix + ' ' + informationObjectCoords.uri + closeLine)\n", " output.write('@prefix ' + exchangeLettersCoords.prefix + ' ' + exchangeLettersCoords.uri + closeLine)\n", " output.write('@prefix ' + sendLetterCoords.prefix + ' ' + sendLetterCoords.uri + closeLine)\n", " output.write('@prefix ' + receiveLetterCoords.prefix + ' ' + receiveLetterCoords.uri + closeLine)\n", " output.write('@prefix ' + onGoingTCoords.prefix + ' ' + onGoingTCoords.uri + closeLine)\n", " output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)\n", " output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)\n", " output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)\n", " output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)\n", " output.write('@prefix ' + beginningCoords.prefix + ' ' + beginningCoords.uri + closeLine)\n", " output.write('@prefix ' + endCoords.prefix + ' ' + endCoords.uri + closeLine)\n", " output.write('@prefix ' + yearCoords.prefix + ' ' + yearCoords.uri + closeLine) \n", " output.write('@prefix ' + monthCoords.prefix + ' ' + monthCoords.uri + closeLine)\n", " output.write('@prefix ' + dayCoords.prefix + ' ' + dayCoords.uri + closeLine)\n", " output.write('\\n')\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [] }, "outputs": [], "source": [ "filePrefix = 'Biblio'\n", "fileType = 'Datini'\n", "max_entries = 1000000000\n", "\n", "with open(import_dir + filePrefix + fileType + '_Date.csv', newline=\"\") as csv_file, open(export_dir + filePrefix + fileType + '_event_exchange_date_normalization.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", " \n", " #Evento send letter\n", " el2placeHolder = \"\"\n", " el3placeHolder = \"\"\n", "\n", " \n", " e52PplaceHolder = \"\"\n", " e52AplaceHolder = \"\"\n", " \n", " # Data invio \n", " # if(row['anno_inizio'] != '' and len(row['anno_inizio'])== 4 and row['anno_inizio'] != '****'):\n", " # line = triple(e52PplaceHolder, yearCoords.prefix, '\\\"'+row['anno_inizio'] +'\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # if (len(row['data_partenza_mese']) == 2 and row['data_partenza_mese'] != '**'):\n", " # mese = row['data_partenza_mese']\n", " # line = triple(e52PplaceHolder, monthCoords.prefix, '\\\"'+row['data_partenza_mese'] + '\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # else:\n", " # mese = '01'\n", " # line = triple(e52PplaceHolder, monthCoords.prefix, '\\\"01\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # if (len(row['data_partenza_giorno']) == 2 and row['data_partenza_giorno'] != '**'):\n", " # giorno = row['data_partenza_giorno']\n", " # line = triple(e52PplaceHolder, dayCoords.prefix, '\\\"'+row['data_partenza_giorno'] + '\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # else:\n", " # giorno = '01'\n", " # line = triple(e52PplaceHolder, dayCoords.prefix, '\\\"01\\\"^^xsd:integer') + closeLine\n", " # output.write(line) \n", " # else:\n", " # line = triple(e52PplaceHolder, cidocCoords.prefix + 'P3_has_note', '\\\"Data incompleta\\\"') + closeLine\n", " # output.write(line)\n", " \n", " if(row['anno_inizio'] != '' and len(row['anno_inizio'])== 4 and row['anno_inizio'] != '****'): \n", " anno = row['anno_inizio'] \n", " # if (len(row['data_partenza_mese']) == 2 and row['data_partenza_mese'] != '**' and row['data_partenza_mese'] != ''):\n", " # mese = row['data_partenza_mese']\n", " # else:\n", " # mese = '01'\n", " # if (len(row['data_partenza_giorno']) == 2 and row['data_partenza_giorno'] != '**' and row['data_partenza_giorno'] != ''):\n", " # giorno = row['data_partenza_giorno']\n", " # else:\n", " # giorno = '01'\n", "\n", " # line = triple(e52PplaceHolder, beginningCoords.prefix, '\\\"'+anno+mese+giorno+'\\\"^^xsd:date') + closeLine\n", " # output.write(line)\n", " line = triple(e52PplaceHolder, endCoords.prefix, '\\\"'+anno+'12'+'31'+'\\\"^^xsd:date') + closeLine\n", " output.write(line) \n", " \n", " # Data arrivo\n", " # if(row['anno_fine'] != '' and len(row['anno_fine'])== 4 and row['anno_fine'] != '****'):\n", " # line = triple(e52AplaceHolder, yearCoords.prefix, '\\\"'+row['anno_fine'] +'\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # if (len(row['data_arrivo_mese']) == 2 and row['data_arrivo_mese'] != '**' and row['data_arrivo_mese'] != ''):\n", " # line = triple(e52AplaceHolder, monthCoords.prefix, '\\\"'+row['data_arrivo_mese'] + '\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # else:\n", " # mese = '12'\n", " # line = triple(e52AplaceHolder, monthCoords.prefix, '\\\"12\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # if (len(row['data_arrivo_giorno']) == 2 and row['data_arrivo_giorno'] != '**' and row['data_arrivo_giorno'] != ''):\n", " # line = triple(e52AplaceHolder, dayCoords.prefix, '\\\"'+row['data_arrivo_giorno'] + '\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # else:\n", " # giorno = '31'\n", " # line = triple(e52AplaceHolder, dayCoords.prefix, '\\\"31\\\"^^xsd:integer') + closeLine\n", " # output.write(line)\n", " # else:\n", " # line = triple(e52AplaceHolder, cidocCoords.prefix + 'P3_has_note', '\\\"Data incompleta\\\"') + closeLine\n", " # output.write(line)\n", "\n", " if(row['anno_fine'] != '' and len(row['anno_fine'])== 4 and row['anno_fine'] != '****'): \n", " anno = row['anno_inizio'] \n", " # if (len(row['data_arrivo_mese']) == 2 and row['data_arrivo_mese'] != '**' and row['data_arrivo_mese'] != ''):\n", " # mese = row['data_arrivo_mese']\n", " # else:\n", " # mese = '12'\n", " # if (len(row['data_arrivo_giorno']) == 2 and row['data_arrivo_giorno'] != '**' and row['data_arrivo_giorno'] != ''):\n", " # giorno = row['data_arrivo_giorno']\n", " # else:\n", " # giorno = '31' \n", " \n", " # line = triple(e52AplaceHolder, endCoords.prefix, '\\\"'+anno+mese+giorno+'\\\"^^xsd:date') + closeLine\n", " # output.write(line)\n", " line = triple(e52AplaceHolder, beginningCoords.prefix, '\\\"'+anno+'01'+'01'+'\\\"^^xsd:date') + 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", " " ] } ], "metadata": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" }, "kernelspec": { "display_name": "Python 3.9.0 64-bit", "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" } } }, "nbformat": 4, "nbformat_minor": 2 }