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

EnsembleBackend._analyze_with_sources()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 13
rs 9.75
c 0
b 0
f 0
cc 2
nop 3
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 _suggest(self, text, project, params):
34
        sources = annif.util.parse_sources(params['sources'])
35
        hits_from_sources = self._suggest_with_sources(text, sources)
36
        merged_hits = annif.util.merge_hits(
37
            hits_from_sources, project.subjects)
38
        self.debug('{} hits after merging'.format(len(merged_hits)))
39
        return merged_hits
40