Browse Source

Add parser CSV_to_RDF MPP Datini, Ospedale, Martini

Federica 2 years ago
parent
commit
ca67122a5b

+ 226 - 0
Museo/CSV_to_RDF/CSV_to_RDF_MPP_datini.ipynb

@@ -0,0 +1,226 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 29,
+   "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",
+    "\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": 30,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/CSV/MPP/Datini/'\n",
+    "export_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/RDF/MPP/Datini/'"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 31,
+   "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('<http://www.museodipalazzopretorio/>', 'dt:')\n",
+    "# Added by FS\n",
+    "personAuthCoords = RDFcoords('<http://www.museodipalazzopretorio/>', 'pa:')\n",
+    "# W3/CIDOC Predicates\n",
+    "hasTypeCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', 'tp:')\n",
+    "carriesCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P128_carries>', 'ca:')\n",
+    "identifiedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by>', 'ib:')\n",
+    "labelCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#label>', 'lb:')\n",
+    "# Added by FS CIDOC properties for person\n",
+    "wasBroughtCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P92i_was_brought_into_existence_by>', 'wb:')\n",
+    "carriedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P14_carried_out_by>', 'cb:')\n",
+    "\n",
+    "# CIDOC Objects\n",
+    "manMadeObjectCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E22_Man-Made_Object>', 'mo:', 'E22')\n",
+    "informationObjectCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E73_Information_Object>', 'io:', 'E73')\n",
+    "titleCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E35_Title>', 'ti:' ,'E35')\n",
+    "placeAppellationCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E44_Place_appellation>', 'pa:', 'E44')\n",
+    "identifierCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E42_Identifier>', 'id:', 'E42')\n",
+    "# Added by FS CIDOC entity\n",
+    "creationCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E65_Creation>', 'cr:', 'E65')\n",
+    "personCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E21_Person>', 'ps:', 'E21')\n",
+    "crmCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 32,
+   "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": 33,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def writeTTLHeader(output):\n",
+    "    output.write('@prefix ' + datiniCoords.prefix + ' ' + datiniCoords.uri + closeLine)\n",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + personAuthCoords.prefix + ' ' + personAuthCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + hasTypeCoords.prefix + ' ' + hasTypeCoords.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",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + wasBroughtCoords.prefix + ' ' + wasBroughtCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + carriedByCoords.prefix + ' ' + carriedByCoords.uri + closeLine)\n",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + crmCoords.prefix + ' ' + crmCoords.uri + closeLine)\n",
+    "    output.write('\\n')\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 34,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "filePrefix = 'SR20OA_'\n",
+    "fileType = 'Datini'\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",
+    "        #\n",
+    "        # Triplify the 'codice' -- should exist for every entry\n",
+    "        codice = ''\n",
+    "        if(row['NCTR'] != '' and row['NCTN'] != ''):\n",
+    "            codice = row['NCTR'] + row['NCTN']\n",
+    "        if(codice != ''):\n",
+    "            line = triple(datiniCoords.prefix + codice, identifiedByCoords.prefix, datiniCoords.prefix + '_' + identifierCoords.code + '_' + codice) + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple(datiniCoords.prefix + '_' + identifierCoords.code + '_' + codice, hasTypeCoords.prefix, identifierCoords.prefix) + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple(datiniCoords.prefix + '_' + identifierCoords.code + '_' + codice, labelCoords.prefix, '\\\"' + codice + '\\\"') + closeLine\n",
+    "            output.write(line)\n",
+    "\n",
+    "        # Write E22 Man Made Object & E73 Information Object -- should exist for every entry?\n",
+    "        line = triple(datiniCoords.prefix + codice, hasTypeCoords.prefix, manMadeObjectCoords.prefix) + closeLine\n",
+    "        output.write(line)\n",
+    "        line = triple(datiniCoords.prefix + codice, carriesCoords.prefix, datiniCoords.prefix + '_' + informationObjectCoords.code + '_' + codice) + closeLine\n",
+    "        output.write(line)\n",
+    "        line = triple(datiniCoords.prefix + '_' + informationObjectCoords.code + '_' + codice, hasTypeCoords.prefix, informationObjectCoords.prefix) + closeLine\n",
+    "        output.write(line)\n",
+    "\n",
+    "        #\n",
+    "        # If the 'titolo_aspo' property is not empty for the given entry, write down title-related triples\n",
+    "        if(row['SGTT'] != 'None'):\n",
+    "            line = triple(datiniCoords.prefix + '_' + informationObjectCoords.code + '_' + codice, identifiedByCoords.prefix, datiniCoords.prefix + '_' + titleCoords.code + '_' + codice) + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple(datiniCoords.prefix + '_' + titleCoords.code + '_' + codice, hasTypeCoords.prefix, titleCoords.prefix) + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple(datiniCoords.prefix + '_' + titleCoords.code + '_' + codice, labelCoords.prefix, '\"' + row['SGTT'] + '\"') + closeLine\n",
+    "            output.write(line)\n",
+    "        \n",
+    "\n",
+    "        output.write('\\n')\n",
+    "        #\n",
+    "        #\n",
+    "        # Limit number of entries processed (if desired)\n",
+    "        if(ii>max_entries):\n",
+    "            break\n",
+    "        "
+   ]
+  }
+ ],
+ "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
+}

+ 235 - 0
Museo/CSV_to_RDF/CSV_to_RDF_MPP_martini.ipynb

@@ -0,0 +1,235 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 55,
+   "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",
+    "\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": 56,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/CSV/MPP/Martini/'\n",
+    "export_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/RDF/MPP/Martini/'"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 57,
+   "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('<http://www.museodipalazzopretorio/>', 'dt:')\n",
+    "# Added by FS\n",
+    "personAuthCoords = RDFcoords('<http://www.museodipalazzopretorio/>', 'pa:')\n",
+    "# W3/CIDOC Predicates\n",
+    "hasTypeCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', 'tp:')\n",
+    "carriesCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P128_carries>', 'ca:')\n",
+    "identifiedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by>', 'ib:')\n",
+    "labelCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#label>', 'lb:')\n",
+    "# Added by FS CIDOC properties for person\n",
+    "wasBroughtCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P92i_was_brought_into_existence_by>', 'wb:')\n",
+    "carriedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P14_carried_out_by>', 'cb:')\n",
+    "\n",
+    "# CIDOC Objects\n",
+    "manMadeObjectCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E22_Man-Made_Object>', 'mo:', 'E22')\n",
+    "informationObjectCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E73_Information_Object>', 'io:', 'E73')\n",
+    "titleCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E35_Title>', 'ti:' ,'E35')\n",
+    "placeAppellationCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E44_Place_appellation>', 'pa:', 'E44')\n",
+    "identifierCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E42_Identifier>', 'id:', 'E42')\n",
+    "# Added by FS CIDOC entity\n",
+    "creationCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E65_Creation>', 'cr:', 'E65')\n",
+    "personCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E21_Person>', 'ps:', 'E21')\n",
+    "crmCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 58,
+   "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": 59,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def writeTTLHeader(output):\n",
+    "    output.write('@prefix ' + datiniCoords.prefix + ' ' + datiniCoords.uri + closeLine)\n",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + personAuthCoords.prefix + ' ' + personAuthCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + hasTypeCoords.prefix + ' ' + hasTypeCoords.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",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + wasBroughtCoords.prefix + ' ' + wasBroughtCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + carriedByCoords.prefix + ' ' + carriedByCoords.uri + closeLine)\n",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + crmCoords.prefix + ' ' + crmCoords.uri + closeLine)\n",
+    "    output.write('\\n')\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 60,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "filePrefix = 'SR20OA_'\n",
+    "fileType = 'Martini'\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",
+    "        \n",
+    "        # Triplify the 'codice' -- should exist for every entry\n",
+    "        codice = ''\n",
+    "        if(row['NCTR'] != '' and row['NCTN'] != ''):\n",
+    "            codice = row['NCTR'] + row['NCTN']\n",
+    "\n",
+    "        if(codice != ''):\n",
+    "            if(row['RVEL'] == ' '):\n",
+    "                line = triple(datiniCoords.prefix + identifierCoords.code + '_' + codice, labelCoords.prefix, '\\\"' + codice + '\\\"') + closeLine\n",
+    "            else:\n",
+    "                codice = codice + \"-\" + row['RVEL']\n",
+    "                line = triple('<http://www.museodipalazzopretorio/' + identifierCoords.code + '_' + codice, labelCoords.prefix, '\\\"' + codice + '\\\"') + closeLine\n",
+    "                ll = '<http://www.museodipalazzopretorio/E42_' + codice + '>'\n",
+    "                pp = str(ll)\n",
+    "                line = triple(pp, labelCoords.prefix, '\\\"' + codice + '\\\"') + closeLine\n",
+    "            output.write(line)\n",
+    "\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + codice + '>', identifiedByCoords.prefix, '<http://www.museodipalazzopretorio/' + identifierCoords.code + '_' + codice + '>') + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + identifierCoords.code + '_' + codice + '>', hasTypeCoords.prefix, identifierCoords.prefix) + closeLine\n",
+    "            output.write(line)\n",
+    "            \n",
+    "        # Write E22 Man Made Object & E73 Information Object -- should exist for every entry?\n",
+    "        line = triple('<http://www.museodipalazzopretorio/' + codice + '>', hasTypeCoords.prefix, manMadeObjectCoords.prefix) + closeLine\n",
+    "        output.write(line)\n",
+    "        line = triple('<http://www.museodipalazzopretorio/' + codice + '>', carriesCoords.prefix, '<http://www.museodipalazzopretorio/' + informationObjectCoords.code + '_' + codice + '>') + closeLine\n",
+    "        output.write(line)\n",
+    "        line = triple('<http://www.museodipalazzopretorio/' + informationObjectCoords.code + '_' + codice + '>', hasTypeCoords.prefix, informationObjectCoords.prefix) + closeLine\n",
+    "        output.write(line)\n",
+    "\n",
+    "        #\n",
+    "        # If the 'titolo' property is not empty for the given entry, write down title-related triples\n",
+    "        if(row['SGTT'] != ' '):\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + informationObjectCoords.code + '_' + codice + '>', identifiedByCoords.prefix, '<http://www.museodipalazzopretorio/' + titleCoords.code + '_' + codice + '>') + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + titleCoords.code + '_' + codice + '>', hasTypeCoords.prefix, titleCoords.prefix) + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + titleCoords.code + '_' + codice + '>', labelCoords.prefix, '\"' + row['SGTT'] + '\"') + closeLine\n",
+    "            output.write(line)\n",
+    "        #\n",
+    "\n",
+    "        output.write('\\n')\n",
+    "        #\n",
+    "        #\n",
+    "        # Limit number of entries processed (if desired)\n",
+    "        if(ii>max_entries):\n",
+    "            break\n",
+    "        "
+   ]
+  }
+ ],
+ "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
+}

+ 232 - 0
Museo/CSV_to_RDF/CSV_to_RDF_MPP_ospedale.ipynb

@@ -0,0 +1,232 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 79,
+   "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",
+    "\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": 80,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/CSV/MPP/Ospedale/'\n",
+    "export_dir = '/Users/federicaspinelli/Google Drive/OVI:CNR/RDF/MPP/Ospedale/'"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 81,
+   "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('<http://www.museodipalazzopretorio/>', 'dt:')\n",
+    "# Added by FS\n",
+    "personAuthCoords = RDFcoords('<http://www.museodipalazzopretorio/>', 'pa:')\n",
+    "# W3/CIDOC Predicates\n",
+    "hasTypeCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', 'tp:')\n",
+    "carriesCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P128_carries>', 'ca:')\n",
+    "identifiedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by>', 'ib:')\n",
+    "labelCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#label>', 'lb:')\n",
+    "# Added by FS CIDOC properties for person\n",
+    "wasBroughtCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P92i_was_brought_into_existence_by>', 'wb:')\n",
+    "carriedByCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/P14_carried_out_by>', 'cb:')\n",
+    "\n",
+    "# CIDOC Objects\n",
+    "manMadeObjectCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E22_Man-Made_Object>', 'mo:', 'E22')\n",
+    "informationObjectCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E73_Information_Object>', 'io:', 'E73')\n",
+    "titleCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E35_Title>', 'ti:' ,'E35')\n",
+    "placeAppellationCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E44_Place_appellation>', 'pa:', 'E44')\n",
+    "identifierCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E42_Identifier>', 'id:', 'E42')\n",
+    "# Added by FS CIDOC entity\n",
+    "creationCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E65_Creation>', 'cr:', 'E65')\n",
+    "personCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/E21_Person>', 'ps:', 'E21')\n",
+    "crmCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 82,
+   "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": 83,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def writeTTLHeader(output):\n",
+    "    output.write('@prefix ' + datiniCoords.prefix + ' ' + datiniCoords.uri + closeLine)\n",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + personAuthCoords.prefix + ' ' + personAuthCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + hasTypeCoords.prefix + ' ' + hasTypeCoords.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",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + wasBroughtCoords.prefix + ' ' + wasBroughtCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + carriedByCoords.prefix + ' ' + carriedByCoords.uri + closeLine)\n",
+    "    # Added by FS\n",
+    "    output.write('@prefix ' + personCoords.prefix + ' ' + personCoords.uri + closeLine)\n",
+    "    output.write('@prefix ' + crmCoords.prefix + ' ' + crmCoords.uri + closeLine)\n",
+    "    output.write('\\n')\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 84,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "filePrefix = 'SR20OA_'\n",
+    "fileType = 'Ospedale'\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",
+    "        \n",
+    "        # Triplify the 'codice' -- should exist for every entry\n",
+    "        codice = ''\n",
+    "        if(row['NCTR'] != '' and row['NCTN'] != ''):\n",
+    "            codice = row['NCTR'] + row['NCTN']\n",
+    "\n",
+    "        if(codice != ''):\n",
+    "            if(row['RVEL'] == ' '):\n",
+    "                line = triple(datiniCoords.prefix + identifierCoords.code + '_' + codice, labelCoords.prefix, '\\\"' + codice + '\\\"') + closeLine\n",
+    "            else:\n",
+    "                codice = codice + \"_\" + row['RVEL']\n",
+    "                ll = '<http://www.museodipalazzopretorio/E42_' + codice + '>'\n",
+    "                pp = str(ll)\n",
+    "                line = triple(pp, labelCoords.prefix, '\\\"' + codice + '\\\"') + closeLine\n",
+    "            output.write(line)\n",
+    "            \n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + codice + '>', identifiedByCoords.prefix, '<http://www.museodipalazzopretorio/' + identifierCoords.code + '_' + codice + '>') + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + identifierCoords.code + '_' + codice + '>', hasTypeCoords.prefix, identifierCoords.prefix) + closeLine\n",
+    "            output.write(line)\n",
+    "        # Write E22 Man Made Object & E73 Information Object -- should exist for every entry?\n",
+    "        line = triple('<http://www.museodipalazzopretorio/' + codice + '>', hasTypeCoords.prefix, manMadeObjectCoords.prefix) + closeLine\n",
+    "        output.write(line)\n",
+    "        line = triple('<http://www.museodipalazzopretorio/' + codice + '>', carriesCoords.prefix, '<http://www.museodipalazzopretorio/' + informationObjectCoords.code + '_' + codice + '>') + closeLine\n",
+    "        output.write(line)\n",
+    "        line = triple('<http://www.museodipalazzopretorio/' + informationObjectCoords.code + '_' + codice + '>', hasTypeCoords.prefix, informationObjectCoords.prefix) + closeLine\n",
+    "        output.write(line)\n",
+    "\n",
+    "        #\n",
+    "        # If the 'titolo_aspo' property is not empty for the given entry, write down title-related triples\n",
+    "        if(row['SGTT'] != ' '):\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + informationObjectCoords.code + '_' + codice + '>', identifiedByCoords.prefix, '<http://www.museodipalazzopretorio/' + titleCoords.code + '_' + codice + '>') + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + titleCoords.code + '_' + codice + '>', hasTypeCoords.prefix, titleCoords.prefix) + closeLine\n",
+    "            output.write(line)\n",
+    "            line = triple('<http://www.museodipalazzopretorio/' + titleCoords.code + '_' + codice + '>', labelCoords.prefix, '\"' + row['SGTT'] + '\"') + 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": {
+  "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
+}