fork download
  1. import re
  2.  
  3. file_text = 'twenty-two\t22\nseventy-two thousand\t72000'
  4. listfile = [x.split('\t') for x in file_text.splitlines()]
  5. dictRessource = {str(rows[0]):str(rows[1]) for rows in listfile}
  6. regex = re.compile( "|".join([r"\b{}\b(?!((\s?\b\d\b\s?)|(\s?(hundred|thousand|mille|milliard|million|billion|trillion))?(\s?\(?\d?{}\)?)))".format(x.replace('-', r'[\s-]'), y) for x,y in dictRessource.items()]) , re.I)
  7.  
  8. def transform(line):
  9. return regex.sub(lambda match: dictRessource[match.group(0).lower().replace(' ', '-')], line)
  10.  
  11. print( transform("Some tWenty-two things and twEnty two ...") )
Success #stdin #stdout 0.04s 9668KB
stdin
Standard input is empty
stdout
Some 22 things and 22 ...