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

TransformChain._init_transforms()   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 3
dl 0
loc 12
rs 9.85
c 0
b 0
f 0
1
"""Common functionality for transforming text of input documents."""
2
3
from annif.corpus import TransformingDocumentCorpus
4
from annif.exception import ConfigurationException
5
6
7
class IdentityTransform():
8
    """Transform that does not modify text but simply passes it through. This
9
    class also acts as a base class for other transformations, which need to
10
    implement and override the transform function."""
11
12
    name = 'pass'
13
14
    def __init__(self, project):
15
        self.project = project
16
17
    def transform_fn(self, text):
18
        return text
19
20
21
class TransformChain():
22
    """Class instantiating and holding the transformation objects performing
23
    the actual text transformation."""
24
25
    def __init__(self, transform_classes, args, project):
26
        self.project = project
27
        self.transforms = self._init_transforms(transform_classes, args)
28
29
    def _init_transforms(self, transform_classes, args):
30
        transforms = []
31
        for trans, (posargs, kwargs) in zip(transform_classes, args):
32
            try:
33
                transforms.append(
34
                    trans(self.project, *posargs, **kwargs))
35
            except (ValueError, TypeError):
36
                raise ConfigurationException(
37
                    f"Invalid arguments to {trans.name} transform: "
38
                    f"{posargs}, {kwargs})",
39
                    project_id=self.project.project_id)
40
        return transforms
41
42
    def transform_text(self, text):
43
        for trans in self.transforms:
44
            text = trans.transform_fn(text)
45
        return text
46
47
    def transform_corpus(self, corpus):
48
        return TransformingDocumentCorpus(corpus, self.transform_text)
49