Coverage for presidio_analyzer / context_aware_enhancers / context_aware_enhancer.py: 94%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-29 09:03 +0000

1import logging 

2from abc import abstractmethod 

3from typing import List, Optional 

4 

5from presidio_analyzer import EntityRecognizer, RecognizerResult 

6from presidio_analyzer.nlp_engine import NlpArtifacts 

7 

8logger = logging.getLogger("presidio-analyzer") 

9 

10 

11class ContextAwareEnhancer: 

12 """ 

13 A class representing an abstract context aware enhancer. 

14 

15 Context words might enhance confidence score of a recognized entity, 

16 ContextAwareEnhancer is an abstract class to be inherited by a context aware 

17 enhancer logic. 

18 

19 :param context_similarity_factor: How much to enhance confidence of match entity 

20 :param min_score_with_context_similarity: Minimum confidence score 

21 :param context_prefix_count: how many words before the entity to match context 

22 :param context_suffix_count: how many words after the entity to match context 

23 """ 

24 

25 MIN_SCORE = 0 

26 MAX_SCORE = 1.0 

27 

28 def __init__( 

29 self, 

30 context_similarity_factor: float, 

31 min_score_with_context_similarity: float, 

32 context_prefix_count: int, 

33 context_suffix_count: int, 

34 ): 

35 self.context_similarity_factor = context_similarity_factor 

36 self.min_score_with_context_similarity = min_score_with_context_similarity 

37 self.context_prefix_count = context_prefix_count 

38 self.context_suffix_count = context_suffix_count 

39 

40 @abstractmethod 

41 def enhance_using_context( 

42 self, 

43 text: str, 

44 raw_results: List[RecognizerResult], 

45 nlp_artifacts: NlpArtifacts, 

46 recognizers: List[EntityRecognizer], 

47 context: Optional[List[str]] = None, 

48 ) -> List[RecognizerResult]: 

49 """ 

50 Update results in case surrounding words are relevant to the context words. 

51 

52 Using the surrounding words of the actual word matches, look 

53 for specific strings that if found contribute to the score 

54 of the result, improving the confidence that the match is 

55 indeed of that PII entity type 

56 

57 :param text: The actual text that was analyzed 

58 :param raw_results: Recognizer results which didn't take 

59 context into consideration 

60 :param nlp_artifacts: The nlp artifacts contains elements 

61 such as lemmatized tokens for better 

62 accuracy of the context enhancement process 

63 :param recognizers: the list of recognizers 

64 :param context: list of context words 

65 """ 

66 return raw_results