Total Complexity | 4 |
Total Lines | 25 |
Duplicated Lines | 0 % |
Changes | 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 transform |
||
6 | |||
7 | |||
8 | class InputLimiter(transform.BaseTransform): |
||
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 transform cannot be negative', |
||
24 | project_id=self.project.project_id) |
||
25 |