-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
508e655
commit 51af1fe
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from mask import Mask | ||
|
||
if __name__ == "__main__": | ||
mask_pii = Mask() | ||
try: | ||
masked_text = mask_pii.mask_file('file.txt') | ||
print("Masked Text: " + masked_text) | ||
except: | ||
print('Unexpected error!!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .mask import Mask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import spacy | ||
import re | ||
|
||
class Mask: | ||
def __init__(self) -> None: | ||
self.patterns = { | ||
'phone': r'(?:\+\d{1,3}[-\s]?)?\(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4}', | ||
'email': r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', | ||
'ipv6': r'\b(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\b', | ||
'ipv4': r'\b((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}\b' | ||
} | ||
|
||
def mask_text(self,text:str) -> str: | ||
|
||
for category,pattern in self.patterns.items(): | ||
text = re.sub(pattern, f'[REDACTED {category.upper()}]', text) | ||
|
||
text = self.mask_nlp(text) | ||
|
||
return text | ||
|
||
def mask_nlp(self,text:str) -> str: | ||
nlp = spacy.load("en_core_web_sm") | ||
doc = nlp(text) | ||
|
||
for ent in doc.ents: | ||
print(f'Label: {ent.label_}: Value: {ent.text}') | ||
text = re.sub(ent.text,f'[REDACTED {ent.label_}]',text) | ||
return text | ||
|
||
def mask_file(self,file_name:str) -> str: | ||
print('File Name: ' + file_name) | ||
with open(file_name,'r') as file: | ||
file_text = file.read() | ||
return self.mask_text(file_text) |