Passed
Branch tfidf-backend (2ab86e)
by Osma
03:35
created

AnnifBackend.load_subjects()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
"""Common functionality for backends."""
2
3
import abc
4
import os
5
import os.path
6
import annif
7
8
9
class AnnifBackend(metaclass=abc.ABCMeta):
1 ignored issue
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
10
    """Base class for Annif backends that perform analysis. The
11
    non-implemented methods should be overridden in subclasses."""
12
13
    name = None
14
15
    def __init__(self, backend_id, config):
16
        """Initialize backend with a specific configuration. The
17
        configuration is a dict. Keys and values depend on the specific
18
        backend type."""
19
        self.backend_id = backend_id
20
        self.config = config
21
        self._datadir = None
22
23
    def _get_datadir(self):
24
        """return the path of the directory where this backend can store its
25
        data files"""
26
        if self._datadir is None:
27
            self._datadir = os.path.join(
28
                annif.cxapp.app.config['DATADIR'],
29
                'backends',
30
                self.backend_id)
31
            if not os.path.exists(self._datadir):
32
                os.makedirs(self._datadir)
33
        return self._datadir
34
35
    def load_subjects(self, subjects, analyzer):
36
        """load the given subjects analyzed using the given analyzer"""
37
        pass  # default is to do nothing, subclasses may override
38
39
    @abc.abstractmethod
40
    def analyze(self, text):
41
        """Analyze some input text and return a list of subjects represented
42
        as a list of AnalysisHit objects."""
43
        pass
44