Team Ai
Apppublic

Shanmukha2491/Email_Classification

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
utils.py49 linesDownload Raw Back to root
1import re2 3# Dictionary containing patterns to identify different types of Personally Identifiable Information (PII)4PII_PATTERNS = {5    "full_name": r"\b[A-Z][a-z]+ [A-Z][a-z]+\b",                        # Example: John Doe6    "email": r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}",         # Example: john.doe@example.com7    "phone_number": r"\b\d{10}\b",                                     # Example: 98765432108    "dob": r"\b\d{2}/\d{2}/\d{4}\b",                                   # Example: 01/01/20009    "aadhar_num": r"\b\d{4} \d{4} \d{4}\b",                            # Example: 1234 5678 901210    "credit_debit_no": r"\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b",     # Example: 1234 5678 9012 3456 or 1234-5678-9012-345611    "cvv_no": r"\b\d{3}\b",                                            # Example: 12312    "expiry_no": r"\b(0[1-9]|1[0-2])\/\d{2,4}\b"                        # Example: 01/2513}14 15def mask_email(email_text):16    """17    This function scans the given email text for PII (Personally Identifiable Information),18    masks detected entities with placeholders, and returns both the masked text and a list of detected entities.19    20    Parameters:21    - email_text (str): The body of the email to scan and mask.22 23    Returns:24    - masked_email (str): The email text with sensitive data masked.25    - entity_list (list): A list of dictionaries containing position, classification, and actual entity value.26    """27 28    entity_list = []            # List to store details of each detected entity29    masked_email = email_text   # We'll apply masking to this variable (original remains untouched)30 31    # Loop through each type of PII and its associated regex pattern32    for entity, pattern in PII_PATTERNS.items():33        # Find all matches for the current pattern in the text34        for match in re.finditer(pattern, masked_email):35            start, end = match.span()         # Get start and end positions of the match36            entity_value = match.group()      # The actual matched text37 38            # Save the match details in a structured way39            entity_list.append({40                "position": [start, end],41                "classification": entity,42                "entity": entity_value43            })44 45            # Replace the matched value in the text with a placeholder46            masked_email = masked_email.replace(entity_value, f"[{entity}]")47 48    return masked_email, entity_list49