Skip to content

Commit

Permalink
fix(corpus_score): get max score from all references
Browse files Browse the repository at this point in the history
  • Loading branch information
AmitMY committed Jan 25, 2024
1 parent 84be9ce commit affd85b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion signwriting_evaluation/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ def __call__(self, hypothesis: str, reference: str) -> float:
def score(self, hypothesis: str, reference: str) -> float:
raise NotImplementedError

def score_max(self, hypothesis: str, references: list[str]) -> float:
all_scores = self.score_all([hypothesis], references)
return max(max(scores) for scores in all_scores)

def corpus_score(self, hypotheses: list[str], references: list[list[str]]) -> float:
# Default implementation: average over sentence scores
return sum(self.score(h, r) for h, r in zip(hypotheses, references)) / len(references)
return sum(self.score_max(h, r) for h, r in zip(hypotheses, references)) / len(references)

def score_all(self, hypotheses: list[str], references: list[str]) -> list[list[float]]:
# Default implementation: call the score function for each hypothesis-reference pair
Expand Down
8 changes: 8 additions & 0 deletions signwriting_evaluation/metrics/test_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def test_different_shape(self):
self.assertIsInstance(score, float)
self.assertAlmostEqual(score, 0.8326259781509948)

def test_corpus_score(self):
hypothesis = "M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517"
good_reference = "M530x538S17600508x462S12a11493x494S20e00488x510S22f13469x517"
bad_reference = "M530x538S17600508x462"
score = self.metric.corpus_score([hypothesis], [[good_reference, bad_reference]])
self.assertIsInstance(score, float)
self.assertAlmostEqual(score, 0.8326259781509948)


if __name__ == '__main__':
unittest.main()

0 comments on commit affd85b

Please sign in to comment.