-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
25 lines (23 loc) · 859 Bytes
/
utils.py
File metadata and controls
25 lines (23 loc) · 859 Bytes
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
from patterns import pii_patterns
def redact_text(text):
for label, pattern in pii_patterns.items():
if label == "CREDIT CARD":
matches = pattern.findall(text)
for match in matches:
if luhn_check(match):
text = text.replace(match, f"[REDACTED {label}]")
# replace all matches of a pattern with a [REDACTED] label
text = pattern.sub(f"[REDACTED {label}]", text)
return text
# validates a credit card number using the Luhn algorithm
def luhn_check(number: str) -> bool:
digits = [int(d) for d in number if d.isdigit()]
checksum = 0
parity = len(digits) % 2
for i, digit in enumerate(digits):
if i % 2 == parity:
digit *= 2
if digit > 9:
digit -= 9
checksum += digit
return checksum % 10 == 0