File size: 3,916 Bytes
cb7868a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Unit tests for pali_utils — Thai/Pali text utilities.

Tests cover:
- Thai digit conversion
- Pali confusion map variant generation
- PyThaiNLP autocorrect (with fallback when library is absent)
- Pali correction dictionary lookup
- Full correction dict generation
"""

import pytest
from app.services.pali_utils import (
    to_thai_digits,
    THAI_PALI_CONFUSIONS,
    _generate_pali_variants,
    PALI_CORRECTIONS,
    PALI_CORRECTIONS_FULL,
    POPULAR_TERMS,
    HAS_PYTHAINLP,
    _similar_enough,
)


class TestToThaiDigits:
    def test_converts_arabic_to_thai(self):
        assert to_thai_digits("123") == "๑๒๓"

    def test_handles_empty(self):
        assert to_thai_digits("") == ""

    def test_handles_mixed(self):
        assert to_thai_digits("มรรค8") == "มรรค๘"

    def test_leaves_thai_digits_unchanged(self):
        assert to_thai_digits("๑๒๓") == "๑๒๓"


class TestPaliConfusions:
    def test_confusion_map_has_expected_keys(self):
        assert "ส" in THAI_PALI_CONFUSIONS
        assert "ณ" in THAI_PALI_CONFUSIONS
        assert "ภ" in THAI_PALI_CONFUSIONS

    def test_generate_variants(self):
        variants = _generate_pali_variants("สติ")
        assert "ศติ" in variants or "ษติ" in variants

    def test_generate_variants_no_confusion(self):
        """Words without confusion characters should produce no variants."""
        variants = _generate_pali_variants("ธรรม")
        assert len(variants) == 0 or all(v != "ธรรม" for v in variants)


class TestPaliCorrections:
    def test_basic_correction(self):
        assert PALI_CORRECTIONS["อริยะสัจ"] == "อริยสัจ"

    def test_niphan_variants(self):
        assert PALI_CORRECTIONS["นิพพาณ"] == "นิพพาน"
        assert PALI_CORRECTIONS["นิพาน"] == "นิพพาน"

    def test_full_dict_includes_hand_curated(self):
        assert PALI_CORRECTIONS_FULL["อริยะสัจ"] == "อริยสัจ"
        assert PALI_CORRECTIONS_FULL["นิพพาณ"] == "นิพพาน"

    def test_full_dict_includes_auto_generated(self):
        """POPULAR_TERMS should have auto-generated variants via confusion map."""
        if "อริยสัจ" in POPULAR_TERMS:
            has_variant = any(
                k != "อริยสัจ" and PALI_CORRECTIONS_FULL.get(k) == "อริยสัจ"
                for k in PALI_CORRECTIONS_FULL
            )
            assert has_variant, "Expected auto-generated variants for อริยสัจ"

    def test_to_thai_digits_in_corrections(self):
        """Ensure numbered terms have both digit formats."""
        assert "ขัน5" in PALI_CORRECTIONS
        assert PALI_CORRECTIONS["ขัน5"] == "ขันธ์ ๕"


class TestSimilarEnough:
    def test_identical(self):
        assert _similar_enough("สติปัฏฐาน", "สติปัฏฐาน") is True

    def test_similar_passes(self):
        # Minor variation should pass
        assert _similar_enough("สติปัฏฐาน", "สติปัฎฐาน") is True

    def test_vastly_different_fails(self):
        # Very different should fail
        assert _similar_enough("ปุพเพนิวาสานุสสติญาณ", "ปพเนวสสตญ") is False

    def test_empty(self):
        assert _similar_enough("", "") is True


class TestPopularTerms:
    def test_popular_terms_is_list_of_strings(self):
        assert isinstance(POPULAR_TERMS, list)
        assert len(POPULAR_TERMS) > 0
        assert all(isinstance(t, str) for t in POPULAR_TERMS)

    def test_includes_core_doctrines(self):
        core = {"อริยสัจ", "นิพพาน", "ไตรลักษณ์", "สติปัฏฐาน"}
        assert core.issubset(set(POPULAR_TERMS))