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 SubjectCorpus(metaclass=abc.ABCMeta): |
12
|
|
|
"""Abstract base class for subject corpora""" |
13
|
|
|
|
14
|
|
|
@property |
15
|
|
|
@abc.abstractmethod |
16
|
|
|
def subjects(self): |
17
|
|
|
"""Iterate through the subject corpus, 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 subject |
24
|
|
|
corpus.""" |
25
|
|
|
pass # pragma: no cover |
26
|
|
|
|
27
|
|
|
@abc.abstractmethod |
28
|
|
|
def save_skos(self, path): |
29
|
|
|
"""Save the contents of the subject corpus into a SKOS/Turtle |
30
|
|
|
file with the given path name.""" |
31
|
|
|
pass # pragma: no cover |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class SubjectIndex(metaclass=abc.ABCMeta): |
35
|
|
|
"""Base class for an index that remembers the associations between |
36
|
|
|
integer subject IDs and their URIs and labels.""" |
37
|
|
|
|
38
|
|
|
@abc.abstractmethod |
39
|
|
|
def __len__(self) -> int: |
40
|
|
|
pass # pragma: no cover |
41
|
|
|
|
42
|
|
|
@property |
43
|
|
|
@abc.abstractmethod |
44
|
|
|
def languages(self) -> list[str] | None: |
45
|
|
|
pass # pragma: no cover |
46
|
|
|
|
47
|
|
|
@abc.abstractmethod |
48
|
|
|
def __getitem__(self, subject_id: int) -> Subject: |
49
|
|
|
pass # pragma: no cover |
50
|
|
|
|
51
|
|
|
@abc.abstractmethod |
52
|
|
|
def contains_uri(self, uri: str) -> bool: |
53
|
|
|
pass # pragma: no cover |
54
|
|
|
|
55
|
|
|
@abc.abstractmethod |
56
|
|
|
def by_uri(self, uri: str, warnings: bool = True) -> int | None: |
57
|
|
|
"""return the subject ID of a subject by its URI, or None if not found. |
58
|
|
|
If warnings=True, log a warning message if the URI cannot be found.""" |
59
|
|
|
pass # pragma: no cover |
60
|
|
|
|
61
|
|
|
@abc.abstractmethod |
62
|
|
|
def by_label(self, label: str | None, language: str) -> int | None: |
63
|
|
|
"""return the subject ID of a subject by its label in a given |
64
|
|
|
language""" |
65
|
|
|
pass # pragma: no cover |
66
|
|
|
|
67
|
|
|
@abc.abstractmethod |
68
|
|
|
def active(self) -> list[tuple[int, Subject]]: |
69
|
|
|
"""return a list of (subject_id, Subject) tuples of all subjects that |
70
|
|
|
are available for use""" |
71
|
|
|
pass # pragma: no cover |
72
|
|
|
|