hishn12/Email_Classification_without_LLM
1
1import re2from typing import List, Dict3 4class PIIDetector:5 """Detects PII using regex only (no spaCy or LLMs)."""6 7 def __init__(self):8 self.patterns = {9 'full_name': r'\b[A-Z][a-z]+(?:\s[A-Z][a-z]+)+\b',10 'email': r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b',11 # 'phone_number': r'\b(?:\+91[\-\s]?|0)?[6-9]\d{9}\b',12 'dob': r'\b\d{2}[/-]\d{2}[/-]\d{4}\b',13 'aadhar_num': r'\b\d{4}[\s-]?\d{4}[\s-]?\d{4}\b',14 'credit_debit_no': r'\b(?:\d[ -]*?){13,16}\b',15 'cvv_no': r'\b\d{3}\b', # Just 3-digit (standalone), improved16 'expiry_no': r'\b(0[1-9]|1[0-2])\/([0-9]{2,4})\b' # MM/YY or MM/YYYY17 }18 19 def detect(self, text: str) -> Dict[str, List[str]]:20 findings = {}21 22 for pii_type, pattern in self.patterns.items():23 matches = re.findall(pattern, text)24 25 values = []26 for m in matches:27 if isinstance(m, tuple):28 m = "/".join(m)29 if isinstance(m, str) and m.strip(): # ✅ prevent empty strings30 values.append(m)31 32 if values:33 findings[pii_type] = values34 35 return findings36 