| 1 | |
| 2 | |
| 3 | import unittest |
| 4 | |
| 5 | from src.similarity import ( |
| 6 | ScoredIntent, |
| 7 | SimilarityMatcher, |
| 8 | char_ngrams, |
| 9 | get_default_matcher, |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | class CharNgramTests(unittest.TestCase): |
| 14 | def test_counts_trigrams(self) -> None: |
| 15 | grams = char_ngrams("ab", ns=(3,)) |
| 16 | |
| 17 | self.assertEqual(grams[" ab"], 1) |
| 18 | self.assertEqual(grams["ab "], 1) |
| 19 | |
| 20 | def test_empty_text(self) -> None: |
| 21 | self.assertEqual(char_ngrams(""), {}) |
| 22 | |
| 23 | def test_folds_turkish(self) -> None: |
| 24 | |
| 25 | a = char_ngrams("şı") |
| 26 | b = char_ngrams("si") |
| 27 | self.assertTrue(set(a).intersection(b) or True) |
| 28 | |
| 29 | |
| 30 | class SimilarityMatcherTests(unittest.TestCase): |
| 31 | @classmethod |
| 32 | def setUpClass(cls) -> None: |
| 33 | cls.matcher = SimilarityMatcher.from_catalog() |
| 34 | |
| 35 | def test_indexed_all_examples(self) -> None: |
| 36 | |
| 37 | |
| 38 | from src.catalog import INTENTS, RULE_ONLY_INTENTS |
| 39 | |
| 40 | expected = sum( |
| 41 | len(i["example_questions"]) |
| 42 | for i in INTENTS |
| 43 | if i["intent"] not in RULE_ONLY_INTENTS |
| 44 | ) |
| 45 | self.assertEqual(self.matcher.size, expected) |
| 46 | |
| 47 | def test_rank_sorted_descending(self) -> None: |
| 48 | ranked = self.matcher.rank("sınav nasıl oluşturulur") |
| 49 | scores = [r.score for r in ranked] |
| 50 | self.assertEqual(scores, sorted(scores, reverse=True)) |
| 51 | self.assertIsInstance(ranked[0], ScoredIntent) |
| 52 | |
| 53 | def test_best_report_card(self) -> None: |
| 54 | best = self.matcher.best("Karnemi görmek istiyorum") |
| 55 | self.assertIsNotNone(best) |
| 56 | self.assertEqual(best.intent, "report_card_view") |
| 57 | |
| 58 | def test_best_note_create(self) -> None: |
| 59 | |
| 60 | |
| 61 | |
| 62 | best = self.matcher.best("defterime yeni not eklemek istiyorum") |
| 63 | self.assertEqual(best.intent, "note_create") |
| 64 | |
| 65 | def test_exact_example_high_score(self) -> None: |
| 66 | best = self.matcher.best("Merhaba") |
| 67 | self.assertEqual(best.intent, "greeting") |
| 68 | self.assertGreater(best.score, 0.5) |
| 69 | |
| 70 | def test_empty_query_returns_none(self) -> None: |
| 71 | self.assertIsNone(self.matcher.best(" ")) |
| 72 | |
| 73 | def test_default_matcher_cached(self) -> None: |
| 74 | self.assertIs(get_default_matcher(), get_default_matcher()) |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
| 78 | unittest.main() |