|
1
|
|
|
"""Common functionality for backends.""" |
|
2
|
|
|
|
|
3
|
|
|
import abc |
|
4
|
|
|
import os |
|
5
|
|
|
import os.path |
|
6
|
|
|
import annif |
|
|
|
|
|
|
7
|
|
|
from annif import logger |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class AnnifBackend(metaclass=abc.ABCMeta): |
|
|
|
|
|
|
11
|
|
|
"""Base class for Annif backends that perform analysis. The |
|
12
|
|
|
non-implemented methods should be overridden in subclasses.""" |
|
13
|
|
|
|
|
14
|
|
|
name = None |
|
15
|
|
|
|
|
16
|
|
|
def __init__(self, backend_id, params, datadir): |
|
17
|
|
|
"""Initialize backend with specific parameters. The |
|
18
|
|
|
parameters are a dict. Keys and values depend on the specific |
|
19
|
|
|
backend type.""" |
|
20
|
|
|
self.backend_id = backend_id |
|
21
|
|
|
self.params = params |
|
22
|
|
|
self._datadir = os.path.join(datadir, 'backends', self.backend_id) |
|
23
|
|
|
|
|
24
|
|
|
def _get_datadir(self): |
|
25
|
|
|
"""return the path of the directory where this backend can store its |
|
26
|
|
|
data files""" |
|
27
|
|
|
if not os.path.exists(self._datadir): |
|
28
|
|
|
os.makedirs(self._datadir) |
|
29
|
|
|
return self._datadir |
|
30
|
|
|
|
|
31
|
|
|
def load_subjects(self, subjects, analyzer): |
|
32
|
|
|
"""load the given subjects analyzed using the given analyzer""" |
|
33
|
|
|
pass # default is to do nothing, subclasses may override |
|
34
|
|
|
|
|
35
|
|
|
@abc.abstractmethod |
|
36
|
|
|
def _analyze(self, text, params): |
|
37
|
|
|
"""This method should implemented by backends. It implements |
|
38
|
|
|
the analyze functionality, with pre-processed parameters.""" |
|
39
|
|
|
pass |
|
40
|
|
|
|
|
41
|
|
|
def analyze(self, text, params=None): |
|
42
|
|
|
"""Analyze some input text and return a list of subjects represented |
|
43
|
|
|
as a list of AnalysisHit objects.""" |
|
44
|
|
|
beparams = dict(self.params) |
|
45
|
|
|
if params is not None: |
|
46
|
|
|
beparams.update(params) |
|
47
|
|
|
return self._analyze(text, params=beparams) |
|
48
|
|
|
|
|
49
|
|
|
def debug(self, message): |
|
50
|
|
|
"""Log a debug message from this backend""" |
|
51
|
|
|
logger.debug("Backend {}: {}".format(self.backend_id, message)) |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def info(self, message): |
|
54
|
|
|
"""Log an info message from this backend""" |
|
55
|
|
|
logger.info("Backend {}: {}".format(self.backend_id, message)) |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def warning(self, message): |
|
58
|
|
|
"""Log a warning message from this backend""" |
|
59
|
|
|
logger.warning("Backend {}: {}".format(self.backend_id, message)) |
|
|
|
|
|
|
60
|
|
|
|