You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 916 B

123456789101112131415161718192021222324
  1. from typing import List, Set, Tuple, Union, Callable
  2. import numpy as np
  3. from scipy.optimize import linear_sum_assignment
  4. def _align_bags(
  5. predicted: List[Set[str]],
  6. gold: List[Set[str]],
  7. method: Callable[[object, object], float],
  8. ) -> List[float]:
  9. """
  10. Takes gold and predicted answer sets and first finds the optimal 1-1 alignment
  11. between them and gets maximum metric values over all the answers.
  12. """
  13. scores = np.zeros([len(gold), len(predicted)])
  14. for gold_index, gold_item in enumerate(gold):
  15. for pred_index, pred_item in enumerate(predicted):
  16. scores[gold_index, pred_index] = method(pred_item, gold_item)
  17. row_ind, col_ind = linear_sum_assignment(-scores)
  18. max_scores = np.zeros([max(len(gold), len(predicted))])
  19. for row, column in zip(row_ind, col_ind):
  20. max_scores[row] = max(max_scores[row], scores[row, column])
  21. return max_scores