|
| 1 | +from sacrerouge.common.testing.metric_test_cases import ReferenceBasedMetricTestCase |
| 2 | +from sacrerouge.common.testing.util import sacrerouge_command_exists |
| 3 | +from sacrerouge.metrics.chrf import ChrF |
| 4 | + |
| 5 | + |
| 6 | +class TestChrF(ReferenceBasedMetricTestCase): |
| 7 | + def test_chrf(self): |
| 8 | + # This is a regression test, not necessarily a test for correctness |
| 9 | + metric = ChrF() |
| 10 | + expected_output = [ |
| 11 | + {'chrf': 43.85388187268078}, |
| 12 | + {'chrf': 52.60988684061151}, |
| 13 | + {'chrf': 49.65855270200033}, |
| 14 | + {'chrf': 41.43422564802467}, |
| 15 | + {'chrf': 42.53477752245672}, |
| 16 | + {'chrf': 41.91433502501302}, |
| 17 | + {'chrf': 49.91729287293663}, |
| 18 | + {'chrf': 49.63089847829706}, |
| 19 | + {'chrf': 43.484464138558174}, |
| 20 | + {'chrf': 56.073961048238665}, |
| 21 | + {'chrf': 48.94445553665466}, |
| 22 | + {'chrf': 46.85719853900437} |
| 23 | + ] |
| 24 | + super().assert_expected_output(metric, expected_output) |
| 25 | + |
| 26 | + def test_chrf_examples(self): |
| 27 | + chrf = ChrF() |
| 28 | + |
| 29 | + hypothesis = 'The dog bit the man.' |
| 30 | + references = ['The dog bit the man.', 'The dog had bit the man.'] |
| 31 | + expected = 100.0 |
| 32 | + actual = chrf.score(hypothesis, references)['chrf'] |
| 33 | + self.assertAlmostEqual(expected, actual) |
| 34 | + |
| 35 | + hypothesis = 'It wasn\'t surprising.' |
| 36 | + references = ['It was not unexpected.', 'No one was surprised.'] |
| 37 | + expected = 35.34635301425291 |
| 38 | + actual = chrf.score(hypothesis, references)['chrf'] |
| 39 | + self.assertAlmostEqual(expected, actual) |
| 40 | + |
| 41 | + hypothesis = 'The man had just bitten him.' |
| 42 | + references = ['The man bit him first.', 'The man had bitten the dog.'] |
| 43 | + expected = 51.87740799504779 |
| 44 | + actual = chrf.score(hypothesis, references)['chrf'] |
| 45 | + self.assertAlmostEqual(expected, actual) |
| 46 | + |
| 47 | + def test_chrf_order_invariant(self): |
| 48 | + metric = ChrF() |
| 49 | + self.assert_order_invariant(metric) |
| 50 | + |
| 51 | + def test_command_exists(self): |
| 52 | + assert sacrerouge_command_exists(['chrf']) |
0 commit comments