Coverage for presidio_analyzer / analysis_explanation.py: 85%
26 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 09:03 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 09:03 +0000
1from typing import Dict
4class AnalysisExplanation:
5 """
6 Hold tracing information to explain why PII entities were identified as such.
8 :param recognizer: name of recognizer that made the decision
9 :param original_score: recognizer's confidence in result
10 :param pattern_name: name of pattern
11 (if decision was made by a PatternRecognizer)
12 :param pattern: regex pattern that was applied (if PatternRecognizer)
13 :param validation_result: result of a validation (e.g. checksum)
14 :param textual_explanation: Free text for describing
15 a decision of a logic or model
16 """
18 def __init__(
19 self,
20 recognizer: str,
21 original_score: float,
22 pattern_name: str = None,
23 pattern: str = None,
24 validation_result: bool = None,
25 textual_explanation: str = None,
26 regex_flags: int = None,
27 ):
28 self.recognizer = recognizer
29 self.pattern_name = pattern_name
30 self.pattern = pattern
31 self.original_score = original_score
32 self.score = original_score
33 self.textual_explanation = textual_explanation
34 self.score_context_improvement = 0
35 self.supportive_context_word = ""
36 self.validation_result = validation_result
37 self.regex_flags = regex_flags
39 def __repr__(self):
40 """Create string representation of the object."""
41 return str(self.__dict__)
43 def set_improved_score(self, score: float) -> None:
44 """Update the score and calculate the difference from the original score."""
45 self.score = score
46 self.score_context_improvement = self.score - self.original_score
48 def set_supportive_context_word(self, word: str) -> None:
49 """Set the context word which helped increase the score."""
50 self.supportive_context_word = word
52 def append_textual_explanation_line(self, text: str) -> None:
53 """Append a new line to textual_explanation field."""
54 if self.textual_explanation is None:
55 self.textual_explanation = text
56 else:
57 self.textual_explanation = f"{self.textual_explanation}\n{text}"
59 def to_dict(self) -> Dict:
60 """
61 Serialize self to dictionary.
63 :return: a dictionary
64 """
65 return self.__dict__