| Total Complexity | 4 |
| Total Lines | 40 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |