Passed
Pull Request — master (#257)
by Osma
02:44
created

annif.backend.dummy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 22
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A DummyBackend._analyze() 0 6 1
A DummyBackend.initialize() 0 2 1
A DummyBackend.learn() 0 9 4
1
"""Dummy backend for testing basic interaction of projects and backends"""
2
3
4
from annif.hit import AnalysisHit, ListAnalysisResult
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 _analyze(self, text, project, params):
18
        score = float(params.get('score', 1.0))
19
        return ListAnalysisResult([AnalysisHit(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