Passed
Push — master ( c8c370...dee89b )
by Osma
03:14
created

annif.backend.backend.AnnifBackend._suggest()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
"""Common functionality for backends."""
2
3
import abc
4
from annif import logger
5
6
7
class AnnifBackend(metaclass=abc.ABCMeta):
8
    """Base class for Annif backends that perform analysis. The
9
    non-implemented methods should be overridden in subclasses."""
10
11
    name = None
12
    needs_subject_index = False
13
    needs_subject_vectorizer = False
14
15
    def __init__(self, backend_id, params, datadir):
16
        """Initialize backend with specific parameters. The
17
        parameters are a dict. Keys and values depend on the specific
18
        backend type."""
19
        self.backend_id = backend_id
20
        self.params = params
21
        self.datadir = datadir
22
23
    def train(self, corpus, project):
24
        """train the model on the given document or subject corpus"""
25
        pass  # default is to do nothing, subclasses may override
26
27
    def initialize(self):
28
        """This method can be overridden by backends. It should cause the
29
        backend to pre-load all data it needs during operation."""
30
        pass
31
32
    @abc.abstractmethod
33
    def _suggest(self, text, project, params):
34
        """This method should implemented by backends. It implements
35
        the suggest functionality, with pre-processed parameters."""
36
        pass  # pragma: no cover
37
38
    def suggest(self, text, project, params=None):
39
        """Suggest subjects for the input text and return a list of subjects
40
        represented as a list of SubjectSuggestion objects."""
41
        beparams = dict(self.params)
42
        if params is not None:
43
            beparams.update(params)
44
        return self._suggest(text, project, params=beparams)
45
46
    def debug(self, message):
47
        """Log a debug message from this backend"""
48
        logger.debug("Backend {}: {}".format(self.backend_id, message))
49
50
    def info(self, message):
51
        """Log an info message from this backend"""
52
        logger.info("Backend {}: {}".format(self.backend_id, message))
53
54
    def warning(self, message):
55
        """Log a warning message from this backend"""
56
        logger.warning("Backend {}: {}".format(self.backend_id, message))
57
58
59
class AnnifLearningBackend(AnnifBackend):
60
    """Base class for Annif backends that can perform online learning"""
61
62
    @abc.abstractmethod
63
    def learn(self, corpus, project):
64
        """further train the model on the given document or subject corpus"""
65
        pass  # pragma: no cover
66