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
|
#
# Copyright (c) 2015 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from twisted.trial import unittest
from pixelated.support.markov import MarkovGenerator
import random
SAMPLE_TEXT = 'One two three four'
class MarkovGeneratorTest(unittest.TestCase):
def setUp(self):
self.random = random.Random(0)
def test_starts_with_capital_case_workd(self):
gen = MarkovGenerator(['lower Upper smaller Capital'], random=self.random)
result = gen.generate(1)
self.assertTrue(result.startswith('Upper'))
def test_aborts_if_no_upper_letter_word_found(self):
gen = MarkovGenerator(['all lower case'], random=self.random)
self.assertRaises(ValueError, gen.generate, 1)
def test_generate(self):
gen = MarkovGenerator([SAMPLE_TEXT], random=self.random)
result = gen.generate(3)
self.assertEqual('One two three', result)
def test_minimum_three_words(self):
self.assertRaises(ValueError, MarkovGenerator([]).generate, 1)
self.assertRaises(ValueError, MarkovGenerator, ['1'])
self.assertRaises(ValueError, MarkovGenerator, ['1', '2'])
self.assertRaises(ValueError, MarkovGenerator, ['1', '2', '3'])
def test_add_paragraph_on_empty_chain(self):
gen = MarkovGenerator([SAMPLE_TEXT], random=self.random, add_paragraph_on_empty_chain=True)
result = gen.generate(5)
self.assertEqual('One two three four \n\n One', result)
def test_multiple_inputs(self):
gen = MarkovGenerator([SAMPLE_TEXT, 'Five Six seven eight'], random=self.random)
result = gen.generate(3)
self.assertEqual('Five Six seven', result)
def test_add(self):
gen = MarkovGenerator([], random=self.random)
gen.add(SAMPLE_TEXT)
result = gen.generate(3)
self.assertEqual('One two three', result)
def test_multiple_word_occurences(self):
gen = MarkovGenerator(['One Two One Three One Two One Four'], random=self.random)
result = gen.generate(2)
self.assertEqual('Two One', result)
|