| Total Complexity | 4 |
| Total Lines | 32 |
| 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 __future__ import annotations |
||
| 5 | |||
| 6 | from typing import TYPE_CHECKING |
||
| 7 | |||
| 8 | from annif.exception import ConfigurationException |
||
| 9 | |||
| 10 | from . import transform |
||
| 11 | |||
| 12 | if TYPE_CHECKING: |
||
| 13 | from annif.project import AnnifProject |
||
| 14 | |||
| 15 | |||
| 16 | class InputLimiter(transform.BaseTransform): |
||
| 17 | name = "limit" |
||
| 18 | |||
| 19 | def __init__(self, project: AnnifProject | None, input_limit: str) -> None: |
||
| 20 | super().__init__(project) |
||
| 21 | self.input_limit = int(input_limit) |
||
| 22 | self._validate_value(self.input_limit) |
||
| 23 | |||
| 24 | def transform_text(self, text: str) -> str: |
||
| 25 | return text[: self.input_limit] |
||
| 26 | |||
| 27 | def _validate_value(self, input_limit: int) -> None: |
||
| 28 | if input_limit < 0: |
||
| 29 | raise ConfigurationException( |
||
| 30 | "input_limit in limit_input transform cannot be negative", |
||
| 31 | project_id=self.project.project_id, |
||
| 32 | ) |
||
| 33 |