Code as a Liberal Art, Spring 2024

Unit 2, Lesson 3 Homework

Due: Wednesday, April 3, 8pm

  1. Review the class notes for this week.
  2. Make sure that you have the code that we worked on in class operating correctly.

    Start by running it as we implemented it in class, on the very simple example text that we were discussing in class.

    I thought it might also be useful if we used a short, controlled sample text for which we could display the resulting data structure and visually analyze it for correctness. Download this file:

    unit2lesson3_sample.txt

    Modify the first line so it is opening this file as its input text. Then, add the following lines to the end of your program:

    import pprint
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(markov_data)
    
    can you try to get Viyan's version of the algorithm running? See the end of the classnotes for some explanation of how this works.

    When I implement Viyan's version of the algorithm, this is what I get. See if you can generate this same output:

    {'There': ['are', 'are'],
     'are': ['many', 'also', 'really'],
     'many': ['things', 'things', 'years', 'people'],
     'things': ['that', 'that'],
     'that': ['I', 'I', 'they', 'they'],
     'I': ['would', 'would', 'would', 'have'],
     'would': ['like', 'like', 'like', 'like', 'most'],
     'like': ['to', 'to', 'to', 'to', 'to'],
     'to': ['learn.',
            'do.',
            'learn',
            'write',
            'play',
            'play',
            'learn.',
            'learn.',
            'learn',
            'practice.'],
     'learn.': ['There', 'For', 'The'],
     'also': ['many'],
     'do.': ['For'],
     'For': ['example', 'many', 'many', 'other'],
     'example': ['I', 'is'],
     'learn': ['how', 'is'],
     'how': ['to', 'to'],
     'write': ['computer'],
     'computer': ['programs.'],
     'programs.': ['Another'],
     'Another': ['example'],
     'is': ['learning', 'a', 'the', 'the', 'to'],
     'learning': ['how', 'and'],
     'play': ['saxophone.', 'jazz.'],
     'saxophone.': ['For'],
     'years': ['I'],
     'have': ['thought'],
     'thought': ['about'],
     'about': ['learning.', 'it,'],
     'learning.': ['The'],
     'The': ['saxophone', 'best'],
     'saxophone': ['is'],
     'a': ['musical'],
     'musical': ['instrument', 'instrument'],
     'instrument': ['used', 'that', 'that'],
     'used': ['to'],
     'jazz.': ['For'],
     'people': ['this', 'the'],
     'this': ['is'],
     'the': ['musical', 'piano', 'instrument', 'same'],
     'they': ['would', 'would'],
     'other': ['people'],
     'piano': ['is'],
     'most': ['like'],
     'best': ['way'],
     'way': ['to'],
     'practice.': ['Actually'],
     'Actually': ['when'],
     'when': ['you'],
     'you': ['think'],
     'think': ['about'],
     'it,': ['learning'],
     'and': ['doing'],
     'doing': ['are'],
     'really': ['the'],
     'same': ['thing.']}
    
  3. You can run it on the David Copperfield text that I shared, or a different text or group of texts that you might find online.
  4. As an optional follow-up exercise,

    Now run the program and you should see this output:

    {   'Actually': {'when': 1},
        'Another': {'example': 1},
        'For': {'example': 1, 'many': 2, 'other': 1},
        'I': {'have': 1, 'would': 3},
        'The': {'best': 1, 'saxophone': 1},
        'There': {'are': 2},
        'a': {'musical': 1},
        'about': {'it,': 1, 'learning.': 1},
        'also': {'many': 1},
        'and': {'doing': 1},
        'are': {'also': 1, 'many': 1, 'really': 1},
        'best': {'way': 1},
        'computer': {'programs.': 1},
        'doing': {'are': 1},
        'example': {'I': 1, 'is': 1},
        'have': {'thought': 1},
        'how': {'to': 2},
        'instrument': {'that': 2, 'used': 1},
        'is': {'a': 1, 'learning': 1, 'the': 2, 'to': 1},
        'it,': {'learning': 1},
        'learn': {'how': 1, 'is': 1},
        'learn.': {'There': 1},
        'learning': {'and': 1, 'how': 1},
        'like': {'to': 5},
        'many': {'people': 1, 'things': 2, 'years': 1},
        'most': {'like': 1},
        'musical': {'instrument': 2},
        'other': {'people': 1},
        'people': {'the': 1, 'this': 1},
        'piano': {'is': 1},
        'play': {'jazz.': 1, 'saxophone.': 1},
        'practice.': {'Actually': 1},
        'really': {'the': 1},
        'same': {'thing.': 1},
        'saxophone': {'is': 1},
        'saxophone.': {'For': 1},
        'that': {'I': 2, 'they': 2},
        'the': {'instrument': 1, 'musical': 1, 'piano': 1, 'same': 1},
        'they': {'would': 2},
        'things': {'that': 2},
        'think': {'about': 1},
        'this': {'is': 1},
        'thought': {'about': 1},
        'to': {   'do.': 1,
                  'learn': 2,
                  'learn.': 3,
                  'play': 2,
                  'practice.': 1,
                  'write': 1},
        'used': {'to': 1},
        'way': {'to': 1},
        'when': {'you': 1},
        'would': {'like': 4, 'most': 1},
        'write': {'computer': 1},
        'years': {'I': 1},
        'you': {'think': 1}}
    

    Now you can verify that your program is running correctly by making sure your output looks like this. I hope that you might also be able to scrutinize this output, compare it to the input program, and gain some deeper insight into how this algorithm is running.