Skip to content

Commit

Permalink
Merge pull request #52 from acdh-oeaw/51-import-skosexactmatch-is-ign…
Browse files Browse the repository at this point in the history
…ored

51 import skosexactmatch is ignored
  • Loading branch information
csae8092 authored Nov 23, 2022
2 parents 7bd7487 + 671f9a5 commit 4759c4d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
10 changes: 10 additions & 0 deletions vocabs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions vocabs/skos_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import Graph, Namespace, RDF, URIRef
from rdflib import Graph, Namespace, RDF, URIRef, SKOS
from .models import (
SkosCollection,
SkosConcept,
Expand All @@ -11,7 +11,8 @@
CollectionSource,
ConceptLabel,
ConceptNote,
ConceptSource
ConceptSource,
SKOS_RELATION_TYPES
)
import re
import logging
Expand All @@ -23,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#")
Expand Down Expand Up @@ -198,6 +198,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):
Expand Down Expand Up @@ -416,6 +424,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
Expand Down
30 changes: 30 additions & 0 deletions vocabs/tests/exact_match.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix vocab: <https://vocabs.acdh.oeaw.ac.at/testit/> .

<https://chronontology.dainst.org/period/vpg0DvwPQxyc> skos:exactMatch vocab:100 .

vocab:100 a skos:Concept ;
dct:source "ACDH-CH" ;
skos:exactMatch <https://chronontology.dainst.org/period/vpg0DvwPQxyc> ;
skos:narrowMatch <http://www.eionet.europa.eu/gemet/concept/3252> ;
skos:exactMatch <https://d-nb.info/gnd/1197273174> ;
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 .
9 changes: 9 additions & 0 deletions vocabs/tests/test_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down

0 comments on commit 4759c4d

Please sign in to comment.