Passed
Pull Request — master (#259)
by Osma
02:45
created

annif.backend.backend.AnnifBackend._get_datadir()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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