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

InputLimiter.transform_fn()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
"""A simple transformation that truncates the text of input documents to a
2
given character length."""
3
4
from annif.exception import ConfigurationException
5
from . import transformer
6
7
8
class InputLimiter(transformer.IdentityTransform):
9
10
    name = 'limit'
11
12
    def __init__(self, project, input_limit):
13
        super().__init__(project)
14
        self.input_limit = int(input_limit)
15
        self._validate_value(self.input_limit)
16
17
    def transform_fn(self, text):
18
        return text[:self.input_limit]
19
20
    def _validate_value(self, input_limit):
21
        if input_limit < 0:
22
            raise ConfigurationException(
23
                'input_limit in limit_input transformer cannot be negative',
24
                project_id=self.project.project_id)
25