utils.py 1.0 KB

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