Passed
Pull Request — master (#496)
by
unknown
02:05
created

annif.transformer.transformer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A IdentityTransformer.transform_text() 0 3 1
A Transformer.__init__() 0 3 1
A IdentityTransformer.__init__() 0 2 1
A IdentityTransformer.transform_corpus() 0 3 1
A Transformer._init_transformers() 0 12 3
A Transformer.transform_text() 0 4 2
A Transformer.transform_corpus() 0 4 2
1
"""Common functionality for input transforming."""
2
3
from annif.corpus import TransformingDocumentCorpus
4
from annif.exception import ConfigurationException
5
6
7
class IdentityTransformer():
8
    """"""""  # TODO
9
10
    name = 'pass_through'
11
12
    def __init__(self, project):
13
        self.project = project
14
15
    def transform_text(self, text):
16
        """"""  # TODO
17
        return text
18
19
    def transform_corpus(self, corpus):
20
        """"""  # TODO
21
        return TransformingDocumentCorpus(corpus, self.transform_text)
22
23
24
class Transformer():
25
    """"""  # TODO
26
27
    def __init__(self, transformer_classes, args, project):
28
        self.project = project
29
        self.transformers = self._init_transformers(transformer_classes, args)
30
31
    def _init_transformers(self, transformer_classes, args):
32
        transformers = []
33
        for trans, (posargs, kwargs) in zip(transformer_classes, args):
34
            try:
35
                transformers.append(
36
                    trans(self.project, *posargs, **kwargs))
37
            except (ValueError, TypeError):
38
                raise ConfigurationException(
39
                    f"Invalid arguments to {trans.name} transformer: "
40
                    f"{posargs}, {kwargs})",
41
                    project_id=self.project.project_id)
42
        return transformers
43
44
    def transform_text(self, text):
45
        for trans in self.transformers:
46
            text = trans.transform_text(text)
47
        return text
48
49
    def transform_corpus(self, corpus):
50
        for trans in self.transformers:
51
            corpus = trans.transform_corpus(corpus)
52
        return corpus
53