aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/base/extractor.py
blob: d5b092202d243fab53bbb54b08ea2b1ccaa3438f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""The Extractor classes transform content into triples.

Part of the bsie module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
import abc
import collections
import typing

# inner-module imports
from . import reader
from bsie.utils import node
from bsie.utils.bsfs import URI, typename

# exports
__all__: typing.Sequence[str] = (
    'Extractor',
    )


## code ##

class Extractor(abc.ABC, collections.abc.Iterable, collections.abc.Callable):
    """Produce (node, predicate, value)-triples from some content."""

    # what type of content is expected (i.e. reader subclass).
    CONTENT_READER: typing.Optional[typing.Type[reader.Reader]] = None

    def __str__(self) -> str:
        return typename(self)

    def __repr__(self) -> str:
        return f'{typename(self)}()'

    @abc.abstractmethod
    def schema(self) -> str:
        """Return the schema (predicates and nodes) produced by this Extractor."""

    @abc.abstractmethod
    def extract(
            self,
            subject: node.Node,
            content: typing.Any,
            predicates: typing.Iterable[URI],
            ) -> typing.Iterator[typing.Tuple[node.Node, URI, typing.Any]]:
        """Return (node, predicate, value) triples."""

## EOF ##