Completed
Push — master ( f6c6f1...e349d9 )
by Juho
26s queued 19s
created

annif.transform.transform   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseTransform.transform_fn() 0 4 1
A TransformChain.transform_corpus() 0 2 1
A IdentityTransform.transform_fn() 0 2 1
A TransformChain.transform_text() 0 4 2
A TransformChain.__init__() 0 3 1
A BaseTransform.__init__() 0 2 1
A TransformChain._init_transforms() 0 12 3
1
"""Common functionality for transforming text of input documents."""
2
3
import abc
4
from annif.corpus import TransformingDocumentCorpus
5
from annif.exception import ConfigurationException
6
7
8
class BaseTransform(metaclass=abc.ABCMeta):
9
    """Base class for text transformations, which need to implement the
10
    transform function."""
11
12
    name = None
13
14
    def __init__(self, project):
15
        self.project = project
16
17
    @abc.abstractmethod
18
    def transform_fn(self, text):
19
        """Perform the text transformation."""
20
        pass  # pragma: no cover
21
22
23
class IdentityTransform(BaseTransform):
24
    """Transform that does not modify text but simply passes it through."""
25
26
    name = 'pass'
27
28
    def transform_fn(self, text):
29
        return text
30
31
32
class TransformChain():
33
    """Class instantiating and holding the transformation objects performing
34
    the actual text transformation."""
35
36
    def __init__(self, transform_classes, args, project):
37
        self.project = project
38
        self.transforms = self._init_transforms(transform_classes, args)
39
40
    def _init_transforms(self, transform_classes, args):
41
        transforms = []
42
        for trans, (posargs, kwargs) in zip(transform_classes, args):
43
            try:
44
                transforms.append(
45
                    trans(self.project, *posargs, **kwargs))
46
            except (ValueError, TypeError):
47
                raise ConfigurationException(
48
                    f"Invalid arguments to {trans.name} transform: "
49
                    f"{posargs}, {kwargs})",
50
                    project_id=self.project.project_id)
51
        return transforms
52
53
    def transform_text(self, text):
54
        for trans in self.transforms:
55
            text = trans.transform_fn(text)
56
        return text
57
58
    def transform_corpus(self, corpus):
59
        return TransformingDocumentCorpus(corpus, self.transform_text)
60