I'm trying to remove social security numbers (SSN) for GDPR compliant reasons from messy data generated with speech-to-text. Here is a sample string (translated to English which explains why 'and' occurs when the SSN are listed):
sample1 = "hello my name is sofie my social security number is thirteen zero four five and seventy eighteen seven and forty and I live on mountain street number twelve"
My goal is to remove the part "thirteen ... forty "
while keeping other numbers that may appear in the string resulting in:
sample1_wo_ssn = "hello my name is sofie my social security number is and I live on mountain street number twelve"
The length of the social security number can vary as a consequence of how data is generated (3-10 separated numbers).
My approach:
- Replace written numbers with digits using a dict
- Use regex to find where 3 or more numbers occur with only whitespace or
"and"
separating them and remove these together with any number following these 3 numbers.
Here is my code:
import re
number_dict = {
'zero': '0',
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9',
'ten': '10',
'eleven': '11',
'twelve': '12',
'thirteen': '13',
'fourteen': '14',
'fifteen': '15',
'sixteen': '16',
'seventeen': '17',
'eighteen': '18',
'nineteen': '19',
'twenty': '20',
'thirty': '30',
'forty': '40',
'fifty': '50',
'sixty': '60',
'seventy': '70',
'eighty': '80',
'ninety': '90'
}
sample1 = "hello my name is sofie my social security number is thirteen zero four five and seventy eighteen seven and forty and I live on mountain street number twelve"
sample1_temp = [number_dict.get(item,item) for item in sample1.split()]
sample1_numb = ' '.join(sample1_temp)
re_results = re.findall(r'(\d+ (and\s)?\d+ (and\s)?\d+\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?\s?(and\s)?(\d+)?)', sample1_numb)
print(re_results)
Output:
[('13 0 4 5 and 70 18 7 and 40 and ', '', '', '', '5', 'and ', '70', '', '18', '', '7', 'and ', '40', 'and ', '', '', '', '', '')]
This is where I'm stuck.
In this example I could do something like sample1_wh_ssn = re.sub(re_results[0][0],'',sample1_numb)
to get the desired result, but this will not generalize.
Any help would be greatly appreciated.