fork(1) download
  1. import re
  2.  
  3. def transform(line,file):
  4. pattern = re.compile("-")
  5. listfile = []
  6. ################
  7. csvfile = '''twenty-two\t22
  8. seventy-two thousand\t72000'''
  9. rscf = csvfile.splitlines()
  10. ######
  11. #with open(file,"r") as rscf :
  12. dictRessource = dict()
  13. for row in rscf:
  14. listfile.append(row.split('\t'))
  15. dictRessource = {str(rows[0]):str(rows[1]) for rows in listfile}
  16.  
  17. regex = "|".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()])
  18. return re.sub(f'{regex}', lambda match: dictRessource[match.group(0).lower().replace(' ', '-')], line, flags=re.IGNORECASE)
  19.  
  20. print( transform("Some tWenty-two things and twEnty two ...", "file") )
Success #stdin #stdout 0.03s 9436KB
stdin
Standard input is empty
stdout
Some 22 things and 22 ...