Commit
·
764e30e
1
Parent(s):
5574ac6
Fix: Add remove_pii method to FileService
Browse files- src/api/routes.py +13 -5
- src/services/file_service.py +17 -108
- src/services/regex_pii_remover.py +46 -204
src/api/routes.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import logging
|
| 2 |
from fastapi import APIRouter, HTTPException, UploadFile, File
|
| 3 |
-
from models.request_models import ProviderNotesRequest
|
| 4 |
-
from models.response_models import CodingResponse
|
| 5 |
from services.groq_service import groq_service
|
| 6 |
from services.file_service import file_service
|
| 7 |
|
|
@@ -74,9 +74,14 @@ async def upload_provider_notes_file(file: UploadFile = File(...)):
|
|
| 74 |
logger.info(f"📄 File read successfully (length: {len(text)})")
|
| 75 |
|
| 76 |
# Remove PII
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Analyze with Groq
|
| 82 |
result = groq_service.analyze_provider_notes(cleaned_text)
|
|
@@ -99,6 +104,9 @@ async def upload_provider_notes_file(file: UploadFile = File(...)):
|
|
| 99 |
|
| 100 |
except HTTPException:
|
| 101 |
raise
|
|
|
|
|
|
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
logger.error(f"❌ Error processing uploaded file: {str(e)}", exc_info=True)
|
| 104 |
raise HTTPException(status_code=500, detail=f"Error processing uploaded file: {str(e)}")
|
|
|
|
| 1 |
import logging
|
| 2 |
from fastapi import APIRouter, HTTPException, UploadFile, File
|
| 3 |
+
from models.request_models import ProviderNotesRequest
|
| 4 |
+
from models.response_models import CodingResponse, FileUploadResponse
|
| 5 |
from services.groq_service import groq_service
|
| 6 |
from services.file_service import file_service
|
| 7 |
|
|
|
|
| 74 |
logger.info(f"📄 File read successfully (length: {len(text)})")
|
| 75 |
|
| 76 |
# Remove PII
|
| 77 |
+
try:
|
| 78 |
+
cleaned_text, pii_count = file_service.remove_pii(text)
|
| 79 |
+
logger.info(f"🔒 PII removal complete: {pii_count} entities removed")
|
| 80 |
+
except Exception as pii_error:
|
| 81 |
+
logger.error(f"⚠️ PII removal failed: {str(pii_error)}")
|
| 82 |
+
# Continue without PII removal if it fails
|
| 83 |
+
cleaned_text = text
|
| 84 |
+
pii_count = 0
|
| 85 |
|
| 86 |
# Analyze with Groq
|
| 87 |
result = groq_service.analyze_provider_notes(cleaned_text)
|
|
|
|
| 104 |
|
| 105 |
except HTTPException:
|
| 106 |
raise
|
| 107 |
+
except UnicodeDecodeError:
|
| 108 |
+
logger.error("❌ File encoding error")
|
| 109 |
+
raise HTTPException(status_code=400, detail="File must be UTF-8 encoded text")
|
| 110 |
except Exception as e:
|
| 111 |
logger.error(f"❌ Error processing uploaded file: {str(e)}", exc_info=True)
|
| 112 |
raise HTTPException(status_code=500, detail=f"Error processing uploaded file: {str(e)}")
|
src/services/file_service.py
CHANGED
|
@@ -2,132 +2,41 @@ from fastapi import UploadFile, HTTPException
|
|
| 2 |
import os
|
| 3 |
from typing import Dict
|
| 4 |
import logging
|
| 5 |
-
from services.regex_pii_remover import
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
|
| 10 |
class FileService:
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
|
| 15 |
-
|
| 16 |
-
@staticmethod
|
| 17 |
-
def validate_file(file: UploadFile) -> None:
|
| 18 |
-
"""
|
| 19 |
-
Validate uploaded file
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
file: Uploaded file object
|
| 23 |
-
|
| 24 |
-
Raises:
|
| 25 |
-
HTTPException: If file is invalid
|
| 26 |
-
"""
|
| 27 |
-
if not file:
|
| 28 |
-
raise HTTPException(status_code=400, detail="No file provided")
|
| 29 |
-
|
| 30 |
-
file_ext = os.path.splitext(file.filename)[1].lower()
|
| 31 |
-
if file_ext not in FileService.ALLOWED_EXTENSIONS:
|
| 32 |
-
raise HTTPException(
|
| 33 |
-
status_code=400,
|
| 34 |
-
detail=f"Invalid file type. Only {', '.join(FileService.ALLOWED_EXTENSIONS)} files are allowed"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
@staticmethod
|
| 38 |
-
async def extract_text_from_file(file: UploadFile, remove_pii: bool = True) -> Dict[str, any]:
|
| 39 |
"""
|
| 40 |
-
|
| 41 |
|
| 42 |
Args:
|
| 43 |
-
|
| 44 |
-
remove_pii: Whether to remove PII from extracted text (default: True)
|
| 45 |
|
| 46 |
Returns:
|
| 47 |
-
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
FileService.validate_file(file)
|
| 52 |
-
|
| 53 |
-
# Read file content
|
| 54 |
-
content = await file.read()
|
| 55 |
-
|
| 56 |
-
# Check file size
|
| 57 |
-
file_size = len(content)
|
| 58 |
-
if file_size > FileService.MAX_FILE_SIZE:
|
| 59 |
-
raise HTTPException(
|
| 60 |
-
status_code=400,
|
| 61 |
-
detail=f"File too large. Maximum size is {FileService.MAX_FILE_SIZE / (1024*1024)} MB"
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
# Decode text
|
| 65 |
-
try:
|
| 66 |
-
text = content.decode('utf-8')
|
| 67 |
-
except UnicodeDecodeError:
|
| 68 |
-
try:
|
| 69 |
-
text = content.decode('latin-1')
|
| 70 |
-
except Exception as e:
|
| 71 |
-
raise HTTPException(
|
| 72 |
-
status_code=400,
|
| 73 |
-
detail="Unable to decode file. Please ensure it's a valid text file"
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
# Validate extracted text
|
| 77 |
-
if not text.strip():
|
| 78 |
-
raise HTTPException(
|
| 79 |
-
status_code=400,
|
| 80 |
-
detail="File is empty or contains no readable text"
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
if len(text.strip()) < 10:
|
| 84 |
-
raise HTTPException(
|
| 85 |
-
status_code=400,
|
| 86 |
-
detail="Extracted text is too short. Please provide more detailed provider notes"
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
logger.info(f"Successfully extracted {len(text)} characters from {file.filename}")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
pii_info = {
|
| 93 |
-
"pii_removed": False,
|
| 94 |
-
"pii_count": 0,
|
| 95 |
-
"pii_details": []
|
| 96 |
-
}
|
| 97 |
|
| 98 |
-
|
| 99 |
-
logger.info("Removing PII from extracted text using regex patterns...")
|
| 100 |
-
pii_result = regex_pii_remover.sanitize_provider_notes(text)
|
| 101 |
-
|
| 102 |
-
text = pii_result["sanitized_notes"]
|
| 103 |
-
pii_info = {
|
| 104 |
-
"pii_removed": pii_result["was_pii_found"],
|
| 105 |
-
"pii_count": pii_result["pii_removed_count"],
|
| 106 |
-
"pii_details": pii_result["pii_details"]
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
if pii_result["was_pii_found"]:
|
| 110 |
-
logger.info(f"Removed {pii_result['pii_removed_count']} PII entities using regex")
|
| 111 |
-
else:
|
| 112 |
-
logger.info("No PII detected in text")
|
| 113 |
|
| 114 |
-
return
|
| 115 |
-
"text": text,
|
| 116 |
-
"filename": file.filename,
|
| 117 |
-
"file_size": file_size,
|
| 118 |
-
"text_length": len(text),
|
| 119 |
-
"pii_info": pii_info
|
| 120 |
-
}
|
| 121 |
|
| 122 |
-
except HTTPException:
|
| 123 |
-
raise
|
| 124 |
except Exception as e:
|
| 125 |
-
logger.error(f"Error
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
detail=f"Error processing file: {str(e)}"
|
| 129 |
-
)
|
| 130 |
|
| 131 |
|
| 132 |
-
#
|
| 133 |
file_service = FileService()
|
|
|
|
| 2 |
import os
|
| 3 |
from typing import Dict
|
| 4 |
import logging
|
| 5 |
+
from services.regex_pii_remover import RegexPIIRemover
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
|
| 10 |
class FileService:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
"""Initialize file service with PII remover"""
|
| 13 |
+
self.pii_remover = RegexPIIRemover()
|
| 14 |
+
logger.info("✅ FileService initialized")
|
| 15 |
|
| 16 |
+
def remove_pii(self, text: str) -> tuple[str, int]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
+
Remove PII from text using regex patterns
|
| 19 |
|
| 20 |
Args:
|
| 21 |
+
text: Input text containing potential PII
|
|
|
|
| 22 |
|
| 23 |
Returns:
|
| 24 |
+
tuple: (cleaned_text, pii_count)
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
+
logger.info(f"🔒 Starting PII removal (text length: {len(text)})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
cleaned_text, pii_count = self.pii_remover.remove_pii(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
logger.info(f"✅ PII removal complete: {pii_count} entities removed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
return cleaned_text, pii_count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
logger.error(f"❌ Error during PII removal: {str(e)}")
|
| 37 |
+
# Return original text if PII removal fails
|
| 38 |
+
return text, 0
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
+
# Global instance
|
| 42 |
file_service = FileService()
|
src/services/regex_pii_remover.py
CHANGED
|
@@ -1,229 +1,71 @@
|
|
| 1 |
import re
|
| 2 |
import logging
|
| 3 |
-
from typing import Dict, List, Tuple
|
| 4 |
|
| 5 |
logger = logging.getLogger(__name__)
|
| 6 |
|
| 7 |
|
| 8 |
class RegexPIIRemover:
|
| 9 |
-
"""
|
| 10 |
-
Lightweight regex-based PII detection and removal service
|
| 11 |
-
Detects and removes common personal information from medical notes
|
| 12 |
-
"""
|
| 13 |
|
| 14 |
def __init__(self):
|
| 15 |
-
"""Initialize
|
| 16 |
-
|
| 17 |
-
# Pattern definitions with descriptions
|
| 18 |
self.patterns = {
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
'replacement': '[PHONE_REDACTED]',
|
| 22 |
-
'description': 'Phone numbers'
|
| 23 |
-
},
|
| 24 |
-
'EMAIL': {
|
| 25 |
-
'pattern': r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b',
|
| 26 |
-
'replacement': '[EMAIL_REDACTED]',
|
| 27 |
-
'description': 'Email addresses'
|
| 28 |
-
},
|
| 29 |
-
'SSN': {
|
| 30 |
-
'pattern': r'\b\d{3}-\d{2}-\d{4}\b',
|
| 31 |
-
'replacement': '[SSN_REDACTED]',
|
| 32 |
-
'description': 'Social Security Numbers'
|
| 33 |
-
},
|
| 34 |
-
'DATE_OF_BIRTH': {
|
| 35 |
-
'pattern': r'\b(0?[1-9]|1[0-2])[/-](0?[1-9]|[12][0-9]|3[01])[/-](19|20)\d{2}\b',
|
| 36 |
-
'replacement': '[DOB_REDACTED]',
|
| 37 |
-
'description': 'Dates of birth'
|
| 38 |
-
},
|
| 39 |
-
'ZIP_CODE': {
|
| 40 |
-
'pattern': r'\b\d{5}(?:-\d{4})?\b',
|
| 41 |
-
'replacement': '[ZIP_REDACTED]',
|
| 42 |
-
'description': 'ZIP codes'
|
| 43 |
-
},
|
| 44 |
-
'CREDIT_CARD': {
|
| 45 |
-
'pattern': r'\b(?:\d{4}[-\s]?){3}\d{4}\b',
|
| 46 |
-
'replacement': '[CARD_REDACTED]',
|
| 47 |
-
'description': 'Credit card numbers'
|
| 48 |
-
},
|
| 49 |
-
'IP_ADDRESS': {
|
| 50 |
-
'pattern': r'\b(?:\d{1,3}\.){3}\d{1,3}\b',
|
| 51 |
-
'replacement': '[IP_REDACTED]',
|
| 52 |
-
'description': 'IP addresses'
|
| 53 |
-
},
|
| 54 |
-
'STREET_ADDRESS': {
|
| 55 |
-
'pattern': r'\b\d{1,5}\s+([A-Z][a-z]+\s*){1,3}(Street|St|Avenue|Ave|Road|Rd|Boulevard|Blvd|Lane|Ln|Drive|Dr|Court|Ct|Way)\b',
|
| 56 |
-
'replacement': '[ADDRESS_REDACTED]',
|
| 57 |
-
'description': 'Street addresses'
|
| 58 |
-
}
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
# Medical note specific patterns
|
| 62 |
-
self.medical_patterns = {
|
| 63 |
-
'PATIENT_NAME_LABEL': {
|
| 64 |
-
'pattern': r'(Patient|Pt|Patient Name|Name):\s*([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)',
|
| 65 |
-
'replacement': r'\1: [NAME_REDACTED]',
|
| 66 |
-
'description': 'Patient names after labels'
|
| 67 |
-
},
|
| 68 |
-
'DOB_LABEL': {
|
| 69 |
-
'pattern': r'(DOB|Date of Birth|Birth Date|Birthdate):\s*[\d/\-]+',
|
| 70 |
-
'replacement': r'\1: [DOB_REDACTED]',
|
| 71 |
-
'description': 'DOB after labels'
|
| 72 |
-
},
|
| 73 |
-
'PHONE_LABEL': {
|
| 74 |
-
'pattern': r'(Phone|Tel|Telephone|Cell|Mobile|Contact):\s*[\d\s\-\(\)\.]+',
|
| 75 |
-
'replacement': r'\1: [PHONE_REDACTED]',
|
| 76 |
-
'description': 'Phone numbers after labels'
|
| 77 |
-
},
|
| 78 |
-
'ADDRESS_LABEL': {
|
| 79 |
-
'pattern': r'(Address|Addr|Home Address|Mailing Address):\s*[^\n]+',
|
| 80 |
-
'replacement': r'\1: [ADDRESS_REDACTED]',
|
| 81 |
-
'description': 'Addresses after labels'
|
| 82 |
-
},
|
| 83 |
-
'MRN_LABEL': {
|
| 84 |
-
'pattern': r'(MRN|Medical Record Number|Record #|Patient ID|ID):\s*[\w\d\-]+',
|
| 85 |
-
'replacement': r'\1: [MRN_REDACTED]',
|
| 86 |
-
'description': 'Medical record numbers'
|
| 87 |
-
},
|
| 88 |
-
'GUARDIAN_INFO': {
|
| 89 |
-
'pattern': r'(Guardian|Emergency Contact|Next of Kin):\s*[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*',
|
| 90 |
-
'replacement': r'\1: [CONTACT_REDACTED]',
|
| 91 |
-
'description': 'Guardian/emergency contact names'
|
| 92 |
-
}
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
logger.info("Regex PII Remover initialized with pattern-based detection")
|
| 96 |
-
|
| 97 |
-
def detect_pii(self, text: str) -> List[Dict]:
|
| 98 |
-
"""
|
| 99 |
-
Detect PII entities in text using regex patterns
|
| 100 |
-
|
| 101 |
-
Args:
|
| 102 |
-
text: Input text to analyze
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
"""
|
| 107 |
-
findings = []
|
| 108 |
-
|
| 109 |
-
# Check general patterns
|
| 110 |
-
for entity_type, config in self.patterns.items():
|
| 111 |
-
matches = re.finditer(config['pattern'], text)
|
| 112 |
-
for match in matches:
|
| 113 |
-
findings.append({
|
| 114 |
-
'entity_type': entity_type,
|
| 115 |
-
'text': match.group(),
|
| 116 |
-
'start': match.start(),
|
| 117 |
-
'end': match.end(),
|
| 118 |
-
'description': config['description']
|
| 119 |
-
})
|
| 120 |
-
|
| 121 |
-
# Check medical-specific patterns
|
| 122 |
-
for entity_type, config in self.medical_patterns.items():
|
| 123 |
-
matches = re.finditer(config['pattern'], text, re.IGNORECASE)
|
| 124 |
-
for match in matches:
|
| 125 |
-
findings.append({
|
| 126 |
-
'entity_type': entity_type,
|
| 127 |
-
'text': match.group(),
|
| 128 |
-
'start': match.start(),
|
| 129 |
-
'end': match.end(),
|
| 130 |
-
'description': config['description']
|
| 131 |
-
})
|
| 132 |
-
|
| 133 |
-
logger.info(f"Detected {len(findings)} PII entities using regex patterns")
|
| 134 |
-
return findings
|
| 135 |
-
|
| 136 |
-
def remove_pii(self, text: str) -> Dict[str, any]:
|
| 137 |
-
"""
|
| 138 |
-
Remove PII from text using regex patterns
|
| 139 |
-
|
| 140 |
-
Args:
|
| 141 |
-
text: Input text containing potential PII
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
"""
|
| 146 |
-
try:
|
| 147 |
-
original_text = text
|
| 148 |
-
sanitized_text = text
|
| 149 |
-
total_replacements = 0
|
| 150 |
-
replacement_details = []
|
| 151 |
-
|
| 152 |
-
# Apply general PII patterns
|
| 153 |
-
for entity_type, config in self.patterns.items():
|
| 154 |
-
matches = list(re.finditer(config['pattern'], sanitized_text))
|
| 155 |
-
if matches:
|
| 156 |
-
count = len(matches)
|
| 157 |
-
total_replacements += count
|
| 158 |
-
replacement_details.append({
|
| 159 |
-
'type': entity_type,
|
| 160 |
-
'count': count,
|
| 161 |
-
'description': config['description']
|
| 162 |
-
})
|
| 163 |
-
sanitized_text = re.sub(config['pattern'], config['replacement'], sanitized_text)
|
| 164 |
-
logger.info(f"Removed {count} {config['description']}")
|
| 165 |
|
| 166 |
-
#
|
| 167 |
-
|
| 168 |
-
matches = list(re.finditer(config['pattern'], sanitized_text, re.IGNORECASE))
|
| 169 |
-
if matches:
|
| 170 |
-
count = len(matches)
|
| 171 |
-
total_replacements += count
|
| 172 |
-
replacement_details.append({
|
| 173 |
-
'type': entity_type,
|
| 174 |
-
'count': count,
|
| 175 |
-
'description': config['description']
|
| 176 |
-
})
|
| 177 |
-
sanitized_text = re.sub(config['pattern'], config['replacement'], sanitized_text, flags=re.IGNORECASE)
|
| 178 |
-
logger.info(f"Removed {count} {config['description']}")
|
| 179 |
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
else:
|
| 185 |
-
logger.info("No PII detected in text")
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
'original_text': original_text,
|
| 190 |
-
'was_pii_removed': was_pii_removed,
|
| 191 |
-
'pii_count': total_replacements,
|
| 192 |
-
'pii_detected': replacement_details
|
| 193 |
-
}
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
'was_pii_removed': False,
|
| 201 |
-
'pii_count': 0,
|
| 202 |
-
'pii_detected': [],
|
| 203 |
-
'error': str(e)
|
| 204 |
-
}
|
| 205 |
|
| 206 |
-
def
|
| 207 |
"""
|
| 208 |
-
|
| 209 |
-
Main entry point for file processing
|
| 210 |
|
| 211 |
Args:
|
| 212 |
-
|
| 213 |
|
| 214 |
Returns:
|
| 215 |
-
|
| 216 |
"""
|
| 217 |
-
|
| 218 |
-
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
#
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import logging
|
|
|
|
| 3 |
|
| 4 |
logger = logging.getLogger(__name__)
|
| 5 |
|
| 6 |
|
| 7 |
class RegexPIIRemover:
|
| 8 |
+
"""Remove PII using regex patterns"""
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def __init__(self):
|
| 11 |
+
"""Initialize PII removal patterns"""
|
|
|
|
|
|
|
| 12 |
self.patterns = {
|
| 13 |
+
# Social Security Numbers
|
| 14 |
+
'ssn': re.compile(r'\b\d{3}-\d{2}-\d{4}\b'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Phone numbers
|
| 17 |
+
'phone': re.compile(r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Email addresses
|
| 20 |
+
'email': re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Dates (MM/DD/YYYY, MM-DD-YYYY, etc.)
|
| 23 |
+
'date': re.compile(r'\b\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Medical Record Numbers (MRN)
|
| 26 |
+
'mrn': re.compile(r'\b(MRN|Medical Record Number)[:\s]+\w+\b', re.IGNORECASE),
|
| 27 |
|
| 28 |
+
# ZIP codes
|
| 29 |
+
'zip': re.compile(r'\b\d{5}(-\d{4})?\b'),
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Names (simple pattern - captures "Patient: John Doe" or "Name: Jane Smith")
|
| 32 |
+
'patient_name': re.compile(r'(Patient|Name)[:\s]+([A-Z][a-z]+\s[A-Z][a-z]+)', re.IGNORECASE),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Date of Birth
|
| 35 |
+
'dob': re.compile(r'(DOB|Date of Birth)[:\s]+\d{1,2}[/-]\d{1,2}[/-]\d{2,4}', re.IGNORECASE),
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
logger.info(f"✅ RegexPIIRemover initialized with {len(self.patterns)} patterns")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def remove_pii(self, text: str) -> tuple[str, int]:
|
| 41 |
"""
|
| 42 |
+
Remove PII from text
|
|
|
|
| 43 |
|
| 44 |
Args:
|
| 45 |
+
text: Input text
|
| 46 |
|
| 47 |
Returns:
|
| 48 |
+
tuple: (cleaned_text, count_of_pii_removed)
|
| 49 |
"""
|
| 50 |
+
cleaned_text = text
|
| 51 |
+
total_removed = 0
|
| 52 |
|
| 53 |
+
for pii_type, pattern in self.patterns.items():
|
| 54 |
+
matches = pattern.findall(cleaned_text)
|
| 55 |
+
count = len(matches)
|
| 56 |
+
|
| 57 |
+
if count > 0:
|
| 58 |
+
logger.debug(f"Found {count} instances of {pii_type}")
|
| 59 |
+
total_removed += count
|
| 60 |
+
|
| 61 |
+
# Replace with redacted placeholder
|
| 62 |
+
if pii_type == 'patient_name':
|
| 63 |
+
cleaned_text = pattern.sub(r'\1: [REDACTED]', cleaned_text)
|
| 64 |
+
elif pii_type in ['dob', 'mrn']:
|
| 65 |
+
cleaned_text = pattern.sub(r'\1: [REDACTED]', cleaned_text)
|
| 66 |
+
else:
|
| 67 |
+
cleaned_text = pattern.sub('[REDACTED]', cleaned_text)
|
| 68 |
+
|
| 69 |
+
logger.info(f"🔒 Removed {total_removed} PII entities")
|
| 70 |
+
|
| 71 |
+
return cleaned_text, total_removed
|