aboutsummaryrefslogtreecommitdiffstats
path: root/examples/tools/redact.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tools/redact.py')
-rwxr-xr-xexamples/tools/redact.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/examples/tools/redact.py b/examples/tools/redact.py
index 7884d3b..af9201c 100755
--- a/examples/tools/redact.py
+++ b/examples/tools/redact.py
@@ -48,8 +48,11 @@ def luhn_ok(s):
RULES = [
("email", re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+"), None),
("iban", re.compile(r"\b[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?\b", re.I), iban_ok),
- ("codice fiscale", re.compile(r"\b[A-Z]{6}[0-9LMNPQRSTUV]{2}[ABCDEHLMPRST]"
- r"[0-9LMNPQRSTUV]{2}[A-Z][0-9LMNPQRSTUV]{3}[A-Z]\b", re.I), None),
+ # Any 16-character code mixing letters and digits, not the strict
+ # codice fiscale layout: OCR read a scanned "80" as "E0" and the strict
+ # pattern let it through. Other 16-character codes go too (safe side).
+ ("codice fiscale", re.compile(r"\b[A-Z0-9]{16}\b", re.I),
+ lambda s: sum(c.isdigit() for c in s) >= 3 and sum(c.isalpha() for c in s) >= 3),
("card", re.compile(r"(?<!\d)(?:\d[ -]?){12,18}\d(?!\d)"), luhn_ok),
# Mobile (3xx), landline (0x, optional parentheses), or +international.
("phone", re.compile(r"(?<![\w+])(?:\+\d{1,3}[ .-]?\d[\d .-]{5,13}\d"
@@ -357,6 +360,9 @@ def test():
assert marked("Sig. D\u2019Angelo", ["D'Angelo"])[1] == {"items": 1}
assert marked("Sig. D'Angelo", ["D\u2019Angelo"])[1] == {"items": 1}
assert marked("it60x0542811101000000123456 rssmra85t10a562s")[1] == {"iban": 1, "codice fiscale": 1}
+ # An OCR misread codice fiscale; a 16-letter word is not one.
+ assert marked("CF NREMRCE0A01F205X")[1] == {"codice fiscale": 1}
+ assert not marked("rappresentazione")[1]
with tempfile.TemporaryDirectory() as d:
env = {**os.environ, "REDACT_VIEWER": "true"}
# Text outside the CropBox is not in the page, so not in the copy.