def test_get_text_length(self): markov = MarkovChain(order=4) markov.parse("a b c d e f g") output = markov.get_text(length=200) self.assertLessEqual(len(output), 200)
def test_internal_parsed_flag_set(self): # Calling parse method should set _parsed to True markov = MarkovChain(order=1) self.assertEqual(markov._parsed, False) markov.parse("It is a truth universally acknowledged, " "that a single man in possession of a good " "fortune must be in want of a wife.") self.assertEqual(markov._parsed, True)
def test_single_input_order_one(self): input_text = ("According to all known laws of aviation," "there is no way a bee should be able to fly.") markov = MarkovChain(order=1) markov.parse(input_text) self.assertEqual(markov._start_words, ["According"])
def test_start_words_used(self): # Check that we actually use the start words markov = MarkovChain(order=1) markov.parse("a b c b c b c b c b") output = markov.get_text(1000) if not output.startswith("a"): self.fail("Output did not start with a!")
def test_run_on_bug(self): # There was a bug causing the ends of sentences # to get mauled markov = MarkovChain(order=2) markov.parse("a b a b a b a b a b") output = markov.get_text(1000) self.assertNotIn("ab", output)
def test_single_input_order_four(self): input_text = ("According to all known laws of aviation," "there is no way a bee should be able to fly.") markov = MarkovChain(order=4) markov.parse(input_text) self.assertEqual(markov._start_words, ["According to all known"]) self.assertIn(markov._start_words[0], markov._chain.keys())
def test_parse_valid_text(self): # Parse method should set .chain correctly markov = MarkovChain(order=1) self.assertEqual(markov._chain, {}) input_text = "a b c" markov.parse(input_text) self.assertEqual(set(markov._chain.keys()), set(['a', 'b'])) self.assertEqual(markov._chain['a'], [Word('b', 1)]) self.assertEqual(markov._chain['b'], [Word('c', 1)])
def test_parse_newlinest(self): # Newlines must be treated like any other character markov = MarkovChain(order=1) self.assertEqual(markov._chain, {}) input_text = "a \nb \nc\nAAA end" markov.parse(input_text) self.assertEqual(set(markov._chain.keys()), set(['a', '\nb', '\nc\nAAA'])) self.assertEqual(markov._chain['a'], [Word('\nb', 1)]) self.assertEqual(markov._chain['\nb'], [Word('\nc\nAAA', 1)])
def test_at_sign_not_parsed(self): # In order to avoid tooting random people # the @ character must never enter the chain # and should be ignored markov = MarkovChain(order=1) self.assertEqual(markov._chain, {}) input_text = "a b @d c" markov.parse(input_text) self.assertEqual(set(markov._chain.keys()), set(['a', 'b'])) self.assertEqual(markov._chain['a'], [Word('b', 1)]) self.assertEqual(markov._chain['b'], [Word('c', 1)])
def test_order_four_input(self): input_text = ("According to all known laws of aviation," "there is no way a bee should be able to fly.") markov = MarkovChain(order=4) markov.parse(input_text) self.assertIn("According to all known", markov._chain.keys()) self.assertEqual(markov._chain["According to all known"], [Word("laws", 1)]) self.assertIn("is no way a", markov._chain.keys()) self.assertEqual(markov._chain["is no way a"], [Word("bee", 1)]) self.assertIn("should be able to", markov._chain.keys()) self.assertEqual(markov._chain["should be able to"], [Word("fly.", 1)])
def test_multiple_inputs(self): markov = MarkovChain(order=1) markov.parse("Hello there!") markov.parse("Tired: Single line content\n" "Wired: Multiple lines of content") markov.parse("Yet more content") self.assertEqual(markov._start_words, ["Hello", "Tired:", "Yet"])
def test_multiple_parse_calls(self): # Calling parse more than one should # append to the existing chain # but each call should be seperate markov = MarkovChain(order=1) self.assertEqual(markov._chain, {}) input_text_one = "a b c" input_text_two = "d e f" # Note that c does NOT go to d markov.parse(input_text_one) markov.parse(input_text_two) self.assertEqual(set(markov._chain.keys()), set(['a', 'b', 'd', 'e'])) self.assertEqual(markov._chain['a'], [Word('b', 1)]) self.assertEqual(markov._chain['b'], [Word('c', 1)]) self.assertEqual(markov._chain['d'], [Word('e', 1)]) self.assertEqual(markov._chain['e'], [Word('f', 1)])
def test_at_filtered_out(self): markov = MarkovChain(order=1) markov.parse("@a_person Hello there!") self.assertEqual(markov._start_words, ["Hello"])