| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | import unittest |
| 8 | |
| 9 | from src.generate.sentences import (citation_coverage, drop_uncited, is_bullet, |
| 10 | is_cited, split_sentences) |
| 11 | |
| 12 | |
| 13 | class SplitTests(unittest.TestCase): |
| 14 | def test_basic_three_sentences(self): |
| 15 | t = "DNA çift sarmaldır [1]. Ribozom protein üretir. Mitoz bölünmedir." |
| 16 | self.assertEqual(len(split_sentences(t)), 3) |
| 17 | |
| 18 | def test_citation_stays_with_its_sentence(self): |
| 19 | c = split_sentences("A olur [1]. B olur [2].") |
| 20 | self.assertTrue(all(is_cited(x) for x in c)) |
| 21 | |
| 22 | def test_abbreviations_do_not_split(self): |
| 23 | for t in ("Bitkiler vb. canlılar fotosentez yapar [1].", |
| 24 | "Örn. glikoz taşınır [1].", |
| 25 | "Bkz. s. 12 [1].", |
| 26 | "Değer yak. 10 gramdır [1].", |
| 27 | "Dr. Ahmet açıkladı [1]."): |
| 28 | with self.subTest(t): |
| 29 | self.assertEqual(len(split_sentences(t)), 1, t) |
| 30 | |
| 31 | def test_initialisms_do_not_split(self): |
| 32 | |
| 33 | |
| 34 | |
| 35 | for t in ("M.Ö. 300 yılında yaşadı [1].", |
| 36 | "T.C. Anayasası kabul edildi [1].", |
| 37 | "A.B.D. ve Rusya anlaştı [1]."): |
| 38 | with self.subTest(t): |
| 39 | self.assertEqual(len(split_sentences(t)), 1, t) |
| 40 | |
| 41 | def test_decimals_and_codes_do_not_split(self): |
| 42 | for t in ("Değeri 3.14 olarak alınır [1].", |
| 43 | "10.1.1.2 Mitozu açıklar [1].", |
| 44 | "Oran 0.5 ile 1.5 arasındadır [1]."): |
| 45 | with self.subTest(t): |
| 46 | self.assertEqual(len(split_sentences(t)), 1, t) |
| 47 | |
| 48 | def test_bullets_are_separate_claims(self): |
| 49 | |
| 50 | |
| 51 | t = "- Üreticiler ototroftur [1]\n- Tüketiciler heterotroftur\n- Ayrıştırıcılar [2]" |
| 52 | c = split_sentences(t) |
| 53 | self.assertEqual(len(c), 3) |
| 54 | self.assertTrue(is_bullet(c[0])) |
| 55 | self.assertFalse(is_cited(c[1])) |
| 56 | |
| 57 | def test_question_and_exclamation_split(self): |
| 58 | self.assertEqual(len(split_sentences("Bu nedir? Şudur [1]. Harika!")), 3) |
| 59 | |
| 60 | def test_empty_and_whitespace(self): |
| 61 | for t in ("", " ", "\n\n"): |
| 62 | self.assertEqual(split_sentences(t), []) |
| 63 | |
| 64 | def test_no_terminal_punctuation_is_still_a_sentence(self): |
| 65 | self.assertEqual(len(split_sentences("Atıfsız tek satır")), 1) |
| 66 | |
| 67 | def test_multiple_blank_lines_do_not_create_empty_sentences(self): |
| 68 | c = split_sentences("A [1].\n\n\nB [2].") |
| 69 | self.assertEqual(len(c), 2) |
| 70 | |
| 71 | |
| 72 | class CoverageTests(unittest.TestCase): |
| 73 | def test_audit_scenario_is_measured(self): |
| 74 | |
| 75 | |
| 76 | t = ("Mitokondri enerji üretir [1]. Bir glikozdan 47 ATP üretilir. " |
| 77 | "Ribozom nükleusta bulunur. İnsanda 48 kromozom vardır. " |
| 78 | "Bu yüzden hücre bölünür.") |
| 79 | k = citation_coverage(t) |
| 80 | self.assertEqual(k["n_sentences"], 5) |
| 81 | self.assertEqual(k["n_cited_sentences"], 1) |
| 82 | self.assertEqual(k["uncited_ratio"], 0.8) |
| 83 | self.assertEqual(len(k["uncited_sentences"]), 4) |
| 84 | |
| 85 | def test_fully_cited_answer_has_zero_ratio(self): |
| 86 | k = citation_coverage("A olur [1]. B olur [2].") |
| 87 | self.assertEqual(k["uncited_ratio"], 0.0) |
| 88 | |
| 89 | def test_empty_text_does_not_divide_by_zero(self): |
| 90 | self.assertEqual(citation_coverage("")["uncited_ratio"], 0.0) |
| 91 | |
| 92 | |
| 93 | class DropTests(unittest.TestCase): |
| 94 | def test_uncited_sentences_are_removed(self): |
| 95 | metin, atilan = drop_uncited("A olur [1]. B olur. C olur [2].") |
| 96 | self.assertIn("[1]", metin) |
| 97 | self.assertIn("[2]", metin) |
| 98 | self.assertNotIn("B olur", metin) |
| 99 | self.assertEqual(atilan, ["B olur."]) |
| 100 | |
| 101 | def test_line_structure_is_preserved(self): |
| 102 | metin, _ = drop_uncited("- A [1]\n- B\n- C [2]") |
| 103 | self.assertEqual(metin.count("\n"), 1) |
| 104 | |
| 105 | def test_everything_uncited_yields_empty(self): |
| 106 | metin, atilan = drop_uncited("A olur. B olur.") |
| 107 | self.assertEqual(metin, "") |
| 108 | self.assertEqual(len(atilan), 2) |
| 109 | |
| 110 | |
| 111 | if __name__ == "__main__": |
| 112 | unittest.main() |