File size: 1,311 Bytes
454a778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import re
from typing import Optional
from .config import VALIDATOR_ENABLED

class ValidationResult:
    def __init__(self, is_valid: bool, reason: str = "", score: float = 1.0):
        self.is_valid = is_valid
        self.reason = reason
        self.score = score

class DataValidator:
    GARBAGE_PATTERNS = [
        re.compile(r'^[\s\[\]\(\)\{\}<>\.\,\?\!\"\'\;\:\-\_\@\#\$\%\^\&\*\+\=\\\/\|~`]{1,10}$'),
    ]

    def validate_input(self, text: str) -> ValidationResult:
        if not VALIDATOR_ENABLED:
            return ValidationResult(True, "validator disabled", 1.0)
        if not text or not text.strip():
            return ValidationResult(False, "empty input", 0.0)
        if len(text.strip()) < 2:
            return ValidationResult(False, "input too short", 0.1)
        for pat in self.GARBAGE_PATTERNS:
            if pat.match(text.strip()):
                return ValidationResult(False, "non-semantic input", 0.0)
        return ValidationResult(True, "", 1.0)

    def validate_output(self, text: str) -> ValidationResult:
        if not VALIDATOR_ENABLED:
            return ValidationResult(True, "validator disabled", 1.0)
        if not text or not text.strip():
            return ValidationResult(False, "empty response", 0.0)
        return ValidationResult(True, "", 1.0)