File: __init__.py

package info (click to toggle)
simplebayes 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 496 kB
  • sloc: python: 3,322; makefile: 165; sh: 24
file content (279 lines) | stat: -rw-r--r-- 9,820 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# pylint: disable=invalid-name,missing-docstring
import unittest
from unittest.mock import patch, MagicMock

from simplebayes import SimpleBayes
from simplebayes.categories import BayesCategories

from simplebayes.errors import InvalidCategoryError


class SimpleBayesTests(unittest.TestCase):

    def test_tokenizer(self):
        sb = SimpleBayes()
        result = sb.tokenizer('hello world')
        self.assertEqual(result, ['hello', 'world'])
        self.assertEqual(SimpleBayes.tokenize_text('hello world'), ['hello', 'world'])

    def test_count_token_occurrences(self):
        sb = SimpleBayes()
        result = sb.count_token_occurrences(['hello', 'world', 'hello'])
        self.assertEqual(
            result,
            {
                'hello': 2,
                'world': 1
            }
        )

    def test_flush_and_tally(self):
        sb = SimpleBayes()
        sb.train('foo', 'hello world hello')
        self.assertEqual(sb.tally('foo'), 3)
        sb.flush()
        self.assertEqual(sb.tally('foo'), 0)

    def test_untrain(self):
        sb = SimpleBayes()
        sb.train('foo', 'hello world hello')
        self.assertEqual(sb.tally('foo'), 3)
        self.assertEqual(sb.tally('bar'), 0)
        sb.untrain('bar', 'for bar baz')
        self.assertEqual(sb.tally('foo'), 3)
        self.assertEqual(sb.tally('bar'), 0)
        sb.untrain('foo', 'hello world')
        self.assertEqual(sb.tally('foo'), 1)

    @patch.object(BayesCategories, 'get_category')
    def test_train_with_existing_category(self, get_category_mock):
        cat_mock = MagicMock()
        cat_mock.train_token.return_value = None
        get_category_mock.return_value = cat_mock

        sb = SimpleBayes()
        sb.train('foo', 'hello world hello')

        get_category_mock.assert_called_once_with('foo')
        cat_mock.train_token.assert_any_call('hello', 2)
        cat_mock.train_token.assert_any_call('world', 1)

    @patch.object(BayesCategories, 'get_category')
    @patch.object(BayesCategories, 'add_category')
    def test_train_with_new_category(
            self,
            add_category_mock,
            get_category_mock
    ):
        cat_mock = MagicMock()
        cat_mock.train_token.return_value = None
        get_category_mock.side_effect = KeyError()
        add_category_mock.return_value = cat_mock

        sb = SimpleBayes()
        sb.train('foo', 'hello world hello')

        add_category_mock.assert_called_with('foo')
        cat_mock.train_token.assert_any_call('hello', 2)
        cat_mock.train_token.assert_any_call('world', 1)

    @patch.object(BayesCategories, 'get_categories')
    def test_classify(self, get_categories_mock):
        cat1_mock = MagicMock()
        cat1_mock.get_token_count.return_value = 2
        cat1_mock.get_tally.return_value = 8
        cat2_mock = MagicMock()
        cat2_mock.get_token_count.return_value = 4
        cat2_mock.get_tally.return_value = 32

        get_categories_mock.return_value = {
            'foo': cat1_mock,
            'bar': cat2_mock
        }

        sb = SimpleBayes()
        sb.calculate_category_probability()
        result = sb.classify('hello world')

        self.assertEqual('bar', result)
        assert 3 == get_categories_mock.call_count, \
            get_categories_mock.call_count
        cat1_mock.get_token_count.assert_any_call('hello')
        cat1_mock.get_token_count.assert_any_call('world')
        cat1_mock.get_tally.assert_called_once_with()
        cat2_mock.get_token_count.assert_any_call('hello')
        cat2_mock.get_token_count.assert_any_call('world')
        cat2_mock.get_tally.assert_called_once_with()

    @patch.object(BayesCategories, 'get_categories')
    def test_classify_without_categories(self, get_categories_mock):
        get_categories_mock.return_value = {}

        sb = SimpleBayes()
        result = sb.classify('hello world')

        self.assertIsNone(result)
        assert 2 == get_categories_mock.call_count, \
            get_categories_mock.call_count

    @patch.object(BayesCategories, 'get_categories')
    def test_classify_with_empty_category(self, get_categories_mock):
        cat_mock = MagicMock()
        cat_mock.get_tally.return_value = 0
        cat_mock.get_token_count.return_value = 0

        get_categories_mock.return_value = {
            'foo': cat_mock
        }

        sb = SimpleBayes()
        sb.calculate_category_probability()
        result = sb.classify('hello world')

        self.assertIsNone(result)
        assert 3 == get_categories_mock.call_count, \
            get_categories_mock.call_count
        cat_mock.get_tally.assert_called_once_with()

    def test_score_without_categories(self):
        sb = SimpleBayes()
        self.assertEqual(sb.score('hello world'), {})

    def test_score_with_no_matching_tokens(self):
        sb = SimpleBayes()
        sb.train('alpha', 'one two three')
        self.assertEqual(sb.score('unknown tokens here'), {})

    @patch.object(BayesCategories, 'get_categories')
    def test_score(self, get_categories_mock):
        cat1_mock = MagicMock()
        cat1_mock.get_token_count.return_value = 2
        cat1_mock.get_tally.return_value = 8
        cat2_mock = MagicMock()
        cat2_mock.get_token_count.return_value = 4
        cat2_mock.get_tally.return_value = 32

        get_categories_mock.return_value = {
            'foo': cat1_mock,
            'bar': cat2_mock
        }

        sb = SimpleBayes()
        sb.calculate_category_probability()
        result = sb.score('hello world')

        self.assertIn('foo', result)
        self.assertIn('bar', result)
        self.assertAlmostEqual(result['foo'], 0.22222222222222224)
        self.assertAlmostEqual(result['bar'], 1.777777777777778)

        assert 3 == get_categories_mock.call_count, \
            get_categories_mock.call_count
        cat1_mock.get_token_count.assert_any_call('hello')
        cat1_mock.get_token_count.assert_any_call('world')
        cat1_mock.get_tally.assert_called_once_with()
        cat2_mock.get_token_count.assert_any_call('hello')
        cat2_mock.get_token_count.assert_any_call('world')
        cat2_mock.get_tally.assert_called_once_with()

    @patch.object(BayesCategories, 'get_categories')
    def test_score_with_zero_bayes_denon(self, get_categories_mock):
        cat1_mock = MagicMock()
        cat1_mock.get_token_count.return_value = 2
        cat1_mock.get_tally.return_value = 8
        cat2_mock = MagicMock()
        cat2_mock.get_token_count.return_value = 4
        cat2_mock.get_tally.return_value = 32

        get_categories_mock.return_value = {
            'foo': cat1_mock,
            'bar': cat2_mock
        }

        sb = SimpleBayes()
        sb.calculate_category_probability()
        sb.probabilities['foo']['prc'] = 0
        sb.probabilities['foo']['prnc'] = 0
        result = sb.score('hello world')

        self.assertEqual(
            {
                'bar': 1.777777777777778
            },
            result
        )

        assert 3 == get_categories_mock.call_count, \
            get_categories_mock.call_count
        cat1_mock.get_token_count.assert_any_call('hello')
        cat1_mock.get_token_count.assert_any_call('world')
        cat1_mock.get_tally.assert_called_once_with()
        cat2_mock.get_token_count.assert_any_call('hello')
        cat2_mock.get_token_count.assert_any_call('world')
        cat2_mock.get_tally.assert_called_once_with()

    def test_classify_result(self):
        sb = SimpleBayes()
        sb.train('good', 'bright happy joy')
        sb.train('bad', 'sad dark doom')

        result = sb.classify_result('bright joy')

        self.assertEqual(result.category, 'good')
        self.assertGreater(result.score, 0)

    def test_classify_result_empty(self):
        sb = SimpleBayes()

        result = sb.classify_result('anything')

        self.assertIsNone(result.category)
        self.assertEqual(result.score, 0.0)

    def test_get_summaries(self):
        sb = SimpleBayes()
        sb.train('alpha', 'one two three')

        summaries = sb.get_summaries()

        self.assertIn('alpha', summaries)
        self.assertEqual(summaries['alpha'].token_tally, 3)
        self.assertGreaterEqual(summaries['alpha'].prob_in_cat, 0.0)
        self.assertGreaterEqual(summaries['alpha'].prob_not_in_cat, 0.0)

    def test_train_invalid_category_raises(self):
        sb = SimpleBayes()
        with self.assertRaises(InvalidCategoryError):
            sb.train('bad category', 'text')
        with self.assertRaises(InvalidCategoryError):
            sb.train(None, 'text')  # type: ignore[arg-type]

    def test_untrain_removes_empty_category(self):
        sb = SimpleBayes()
        sb.train('alpha', 'one two three')
        sb.untrain('alpha', 'one two three')
        self.assertNotIn('alpha', sb.categories.get_categories())
        self.assertNotIn('alpha', sb.probabilities)
        self.assertNotIn('alpha', sb.get_summaries())

    def test_classify_tie_breaks_lexically(self):
        sb = SimpleBayes()
        sb.train('zeta', 'match token')
        sb.train('alpha', 'match token')

        result = sb.classify('match token')

        self.assertEqual(result, 'alpha')

    def test_laplace_smoothing_alpha(self):
        sb = SimpleBayes(alpha=0.01)
        sb.train('spam', 'buy now click here')
        sb.train('ham', 'meeting tomorrow schedule')
        result = sb.classify_result('click offer')
        self.assertIsNotNone(result.category)
        self.assertGreater(result.score, 0)

    def test_language_and_remove_stop_words_params(self):
        sb = SimpleBayes(language="english", remove_stop_words=False)
        sb.train("foo", "the cat is in the hat")
        self.assertGreater(sb.tally("foo"), 2)  # stop words counted