Team Ai
Apppublic

pranee31/emailClassification

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
utils.py46 linesDownload Raw Back to root
1import re2import spacy3 4nlp = spacy.load("en_core_web_sm")5 6PII_PATTERNS = {7    "email": r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+",8    "phone_number": r"(\+91[\-\s]?)?[0]?(91)?[789]\d{9}",9    "aadhar_num": r"\b\d{4}[\s-]?\d{4}[\s-]?\d{4}\b",10    "credit_debit_no": r"\b(?:\d[ -]*?){13,16}\b",11    "cvv_no": r"\b[0-9]{3}\b",12    "expiry_no": r"\b(0[1-9]|1[0-2])\/?([0-9]{2})\b",13    "dob": r"\b(0?[1-9]|[12][0-9]|3[01])[\/\-\s](0?[1-9]|1[0-2])[\/\-\s](\d{4})\b"14}15 16def mask_pii(text):17    masked_text = text18    entities = []19 20    for label, pattern in PII_PATTERNS.items():21        for match in re.finditer(pattern, masked_text):22            start, end = match.span()23            entity_val = match.group()24            placeholder = f"[{label}]"25            masked_text = masked_text[:start] + placeholder + masked_text[end:]26            entities.append({27                "position": [start, start + len(placeholder)],28                "classification": label,29                "entity": entity_val30            })31 32    doc = nlp(text)33    for ent in doc.ents:34        if ent.label_ == "PERSON":35            start = masked_text.find(ent.text)36            if start != -1:37                end = start + len(ent.text)38                placeholder = "[full_name]"39                masked_text = masked_text[:start] + placeholder + masked_text[end:]40                entities.append({41                    "position": [start, start + len(placeholder)],42                    "classification": "full_name",43                    "entity": ent.text44                })45 46    return masked_text, entities