-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_markov.py
49 lines (37 loc) · 1.54 KB
/
test_markov.py
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
"""Test class for the Markov Chain."""
import unittest
from markov import Markov
class TestMarkovMethods(unittest.TestCase):
"""Test the Markov-Chain."""
def setUp(self):
"""Set up a Markov Chain instance for testing."""
self.m = Markov()
def test_new_word_entry(self):
"""Make sure that a new word and its next word are properly entered."""
self.m.add_next_word('hello', 'world')
self.assertEqual(self.m.chain, {'hello': ['world']})
def test_additional_next_word_entry(self):
"""Test insertion of a new next word for an existing current word."""
self.m.add_next_word('hello', 'world')
self.m.add_next_word('hello', 'you')
self.assertEqual(self.m.chain, {'hello': ['world', 'you']})
def test_paragraph_entry(self):
"""
Test insertion of a paragraph into the chains.
Verify the logic concerning the period.
"""
self.m.add_text(['hello', 'world', '.', 'goodnight', 'moon', '.'])
self.assertEqual(self.m.chain, {
'.': ['hello', 'goodnight'],
'hello': ['world'],
'world': ['.'],
'goodnight': ['moon'],
'moon': ['.']})
def test_sentence_generation(self):
"""Test traversing through Markov-Chain dict to create sentence."""
self.m.add_text(['hello', 'world', '.'])
self.m.add_text(['hello', 'you', '.'])
self.assertIn(
self.m.generate_sentence(4), ['hello world.', 'hello you.'])
if __name__ == '__main__':
unittest.main()