def test_complex_input_2(): correct_map = markov_solution.build_chains("sample4.txt", 2) print "Expected map:", correct_map new_map = markov.process_file("sample4.txt") print "Your map:", new_map assert new_map == correct_map, """\
def test_paragraph(): assert fn_exists("build_paragraph"), """\ The next step is to build a function that assembles a paragraph from several random sentences. It's signature looks like this: build_paragraph(dict, int) -> str The dict argument is a mapping produced by your process_file function. The integer argument is the number of sentences to use to generate a paragraph. It will call your build_sentence() function several times and then join the resulting sentences together into a single string. Use the string join() function to do this.""" random.seed(12345) mapping = markov.process_file("emma.txt") sentence = markov.build_paragraph(mapping, 4) terminators = 0 for letter in sentence: if letter in "?.!": terminators += 1 print "Your sentence:", sentence print "Expected 4 sentences, found %d"%terminators assert terminators >= 4, """\
def test_complex_input_3(): correct_map = markov_solution.build_chains("sample5.txt", 2) new_map = markov.process_file("sample5.txt") for key in correct_map.keys(): print "Expected: %r => %r" % (key, correct_map[key]) print "Received: %r => %r" % (key, new_map[key]) assert sorted(correct_map[key]) == sorted(new_map[key]), """\
def test_complex_input_3(): correct_map = markov_solution.build_chains("sample5.txt", 2) new_map = markov.process_file("sample5.txt") for key in correct_map.keys(): print "Expected: %r => %r"%(key, correct_map[key]) print "Received: %r => %r"%(key, new_map[key]) assert sorted(correct_map[key]) == sorted(new_map[key]), """\
def test_complex_input_1(): mapping = markov.process_file("sample3.txt") output = { ("how", "are"): ["you"], ("are", "you"): ["doing?"] } print "Expected map:", output print "Your map:", mapping assert mapping == output, """\
def test_sentence_generation(): mapping = markov.process_file("sample3.txt") print mapping random.seed(1) received = markov.build_sentence(mapping) expected = "how are you doing?" print "Expected output:", expected print "Your output:", received assert expected == received, """\
def test_build_tweet(): assert fn_exists("build_tweet"), """\ We'll build a new function, build_tweet, which mostly behaves the same as build paragraph, but tries to produce sentences less than 140 characters. The strategy here will be to produce sentences, appending them to each other as long as the complete text is less than 140 characters. For the first sentence, if it is greater than 140 characters, produce a new sentence until you create an appropriate one. The signature for this function is build_tweet(dict) -> str """ mapping = markov.process_file("emma.txt") tweet = markov.build_tweet(mapping) print "Your tweet:", tweet print "Expected: less than 140 characters, found %d"%len(tweet) assert len(tweet) <= 140
def test_basic_markov(): assert markov.process_file("sample1.txt") == {}, """\ Here is the heart of the markov analysis. Let us assume for now that all markov analysis uses a prefix length of two. We have provided for you a series of incrementally complex files to test your markov function. The first file, 'sample1.txt' is completely empty, and should produce an empty output. Now is the time to decide what kind of output process_file returns. The functionality can be stated (in non-programming terms) in the following way: process_file opens a text file, analyzes the text, and returns a mapping of word prefix chains to their suffixes. The only data structure we've learned so far that supports the concept of 'mapping' one value to another is the dictionary, or hash. If sample1.txt is an empty file, we can assume that running process_file("sample1.txt") will produce an empty dictionary, {}. This solidifies our interface, the signature now looks like this: process_file(str) -> dict Where 'str' is the filename and 'dict' contains the Markov mappings. Note: we do not need to specifically check that 'sample1.txt' is an empty file. If we do our processing correctly, our function will return an empty dictionary if there is no input. For now, it is sufficient to blindly return an empty dictionary without actually opening the file. """ output = {("hi", "there"): ["friend."]} assert markov.process_file("sample2.txt") == output, """\
def test_cap(): mapping = markov.process_file("emma.txt") for i in range(5): s = markov.build_sentence(mapping) print "Sentence generated:", s assert s[0] == s[0].capitalize(), """\
def test_complex_input_1(): mapping = markov.process_file("sample3.txt") output = {("how", "are"): ["you"], ("are", "you"): ["doing?"]} print "Expected map:", output print "Your map:", mapping assert mapping == output, """\
def test_cap(): mapping = markov.process_file("emma.txt") for i in range(5): s = markov.build_sentence(mapping) print "Sentence generated:", s assert s[0] == s[0].capitalize(), """Everything's looking pretty good now, you can generate tweets and sentences and paragraphs, the last detail is that sentences are generated with a lowercase letter to start. Make sure that the first word of each sentence is capitalized properly."""