From 2ffefcb623d1fc9f92557ebdb23a04ba81182085 Mon Sep 17 00:00:00 2001 From: csae8092 Date: Wed, 23 Nov 2022 11:16:19 +0100 Subject: [PATCH 1/2] fix: process `skos:*match` on import * ToDo: add some testing --- vocabs/models.py | 10 ++++++++++ vocabs/skos_import.py | 17 +++++++++++++++-- vocabs/tests/exact_match.ttl | 30 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 vocabs/tests/exact_match.ttl diff --git a/vocabs/models.py b/vocabs/models.py index f50dab0..3e6395a 100644 --- a/vocabs/models.py +++ b/vocabs/models.py @@ -52,6 +52,16 @@ ) +SKOS_RELATION_TYPES = [ + ('related', 'related'), + ('broadMatch', 'broad_match'), + ('narrowMatch', 'narrow_match'), + ('exactMatch', 'exact_match'), + ('relatedMatch', 'related_match'), + ('closeMatch', 'close_match') +] + + ###################################################################### # # SkosConceptScheme diff --git a/vocabs/skos_import.py b/vocabs/skos_import.py index ddf6984..00bfbbe 100644 --- a/vocabs/skos_import.py +++ b/vocabs/skos_import.py @@ -1,4 +1,4 @@ -from rdflib import Graph, Namespace, RDF, URIRef +from rdflib import Graph, Namespace, RDF, URIRef, SKOS from .models import ( SkosCollection, SkosConcept, @@ -11,7 +11,8 @@ CollectionSource, ConceptLabel, ConceptNote, - ConceptSource + ConceptSource, + SKOS_RELATION_TYPES ) import re import logging @@ -198,6 +199,14 @@ def language_check(property_lang): concepts = [] for c in g.subjects(RDF.type, SKOS.Concept): concept = {"legacy_id": str(c)} + # process skos:exactMatch etc. + for rel_type in SKOS_RELATION_TYPES: + cur_subj = URIRef(concept['legacy_id']) + pred = SKOS[rel_type[0]] + if (cur_subj, pred, None) in g: + concept[rel_type[1]] = set() + for s, _, o in g.triples((cur_subj, pred, None)): + concept[rel_type[1]].add(f"{o}") # Concept pref labels pref_labels = [] for pref_label in g.preferredLabel(c): @@ -416,6 +425,10 @@ def upload_data(self, user): notation=concept_notation, creator=concept_creator, contributor=concept_contributor, created_by=User.objects.get(username=user) ) + for rel_type in SKOS_RELATION_TYPES: + if concept.get(rel_type[1]): + values = ",".join(concept.get(rel_type[1])) + setattr(new_concept, rel_type[1], values) new_concept.save() # concept to collections diff --git a/vocabs/tests/exact_match.ttl b/vocabs/tests/exact_match.ttl new file mode 100644 index 0000000..579794c --- /dev/null +++ b/vocabs/tests/exact_match.ttl @@ -0,0 +1,30 @@ +@prefix dc: . +@prefix dct: . +@prefix owl: . +@prefix rdfs: . +@prefix skos: . +@prefix vocab: . + + skos:exactMatch vocab:100 . + +vocab:100 a skos:Concept ; + dct:source "ACDH-CH" ; + skos:exactMatch ; + skos:narrowMatch ; + skos:exactMatch ; + skos:definition "Level 1 of the test-it"@en ; + skos:inScheme vocab:testitSchema ; + skos:prefLabel "Level 1"@en ; + skos:topConceptOf vocab:testitSchema . + +vocab:testitSchema a skos:ConceptScheme ; + rdfs:label "Test it Thesaurus"@en ; + dc:creator "Klaus Illmayer" ; + dc:description "Test if import of exactmatch works"@en ; + dc:language "en" ; + dc:publisher "ACDH-CH" ; + dc:subject "Test" ; + dc:title "Test-it exactmatch"@en ; + dct:created "2022-11-18" ; + owl:versionInfo "1.0" ; + skos:hasTopConcept vocab:100 . From 671f9a54589e5c299b007521eb4ae2fa37531202 Mon Sep 17 00:00:00 2001 From: csae8092 Date: Wed, 23 Nov 2022 12:04:52 +0100 Subject: [PATCH 2/2] tests: added tests for #51 --- vocabs/skos_import.py | 1 - vocabs/tests/test_import_export.py | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/vocabs/skos_import.py b/vocabs/skos_import.py index 00bfbbe..6b64ba2 100644 --- a/vocabs/skos_import.py +++ b/vocabs/skos_import.py @@ -24,7 +24,6 @@ logging.getLogger().setLevel(logging.INFO) -SKOS = Namespace("http://www.w3.org/2004/02/skos/core#") DC = Namespace("http://purl.org/dc/elements/1.1/") DCT = Namespace("http://purl.org/dc/terms/") RDFS = Namespace("http://www.w3.org/2000/01/rdf-schema#") diff --git a/vocabs/tests/test_import_export.py b/vocabs/tests/test_import_export.py index 0716988..d5a4154 100644 --- a/vocabs/tests/test_import_export.py +++ b/vocabs/tests/test_import_export.py @@ -35,6 +35,15 @@ def test_uploading_data(self): self.assertEqual(len(SkosCollection.objects.all()), 6) self.assertEqual(len(SkosConcept.objects.all()), 114) + def test_related_concepts(self): + test_file = os.path.join(os.path.dirname(__file__), "exact_match.ttl") + skos_vocab = SkosImporter(file=test_file, language="en") + skos_vocab.upload_data(self.user) + item = SkosConcept.objects.filter( + exact_match__contains='https://d-nb.info/gnd/1197273174' + ) + self.assertEqual(item.count(), 1) + class TestSkosExport(TestCase): """ Test module for SKOS export functionality. """