Passed
Push — testing-on-windows-and-macos ( 782857...ea99ad )
by Juho
04:06
created

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

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""Dummy backend for testing basic interaction of projects and backends"""
2
3
from __future__ import annotations
4
5
from typing import TYPE_CHECKING, Any
6
7
from annif.suggestion import SubjectSuggestion
8
9
from . import backend
10
11
if TYPE_CHECKING:
12
    from annif.corpus.document import DocumentCorpus
13
14
15
class DummyBackend(backend.AnnifLearningBackend):
16
    name = "dummy"
17
    initialized = False
18
    subject_id = 0
19
    is_trained = True
20
    modification_time = None
21
22
    def initialize(self, parallel: bool = False) -> None:
23
        self.initialized = True
24
25
    def _suggest(self, text: str, params: dict[str, Any]) -> list[SubjectSuggestion]:
26
        score = float(params.get("score", 1.0))
27
28
        # Ensure tests fail if "text" with wrong type ends up here
29
        assert isinstance(text, str)
30
31
        # Give no hits for no text
32
        if len(text) == 0:
33
            return []
34
35
        # allow overriding returned subject via uri parameter
36
        if "uri" in params:
37
            subject_id = self.project.subjects.by_uri(params["uri"])
38
        else:
39
            subject_id = self.subject_id
40
41
        return [SubjectSuggestion(subject_id=subject_id, score=score)]
42
43
    def _learn(
44
        self,
45
        corpus: DocumentCorpus,
46
        params: dict[str, Any],
47
    ) -> None:
48
        # in this dummy backend we "learn" by picking up the subject ID
49
        # of the first subject of the first document in the learning set
50
        # and using that in subsequent analysis results
51
        for doc in corpus.documents:
52
            if doc.subject_set:
53
                self.subject_id = doc.subject_set[0]
54
            break
55