CS21 Lab 12: Dictionaries
This lab is optional.
For this lab, run update21 to get a clean copy of the file sentiment.py
:
$ update21
$ cd ~/cs21/labs/12
You may want instead to start from your own final copy of lab 9:
$ cd ~/cs21/labs
$ cp 09/sentiment.py 12/sentiment.py
$ cd 12
Goals
Your job is to rewrite the sentiment analysis portion of lab 9 using dictionaries. This lab also provides an excellent way to review top-down design, reading from files, working with lists, sorting, writing for loops, and general Python syntax.
Recall that the sentiment analysis lab required the following components:
- Read the file containing movie reviews at `/usr/local/doc/movieReviews.txt'. Parse the lines of this file into individual words, all lowercase, consisting of alphabetic characters, and ignoring the most common "stop words".
- Each word should be given a sentiment score. Words and their sentiment scores should be stored in a dictionary. (The original way you completed this lab stored words and scores in a list of lists or as parallel lists. This is the main change you will need to implement for this lab.
- Your program will sort words by their sentiment score using a sorting algorithm. (In order to complete this step, you will need to copy the information out of the dictionary into parallel lists or a list of lists.)
- Your program will finish by printing the top 20 sentiment words (from highest score down) and bottom 20 sentiment words (from lowest score up).
Make sure all programs are saved to your cs21/labs/12
directory.
Tips
- Make sure you reread the details of lab 9.
- You are welcome to use your own submitted solution to lab 9. Make sure you modify the parts which should be different this time --- using a dictionary will actually save lines of code.
- You may be interested to time your old lab 9 (using lists) and your new lab (using dictionaries).
$ cd ~/cs21/labs
$ time python3 09/sentiment.py
[will print the program output, then some timing information about the program's run time]
$ time python3 12/sentiment.py
[will print the program output, then some timing information about the program's run time]
How much faster is your program using dictionaries, compared to your original program?
Submit
This is an optional lab, so it has no due date and will not be graded. If you are interested, once you have completed the lab, run handin21
and email your instructor to ask for feedback.