<jupyter_start><jupyter_text>Build your own concordanceIn the lecture, we discussed how it took 500 Dominican munks to write the first concordance of the Latin bible, and how it took <NAME> 10 years to write the first concordance of the Hebrew bible. With Python, it only takes a matter of seconds to find words in a text, along with the surrounding words.In this exercise, first run the code and check that everything works. Then, try modifying the code.Run each cell in this notebook at a time, in order. If something in one cell doesn't work right, it might be because you have overwritten a variable, so try going back and running all the previous cells again.The point of the exercise isn't for you to understand all the commands fully; the idea is to start getting you familiar with how jupyter notebooks work, with the concept of copying and modifying codes, and hopefully give you a little taste of the power of Python.<jupyter_code># install the natural language toolkit package (nltk), which has a copy of several texts, including the King James Bible
%pip install nltk
# import the nltk package so that it is accessible to Python, and download a collection of texts from Project Gutenberg
import nltk
nltk.download('gutenberg')
# Create a variable called "bible" which contains the text of the King James bible.
bible = nltk.corpus.gutenberg.raw('bible-kjv.txt')

# make all characters lowercase
bible = bible.lower()

# remove the "\n" characters, which indicate line breaks in the text (newlines)
bible = bible.replace('\n', ' ')

# split up the text into a long list of individual words
bible = bible.split(' ')
# make a variable called "concordance", and fill it with every occurrence of the phrase "this world", and a few words preceeding and following "this world"
concordance = []
for i, val in enumerate(bible):
    if val == "world":
        if bible[i-1] == "this":
            concordance.append(str(' '.join(bible[i-5:i+5])))
# take a look at what the algorithm has found
concordance<jupyter_output><empty_output><jupyter_text>Let's try again, but this time let's just search for "world" by itself, not "this world".<jupyter_code>concordance = []
for i, val in enumerate(bible):
    if val == "world":
        concordance.append(str(' '.join(bible[i-5:i+5])))
# take a look at what the algorithm has found
concordance<jupyter_output><empty_output><jupyter_text>Now, in the cell below, modify the code to search for a different word.<jupyter_code># add your modified code here and run the cell...<jupyter_output><empty_output><jupyter_text>The nltk package has the full text of several other classic books. You can see what they are called by running the command in the cell below:<jupyter_code>nltk.corpus.gutenberg.fileids()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup