|
import unittest |
|
from cer import CER |
|
|
|
cer = CER() |
|
class TestCER(unittest.TestCase): |
|
def test_cer_case_sensitive(self): |
|
refs = ["Magyar Országgyűlés"] |
|
preds = ["Magyar Országgyűlés"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.1818181818) < 1e-6) |
|
|
|
def test_cer_whitespace(self): |
|
refs = ["Farkasok voltak"] |
|
preds = ["Farkasokvoltak"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.) < 1e-6) |
|
|
|
refs = ["Farkasokvoltak"] |
|
preds = ["Ferkasok voltak"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.) < 1e-6) |
|
|
|
|
|
refs = ["Farkasok voltak"] |
|
preds = ["Farkasok voltak"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.0) < 1e-6) |
|
|
|
|
|
refs = ["Farkasok voltak"] |
|
preds = ["Farkasok voltak"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.0) < 1e-6) |
|
|
|
def test_cer_sub(self): |
|
refs = ["Magyar"] |
|
preds = ["Megyar"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.125) < 1e-6) |
|
|
|
def test_cer_del(self): |
|
refs = ["Farkasokvoltak"] |
|
preds = ["Farkasokavoltak"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.) < 1e-6) |
|
|
|
def test_cer_insert(self): |
|
refs = ["Farkasokvoltak"] |
|
preds = ["Farkasokoltak"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.) < 1e-6) |
|
|
|
def test_cer_equal(self): |
|
refs = ["Magyar"] |
|
char_error_rate = cer.compute(predictions=refs, references=refs) |
|
self.assertEqual(char_error_rate, 0.0) |
|
|
|
def test_cer_list_of_seqs(self): |
|
|
|
refs = ["Eötvös Loránd Tudományegyetem", "szeretem a lányom"] |
|
char_error_rate = cer.compute(predictions=refs, references=refs) |
|
self.assertEqual(char_error_rate, 0.0) |
|
|
|
refs = ["diák", "Az arab nyelvet könnyű megtanulni!", "autó"] |
|
preds = ["dxák", "Az arab nyelvet könnyű megtanulni!", "autó"] |
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs) |
|
self.assertTrue(abs(char_error_rate - 0.0238095238) < 1e-6) |
|
|
|
def test_correlated_sentences(self): |
|
|
|
|
|
refs = ["Tanuljon mesterséges intelligenciát,", " hogy biztosítsa jövőjét"] |
|
preds = ["Tanuljon mesterséges intelligenciát, hogy", " biztosítsa jövőjét"] |
|
|
|
|
|
|
|
char_error_rate = cer.compute(predictions=preds, references=refs, concatenate_texts=True) |
|
self.assertTrue(abs(char_error_rate - 0.03333333333) < 1e-6) |
|
|
|
def test_cer_empty(self): |
|
refs = [""] |
|
preds = ["tök mindegy"] |
|
with self.assertRaises(ValueError): |
|
cer.compute(predictions=preds, references=refs) |
|
|
|
if __name__ == "__main__": |
|
unittest.main() |