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

annif.backend.dummy.DummyBackend._suggest()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
"""Dummy backend for testing basic interaction of projects and backends"""
2
3
4
from annif.suggestion import SubjectSuggestion, ListSuggestionResult
5
from . import backend
6
7
8
class DummyBackend(backend.AnnifLearningBackend):
9
    name = "dummy"
10
    initialized = False
11
    uri = 'http://example.org/dummy'
12
    label = 'dummy'
13
14
    def initialize(self):
15
        self.initialized = True
16
17
    def _suggest(self, text, project, params):
18
        score = float(params.get('score', 1.0))
19
        return ListSuggestionResult([SubjectSuggestion(uri=self.uri,
20
                                                       label=self.label,
21
                                                       score=score)],
22
                                    project.subjects)
23
24
    def learn(self, corpus, project):
25
        # in this dummy backend we "learn" by picking up the URI and label
26
        # of the first subject of the first document in the learning set
27
        # and using that in subsequent analysis results
28
        for doc in corpus.documents:
29
            if doc.uris and doc.labels:
30
                self.uri = list(doc.uris)[0]
31
                self.label = list(doc.labels)[0]
32
            break
33