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

AnnifBackend   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _get_datadir() 0 11 3
A __init__() 0 7 1
A analyze() 0 5 1
A load_subjects() 0 3 1
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