コード例 #1
0
ファイル: main.py プロジェクト: eeclaire/data-structures
def main():
    """Entry point for the code execution."""
    f = open('sample.txt', 'r')
    text = f.read()

    clean_text_list = prep_text(text)

    m = Markov()
    m.add_text(clean_text_list)

    text = generate_paragraph(m, 5, 10)
    print(text)

    f.close()
コード例 #2
0
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.'])