出于符合GDPR的原因,我正在尝试从语音到文本生成的混乱数据中删除社会安全号码(SSN)。下面是一个示例字符串(翻译成英文,解释了列出SSN时出现“and”的原因):
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"
我的目标是删除部分"thirteen ... forty "
,同时保留字符串中可能出现的其他数字,从而导致:
sample1_wo_ssn = "hello my name is sofie my social security number is and I live on mountain street number twelve"
社会保险号码的长度可能因数据生成方式的不同而不同(3-10个分开的号码)
我的做法:
"and"
分隔它们,并将它们与这3个数字后面的任何数字一起删除李>这是我的密码:
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)
输出:
[('13 0 4 5 and 70 18 7 and 40 and ', '', '', '', '5', 'and ', '70', '', '18', '', '7', 'and ', '40', 'and ', '', '', '', '', '')]
这就是我被困的地方
在本例中,我可以执行类似sample1_wh_ssn = re.sub(re_results[0][0],'',sample1_numb)
的操作来获得所需的结果,但这不会推广
任何帮助都将不胜感激
以下是您当前逻辑的实现,即:
1
到99
的字号转换为数字学分:
见Python code:
输出:
hello my name is sofie my social security number is and I live on mountain street number twelve
相关问题 更多 >
编程相关推荐