{
"cells": [
{
"cell_type": "code",
"execution_count": 37,
"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"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/CSV/datini/'\n",
"export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/ASPO/RDF/datini/'"
]
},
{
"cell_type": "code",
"execution_count": 39,
"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",
"hasAlternativeFormCoords = RDFcoords('', 'af:')\n",
"hasCurrentPermanentLocationCoords = RDFcoords('', 'ap:')\n",
"refersCoords = RDFcoords('', 'rt:')\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",
"thingCoords = RDFcoords('', 'th:', 'E70')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"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": 41,
"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 ' + hasAlternativeFormCoords.prefix + ' ' + hasAlternativeFormCoords.uri + closeLine)\n",
" output.write('@prefix ' + hasCurrentPermanentLocationCoords.prefix + ' ' + hasCurrentPermanentLocationCoords.uri + closeLine)\n",
" output.write('@prefix ' + placeCoords.prefix + ' ' + placeCoords.uri + closeLine)\n",
" output.write('@prefix ' + refersCoords.prefix + ' ' + refersCoords.uri + closeLine)\n",
" output.write('@prefix ' + thingCoords.prefix + ' ' + thingCoords.uri + closeLine)\n",
" output.write('\\n')\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"filePrefix = 'data_'\n",
"fileType = 'item'\n",
"max_entries = 1000000000\n",
"\n",
"with open(import_dir + filePrefix + fileType + '.csv', newline=\"\") as csv_file, open(export_dir + filePrefix + fileType + '_list.ttl', 'w') as output:\n",
" reader = csv.DictReader(csv_file)\n",
" writeTTLHeader(output)\n",
" first = True\n",
" ii = 0\n",
" e55placeHolder = ''\n",
" line = triple(e55placeHolder, hasTypeCoords.prefix, typeCoords.prefix) + closeLine\n",
" output.write(line)\n",
" line = triple(e55placeHolder, labelCoords.prefix, '\\\"Merce\\\"') + closeLine\n",
" output.write(line)\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",
" # E73 Information Object\n",
" e73placeHolder = \"\"\n",
" if row['lista'] != '' and row['lista'] != ' ':\n",
" item = []\n",
" pipe = \" | \"\n",
" if pipe in row['lista']:\n",
" item = row['lista'].replace(\"Merci | \",\"\").split(' | ') \n",
" for merce in item:\n",
" e70placeHolder = ''\n",
" line = triple(e73placeHolder, refersCoords.prefix, e70placeHolder) + closeLine\n",
" output.write(line)\n",
" line = triple(e70placeHolder, hasTypeCoords.prefix, thingCoords.prefix) + closeLine\n",
" output.write(line)\n",
" lemma = merce.replace(\" \",\"\")\n",
" line = triple(e70placeHolder, labelCoords.prefix, '\\\"' + merce.lower() + '\\\"') + closeLine\n",
" output.write(line) \n",
" line = triple(e70placeHolder, hasTypePCoords.prefix, e55placeHolder) + closeLine\n",
" output.write(line)\n",
" else:\n",
" e70placeHolder = ''\n",
" line = triple(e73placeHolder, refersCoords.prefix, e70placeHolder) + closeLine\n",
" output.write(line)\n",
" line = triple(e70placeHolder, hasTypeCoords.prefix, thingCoords.prefix) + closeLine\n",
" output.write(line)\n",
" line = triple(e70placeHolder, labelCoords.prefix, '\\\"' + row['lista'].lower() + '\\\"') + closeLine\n",
" output.write(line) \n",
" line = triple(e70placeHolder, hasTypePCoords.prefix, e55placeHolder) + closeLine\n",
" output.write(line)\n",
" output.write('\\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
}