For our first day and first data science project, we're going to do some rudimentry analysis of Shakespeare's classic play: Macbeth! You will get practice working with lists, condtionals and dictionaries, visualizing data, and thinking analytically about data.
You will be able to:
- Show mastery of the content covered in this section
Here we start by importing a python package and using it to pull the transcript of Macbeth from the project Gutenberg website. We also preview a few details about what is now stored in the variable macbeth; it's a string with 119,846 characters, the first 500 of which are printed below.
import requests
macbeth = requests.get('http://www.gutenberg.org/cache/epub/2264/pg2264.txt').text
print(type(macbeth))
print(len(macbeth))
print(macbeth[:500])
<class 'str'>
119846
***The Project Gutenberg's Etext of Shakespeare's First Folio***
********************The Tragedie of Macbeth*********************
This is our 3rd edition of most of these plays. See the index.
Copyright laws are changing all over the world, be sure to check
the copyright laws for your country before posting these files!!
Please take a look at the important information in this header.
We encourage you to keep this file on your own disk, keeping an
electronic path open for the nex
Your task is to create a bar graph of the 25 most common words in the Shakespeare's Macbeth.
A common python programming pattern to counting objects, produce histograms, or update statistics is to make calls to a dictionary as you iterate through a list. For example, given a list of words, you can create a dictionary to store counts and then iterate through the list of words, checking how many times each word has appeared using your dictionary, and updating the dictionary count now that you've seen that word again. The dictionary.get()
method is very useful in doing this. Read the docstring for the dictionary.get() method and use it along with the pseudocode above to create a bar graph of the 25 most common words from the transcript of Macbeth which has been loaded into a variable 'Macbeth'. Be sure to include a title and appropriate labels for your graph.
from collections import Counter
import matplotlib.pyplot as plt
# Your code here
# Pseudo-code Outline
# Split the transcript into words
macbeth_words = macbeth.split()
# Create a dictionary & Iterate through the text of Macbeth
macbeth_counter = Counter(macbeth_words)
most_common = macbeth_counter.most_common(10)
macbeth_dict = dict(most_common)
word_list = list(macbeth_dict.keys())
word_counts = list(macbeth_dict.values())
# Create Bar Graph
plt.bar(word_list, word_counts)
# Include descriptive titles and labels
plt.xlabel('Word')
plt.xticks(word_list[:10], rotation = 'vertical')
plt.ylabel('word count')
# Give a title to the bar graph
plt.title("Top 10 Words in Macbeth")
plt.show()
This project should take you about an hour and a half to complete. If you're done much more quickly than that and are not behind in the course, feel free to deepen your knowledge by completing any or all of the following tasks until you run out of time:
- Create a list of top characters by mentions of their names
- Split the text by which character is talking
- Create sub graphs of the most common words by character
- Reduce the string to the text of the play itself. (Remove any initial notes, forward, introduction, appendix, etc.)
- Come up with some other fun analyses of the text!
Congratulations! You've got some extra practice combining various data types into useful programming patterns and done an intitial analysis of a classic text!