Passed
Pull Request — main (#846)
by Osma
06:37 queued 03:27
created

annif.vocab.rules   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 23

6 Functions

Rating   Name   Duplication   Size   Complexity  
D kwargs_to_exclude_uris() 0 45 12
A uris_by_type() 0 5 2
A remove_uris() 0 6 3
A uris_by_scheme() 0 5 2
A uris_by_collection() 0 5 2
A add_uris() 0 5 2
1
"""Support for exclude/include rules for subject vocabularies"""
2
3
from rdflib import RDF, Graph, URIRef
4
from rdflib.namespace import SKOS
5
6
import annif
7
from annif.exception import ConfigurationException
8
9
from .vocab import AnnifVocabulary
10
11
logger = annif.logger
12
13
14
def uris_by_type(graph: Graph, type: str, action: str) -> list[str]:
15
    uris = [str(uri) for uri in graph.subjects(RDF.type, URIRef(type))]
16
    if not uris:
17
        logger.warning(f"{action}: no concepts found with type {type}")
18
    return uris
19
20
21
def uris_by_scheme(graph: Graph, scheme: str, action: str) -> list[str]:
22
    uris = [str(uri) for uri in graph.subjects(SKOS.inScheme, URIRef(scheme))]
23
    if not uris:
24
        logger.warning(f"{action}: no concepts found in scheme {scheme}")
25
    return uris
26
27
28
def uris_by_collection(graph: Graph, collection: str, action: str) -> list[str]:
29
    uris = [str(uri) for uri in graph.objects(URIRef(collection), SKOS.member)]
30
    if not uris:
31
        logger.warning(f"{action}: no concepts found in collection {collection}")
32
    return uris
33
34
35
def add_uris(
36
    graph: Graph, uris_func: callable, uris_set: set[str], vals: list[str], action: str
37
) -> None:
38
    for val in vals:
39
        uris_set.update(uris_func(graph, val, action))
40
41
42
def remove_uris(
43
    graph: Graph, uris_func: callable, uris_set: set[str], vals: list[str], action: str
44
) -> None:
45
    for val in vals:
46
        for uri in uris_func(graph, val, action):
47
            uris_set.discard(uri)
48
49
50
def kwargs_to_exclude_uris(vocab: AnnifVocabulary, kwargs: dict[str, str]) -> set[str]:
51
    exclude_uris = set()
52
    actions = {
53
        "exclude": lambda vals: exclude_uris.update(
54
            vals
55
            if "*" not in vals
56
            else uris_by_type(vocab.as_graph(), SKOS.Concept, "exclude")
57
        ),
58
        "exclude_type": lambda vals: add_uris(
59
            vocab.as_graph(), uris_by_type, exclude_uris, vals, "exclude_type"
60
        ),
61
        "exclude_scheme": lambda vals: add_uris(
62
            vocab.as_graph(), uris_by_scheme, exclude_uris, vals, "exclude_scheme"
63
        ),
64
        "exclude_collection": lambda vals: add_uris(
65
            vocab.as_graph(),
66
            uris_by_collection,
67
            exclude_uris,
68
            vals,
69
            "exclude_collection",
70
        ),
71
        "include": lambda vals: exclude_uris.difference_update(vals),
72
        "include_type": lambda vals: remove_uris(
73
            vocab.as_graph(), uris_by_type, exclude_uris, vals, "include_type"
74
        ),
75
        "include_scheme": lambda vals: remove_uris(
76
            vocab.as_graph(), uris_by_scheme, exclude_uris, vals, "include_scheme"
77
        ),
78
        "include_collection": lambda vals: remove_uris(
79
            vocab.as_graph(),
80
            uris_by_collection,
81
            exclude_uris,
82
            vals,
83
            "include_collection",
84
        ),
85
    }
86
87
    for key, value in kwargs.items():
88
        vals = value.split("|")
89
        if key in actions:
90
            actions[key](vals)
91
        else:
92
            raise ConfigurationException(f"unknown vocab keyword argument {key}")
93
94
    return exclude_uris
95