|
1
|
|
|
"""Ensemble backend that combines results from multiple projects""" |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
import annif.suggestion |
|
5
|
|
|
import annif.project |
|
6
|
|
|
import annif.util |
|
7
|
|
|
from . import backend |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class EnsembleBackend(backend.AnnifBackend): |
|
11
|
|
|
"""Ensemble backend that combines results from multiple projects""" |
|
12
|
|
|
name = "ensemble" |
|
13
|
|
|
|
|
14
|
|
|
def _normalize_hits(self, hits, source_project): |
|
15
|
|
|
"""Hook for processing hits from backends. Intended to be overridden |
|
16
|
|
|
by subclasses.""" |
|
17
|
|
|
return hits |
|
18
|
|
|
|
|
19
|
|
|
def _suggest_with_sources(self, text, sources): |
|
20
|
|
|
hits_from_sources = [] |
|
21
|
|
|
for project_id, weight in sources: |
|
22
|
|
|
source_project = annif.project.get_project(project_id) |
|
23
|
|
|
hits = source_project.suggest(text) |
|
24
|
|
|
self.debug( |
|
25
|
|
|
'Got {} hits from project {}'.format( |
|
26
|
|
|
len(hits), source_project.project_id)) |
|
27
|
|
|
norm_hits = self._normalize_hits(hits, source_project) |
|
28
|
|
|
hits_from_sources.append( |
|
29
|
|
|
annif.suggestion.WeightedSuggestion( |
|
30
|
|
|
hits=norm_hits, weight=weight)) |
|
31
|
|
|
return hits_from_sources |
|
32
|
|
|
|
|
33
|
|
|
def _merge_hits_from_sources(self, hits_from_sources, project, params): |
|
34
|
|
|
"""Hook for merging hits from sources. Can be overridden by |
|
35
|
|
|
subclasses.""" |
|
36
|
|
|
return annif.util.merge_hits(hits_from_sources, project.subjects) |
|
37
|
|
|
|
|
38
|
|
|
def _suggest(self, text, project, params): |
|
39
|
|
|
sources = annif.util.parse_sources(params['sources']) |
|
40
|
|
|
hits_from_sources = self._suggest_with_sources(text, sources) |
|
41
|
|
|
merged_hits = self._merge_hits_from_sources(hits_from_sources, |
|
42
|
|
|
project, |
|
43
|
|
|
params) |
|
44
|
|
|
self.debug('{} hits after merging'.format(len(merged_hits))) |
|
45
|
|
|
return merged_hits |
|
46
|
|
|
|