|
| 1 | +import unittest |
| 2 | + |
| 3 | +from signwriting_evaluation.metrics.similarity import SignWritingSimilarityMetric |
| 4 | +from signwriting_evaluation.metrics.bleu import SignWritingBLEU |
| 5 | +from signwriting_evaluation.metrics.chrf import SignWritingCHRF |
| 6 | + |
| 7 | + |
| 8 | +class TestMetricsReturnFloat(unittest.TestCase): |
| 9 | + """Test that all metrics return float values, never integers.""" |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.similarity = SignWritingSimilarityMetric() |
| 13 | + self.bleu = SignWritingBLEU() |
| 14 | + self.chrf = SignWritingCHRF() |
| 15 | + |
| 16 | + def test_similarity_returns_float(self): |
| 17 | + """Test that similarity metric always returns float.""" |
| 18 | + # Test with None values (edge case that was returning int) |
| 19 | + score = self.similarity.score(None, None) |
| 20 | + self.assertIsInstance(score, float, "Similarity score with None inputs should be float") |
| 21 | + |
| 22 | + # Test with valid FSW strings |
| 23 | + hypothesis = "M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517" |
| 24 | + reference = "M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513" |
| 25 | + score = self.similarity.score(hypothesis, reference) |
| 26 | + self.assertIsInstance(score, float, "Similarity score should be float") |
| 27 | + |
| 28 | + # Test with None hypothesis |
| 29 | + score = self.similarity.score(None, reference) |
| 30 | + self.assertIsInstance(score, float, "Similarity score with None hypothesis should be float") |
| 31 | + |
| 32 | + # Test with None reference |
| 33 | + score = self.similarity.score(hypothesis, None) |
| 34 | + self.assertIsInstance(score, float, "Similarity score with None reference should be float") |
| 35 | + |
| 36 | + def test_bleu_returns_float(self): |
| 37 | + """Test that BLEU metric always returns float.""" |
| 38 | + hypothesis = "M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517" |
| 39 | + reference = "M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513" |
| 40 | + score = self.bleu.score(hypothesis, reference) |
| 41 | + self.assertIsInstance(score, float, "BLEU score should be float") |
| 42 | + |
| 43 | + def test_chrf_returns_float(self): |
| 44 | + """Test that CHRF metric always returns float.""" |
| 45 | + hypothesis = "M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517" |
| 46 | + reference = "M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513" |
| 47 | + score = self.chrf.score(hypothesis, reference) |
| 48 | + self.assertIsInstance(score, float, "CHRF score should be float") |
| 49 | + |
| 50 | + def test_similarity_score_all_returns_float(self): |
| 51 | + """Test that score_all returns list of lists of floats.""" |
| 52 | + hypotheses = ["M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"] |
| 53 | + references = ["M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"] |
| 54 | + scores = self.similarity.score_all(hypotheses, references, progress_bar=False) |
| 55 | + for row in scores: |
| 56 | + for score in row: |
| 57 | + self.assertIsInstance(score, float, "Each score in score_all should be float") |
| 58 | + |
| 59 | + def test_similarity_corpus_score_returns_float(self): |
| 60 | + """Test that corpus_score returns float.""" |
| 61 | + hypotheses = ["M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"] |
| 62 | + references = [["M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"]] |
| 63 | + score = self.similarity.corpus_score(hypotheses, references) |
| 64 | + self.assertIsInstance(score, float, "Corpus score should be float") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + unittest.main() |
0 commit comments