Coverage for presidio_analyzer / predefined_recognizers / country_specific / italy / it_identity_card_recognizer.py: 100%
9 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 List, Optional
3from presidio_analyzer import Pattern, PatternRecognizer
5# An Identity Card is a personal recognition document that is valid in Italy.
6# The paper-based identity card was issued for nearly 87 years until 2018
7# and can still be issued in case of emergency. The Electronic Identity Card
8# (CIE) is progressively replacing the paper-based identity card. It is
9# issued upon expiration of the paper-based identity card or in case of loss,
10# theft or deterioration.
11#
12# References:
13# - https://en.wikipedia.org/wiki/Italian_electronic_identity_card
14# - https://www.cartaidentita.interno.gov.it/en/cie/electronic-identity-card
17class ItIdentityCardRecognizer(PatternRecognizer):
18 """
19 Recognizes Italian Identity Card number using case-insensitive regex.
21 :param patterns: List of patterns to be used by this recognizer
22 :param context: List of context words to increase confidence in detection
23 :param supported_language: Language this recognizer supports
24 :param supported_entity: The entity this recognizer can detect
25 """
27 PATTERNS = [
28 Pattern(
29 "Paper-based Identity Card (very weak)",
30 # The number is composed of 2 letters, space (optional), 7 digits
31 r"(?i)\b[A-Z]{2}\s?\d{7}\b",
32 0.01,
33 ),
34 Pattern(
35 "Electronic Identity Card (CIE) 2.0 (very weak)",
36 r"(?i)\b\d{7}[A-Z]{2}\b",
37 0.01,
38 ),
39 Pattern(
40 "Electronic Identity Card (CIE) 3.0 (very weak)",
41 r"(?i)\b[A-Z]{2}\d{5}[A-Z]{2}\b",
42 0.01,
43 ),
44 ]
46 CONTEXT = [
47 "carta",
48 "identità",
49 "elettronica",
50 "cie",
51 "documento",
52 "riconoscimento",
53 "espatrio",
54 ]
56 def __init__(
57 self,
58 patterns: Optional[List[Pattern]] = None,
59 context: Optional[List[str]] = None,
60 supported_language: str = "it",
61 supported_entity: str = "IT_IDENTITY_CARD",
62 name: Optional[str] = None,
63 ):
64 patterns = patterns if patterns else self.PATTERNS
65 context = context if context else self.CONTEXT
66 super().__init__(
67 supported_entity=supported_entity,
68 patterns=patterns,
69 context=context,
70 supported_language=supported_language,
71 name=name,
72 )