Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def test_single_option(self):

        markov = MarkovChain()
        markov._chain = {'START': [Word('A', 1)]}

        for _ in range(1000):
            letter = markov._get_random_word('START')
            self.assertEqual(letter, 'A')
Ejemplo n.º 3
0
    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"])
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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!")
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    def test_get_text_null_input(self):
        # If we have nothing in _chain then we
        # want to return an empty string

        markov = MarkovChain(order=4)
        self.assertEqual(markov._chain, {})
        self.assertEqual(markov._parsed, False)

        output = markov.get_text()
        self.assertEqual(output, "")
Ejemplo n.º 8
0
    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())
Ejemplo n.º 9
0
    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)])
Ejemplo n.º 10
0
    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)])
Ejemplo n.º 11
0
    def test_null_parsing(self):
        # We should have an empty list for start_words
        # if we have not parsed owt

        markov = MarkovChain(order=2)

        self.assertEqual(markov._start_words, [])
Ejemplo n.º 12
0
    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)])
Ejemplo n.º 13
0
    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"])
Ejemplo n.º 14
0
    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)])
Ejemplo n.º 15
0
    def test_10_1_odds(self):

        markov = MarkovChain()
        markov._chain = {'START': [Word('A', 10), Word('B', 1)]}

        a_count = 0
        b_count = 0
        for _ in range(10000):
            letter = markov._get_random_word('START')

            if letter == 'A':
                a_count += 1
            elif letter == 'B':
                b_count += 1
            else:
                self.fail("Expected 'A' or 'B' got {0}".format(letter))

        self.assertEqual(a_count + b_count, 10000)
        self.assertGreaterEqual(a_count, 9000)
Ejemplo n.º 16
0
    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)])
Ejemplo n.º 17
0
 def test_order_zero_raises_exception(self):
     with self.assertRaises(ValueError):
         MarkovChain(order=0)
Ejemplo n.º 18
0
    def test_at_filtered_out(self):
        markov = MarkovChain(order=1)

        markov.parse("@a_person Hello there!")

        self.assertEqual(markov._start_words, ["Hello"])
Ejemplo n.º 19
0
 def test_order_negative_raises_exception(self):
     with self.assertRaises(ValueError):
         MarkovChain(order=-1)
Ejemplo n.º 20
0
 def test_order_string_raises_exception(self):
     with self.assertRaises(ValueError):
         MarkovChain(order="four")