Validator for score weight values (currently a tuple of four numbers).
| 195 | |
| 196 | |
| 197 | class ScoreWeightsValue(ValidatedValue): |
| 198 | """Validator for score weight values (currently a tuple of four numbers).""" |
| 199 | |
| 200 | _IGNORE_CHARS = (",", "/", "(", ")") |
| 201 | """Characters to ignore.""" |
| 202 | |
| 203 | def __init__(self, value: str | ContentDetector.Components): |
| 204 | if isinstance(value, ContentDetector.Components): |
| 205 | self._value = value |
| 206 | else: |
| 207 | translation_table = str.maketrans( |
| 208 | {char: " " for char in ScoreWeightsValue._IGNORE_CHARS} |
| 209 | ) |
| 210 | values = value.translate(translation_table).split() |
| 211 | if not len(values) == 4: |
| 212 | raise ValueError("Score weights must be specified as four numbers!") |
| 213 | self._value = ContentDetector.Components(*(float(val) for val in values)) |
| 214 | |
| 215 | @property |
| 216 | def value(self) -> ContentDetector.Components: |
| 217 | return self._value |
| 218 | |
| 219 | def __str__(self) -> str: |
| 220 | return "{:.3f}, {:.3f}, {:.3f}, {:.3f}".format(*self.value) |
| 221 | |
| 222 | @staticmethod |
| 223 | def from_config(config_value: str, default: "ScoreWeightsValue") -> "ScoreWeightsValue": |
| 224 | try: |
| 225 | return ScoreWeightsValue(config_value) |
| 226 | except ValueError as ex: |
| 227 | raise OptionParseFailure( |
| 228 | "Score weights must be specified as four numbers in the form (H,S,L,E)," |
| 229 | " e.g. (0.9, 0.2, 2.0, 0.5). Commas/brackets/slashes are ignored." |
| 230 | ) from ex |
| 231 | |
| 232 | |
| 233 | class KernelSizeValue(ValidatedValue): |
no outgoing calls
no test coverage detected