annif.vocab.types   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A SubjectIndex.__getitem__() 0 3 1
A SubjectIndex.languages() 0 4 1
A SubjectIndex.active() 0 5 1
A VocabSource.subjects() 0 5 1
A VocabSource.languages() 0 5 1
A SubjectIndex.by_label() 0 5 1
A SubjectIndex.by_uri() 0 5 1
A VocabSource.save_skos() 0 5 1
A SubjectIndex.__len__() 0 3 1
A SubjectIndex.contains_uri() 0 3 1
1
"""Type declarations for vocabulary functionality"""
2
3
from __future__ import annotations
4
5
import abc
6
import collections
7
8
Subject = collections.namedtuple("Subject", "uri labels notation")
9
10
11
class VocabSource(metaclass=abc.ABCMeta):
12
    """Abstract base class for vocabulary sources"""
13
14
    @property
15
    @abc.abstractmethod
16
    def subjects(self):
17
        """Iterate through the vocabulary, yielding Subject objects."""
18
        pass  # pragma: no cover
19
20
    @property
21
    @abc.abstractmethod
22
    def languages(self):
23
        """Provide a list of language codes supported by this vocabulary source."""
24
        pass  # pragma: no cover
25
26
    @abc.abstractmethod
27
    def save_skos(self, path):
28
        """Save the contents of the vocabulary source into a SKOS/Turtle
29
        file with the given path name."""
30
        pass  # pragma: no cover
31
32
33
class SubjectIndex(metaclass=abc.ABCMeta):
34
    """Base class for an index that remembers the associations between
35
    integer subject IDs and their URIs and labels."""
36
37
    @abc.abstractmethod
38
    def __len__(self) -> int:
39
        pass  # pragma: no cover
40
41
    @property
42
    @abc.abstractmethod
43
    def languages(self) -> list[str] | None:
44
        pass  # pragma: no cover
45
46
    @abc.abstractmethod
47
    def __getitem__(self, subject_id: int) -> Subject:
48
        pass  # pragma: no cover
49
50
    @abc.abstractmethod
51
    def contains_uri(self, uri: str) -> bool:
52
        pass  # pragma: no cover
53
54
    @abc.abstractmethod
55
    def by_uri(self, uri: str, warnings: bool = True) -> int | None:
56
        """return the subject ID of a subject by its URI, or None if not found.
57
        If warnings=True, log a warning message if the URI cannot be found."""
58
        pass  # pragma: no cover
59
60
    @abc.abstractmethod
61
    def by_label(self, label: str | None, language: str) -> int | None:
62
        """return the subject ID of a subject by its label in a given
63
        language"""
64
        pass  # pragma: no cover
65
66
    @abc.abstractmethod
67
    def active(self) -> list[tuple[int, Subject]]:
68
        """return a list of (subject_id, Subject) tuples of all subjects that
69
        are available for use"""
70
        pass  # pragma: no cover
71