annif.transform.inputlimiter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A InputLimiter._validate_value() 0 5 2
A InputLimiter.__init__() 0 4 1
A InputLimiter.transform_text() 0 2 1
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