utils.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import csv
  2. import os
  3. UNIT_1e01 = '十'
  4. UNIT_1e02 = '百'
  5. UNIT_1e03 = '千'
  6. UNIT_1e04 = '万'
  7. def get_abs_path(rel_path):
  8. """
  9. Get absolute path
  10. Args:
  11. rel_path: relative path to this file
  12. Returns absolute path
  13. """
  14. return os.path.dirname(os.path.abspath(__file__)) + '/' + rel_path
  15. def load_labels(abs_path):
  16. """
  17. loads relative path file as dictionary
  18. Args:
  19. abs_path: absolute path
  20. Returns dictionary of mappings
  21. """
  22. label_tsv = open(abs_path, encoding="utf-8")
  23. labels = list(csv.reader(label_tsv, delimiter="\t"))
  24. return labels
  25. def augment_labels_with_punct_at_end(labels):
  26. """
  27. augments labels: if key ends on a punctuation that value does not have, add a new label
  28. where the value maintains the punctuation
  29. Args:
  30. labels : input labels
  31. Returns:
  32. additional labels
  33. """
  34. res = []
  35. for label in labels:
  36. if len(label) > 1:
  37. if label[0][-1] == "." and label[1][-1] != ".":
  38. res.append([label[0], label[1] + "."] + label[2:])
  39. return res