def test_add_node(self): chain = MarkovChain() chain.add_node() chain.add_node() self.assertEqual(len(chain.nodes), 2) self.assertEqual(chain.nodes[0].index, 0) self.assertEqual(chain.nodes[1].index, 1)
def test_add_edges(self): chain = MarkovChain(3) chain.add_edges(((0, 1), (1, 2, 0.75), (1, 0, 0.25))) self.assertEqual(len(chain.nodes), 3) self.assertEqual(len(chain.edges), 3) self.assertEqual(len(chain.nodes[1].edges_out), 2) self.assertEqual(chain.nodes[1].edges_out[1].probability, 0.25)
def test_first_order_words_simple(self): mc = MarkovChain() mc.order = 1 mc.add_string('The quick brown fox jumps over the lazy fox.') actual_chain = {('the',): ['quick', 'lazy'], ('quick',): ['brown'], ('brown',): ['fox'], ('fox',): ['jumps'], ('jumps',): ['over'], ('over',): ['the'], ('lazy',): ['fox']} self.assertTrue(equal_dicts(mc.markov_chain, actual_chain))
def test_first_order_words_complex(self): mc = MarkovChain() mc.order = 1 mc.add_string('I felt happy because I saw the others were happy and because I knew I should feel happy, but I ' 'was not really happy.') actual_chain = {('i',): ['felt', 'saw', 'knew', 'should', 'was'], ('felt',): ['happy'], ('happy',): ['because', 'and', 'but'], ('because',): ['i', 'i'], ('saw',): ['the'], ('the',): ['others'], ('others',): ['were'], ('were',): ['happy'], ('and',): ['because'], ('knew',): ['i'], ('should',): ['feel'], ('feel',): ['happy'], ('but',): ['i'], ('was',): ['not'], ('not',): ['really'], ('really',): ['happy']} self.assertTrue(equal_dicts(mc.markov_chain, actual_chain))
def test_first_order_chars_simple(self): mc = MarkovChain() mc.order = 1 mc.set_production_state_to_chars() mc.add_chars('pneumonoultramicroscopicsilicovolcanoconiosis') actual_chain = {('p',): ['n', 'i'], ('n',): ['e', 'o', 'o', 'i'], ('e',): ['u'], ('u',): ['m', 'l'], ('m',): ['o', 'i'], ('o',): ['n', 'u', 's', 'p', 'v', 'l', 'c', 'n', 's'], ('l',): ['t', 'i', 'c'], ('t',): ['r'], ('r',): ['a', 'o'], ('a',): ['m', 'n'], ('i',): ['c', 'c', 'l', 'c', 'o', 's'], ('c',): ['r', 'o', 's', 'o', 'a', 'o'], ('s',): ['c', 'i', 'i'], ('v',): ['o']} self.assertTrue(equal_dicts(actual_chain, mc.markov_chain))
def test_all_nodes_in(self): chain = MarkovChain(((1, 0), (2, 0), (3, 0))) self.assertEqual(chain.is_absorbing(), True)
def test_all_nodes_out(self): chain = MarkovChain(((0, 1), (0, 2), (0, 3))) self.assertEqual(chain.is_absorbing(), True)
def test_circular_chain_with_exit(self): chain = MarkovChain(((0, 1), (1, 2), (2, 3), (3, 0), (0, 4))) self.assertEqual(chain.is_absorbing(), True)
def test_circular_chain(self): chain = MarkovChain(((0, 1), (1, 2), (2, 3), (3, 0))) self.assertEqual(chain.is_absorbing(), False)
def test_all_nodes_in(self): chain = MarkovChain(((1, 0), (2, 0), (3, 0))) self.assertEqual(chain.is_connected(), True)
def test_all_nodes_out(self): chain = MarkovChain(((0, 1), (0, 2), (0, 3))) self.assertEqual(chain.is_connected(), True)
def test_two_connected_circles(self): chain = MarkovChain( ((0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 4), (4, 0))) self.assertEqual(chain.is_connected(), True)
def test_create_empty_chain(self): chain = MarkovChain() self.assertEqual(len(chain.nodes), 0) self.assertEqual(len(chain.edges), 0)
def test_add_nodes(self): chain = MarkovChain() chain.add_nodes(5) self.assertEqual(len(chain.nodes), 5) self.assertEqual(len(chain.edges), 0)
def test_create_chain_with_nodes(self): chain = MarkovChain(5) self.assertEqual(len(chain.nodes), 5) self.assertEqual(len(chain.edges), 0)