Hey, everyone, and welcome back to this class natural language processing in Python.
In this lecture, we are going to begin looking at the code to demonstrate our cipher decryption algorithm.
This particular lecture will focus on how to create the original substitution cipher.
Remember that this is a simulation.
Normally, we don't know the true answer, but in this case we do.
The purpose is that we can compare our answer to the true answer to assess the efficacy of our algorithm.
So our goal by the end of this code is to see if we get the correct answer and we will know what the
correct answer is because that's what we started with.
First, we're going to do all of our usual imports, Nampai and map plot lib.
We'll also need the string library, which provides us with a convenient way to load all the valid ASCII
characters.
We'll need the random library to shuffle the ASCII characters and create our substitution cipher.
We'll need the regex library to do some string operations later on in the scripts, and we'll also need
the O's and Tex rep libraries to do some miscellaneous tasks later on.
So this lecture will focus on this first block of code where we create the substitution cipher to start,
we'll just create two lists of letters, letters one and letters two.
When you call string that ASCII lowercase.
This returns all 26 letters in lowercase in alphabetical order.
You can print them out yourself in order to confirm what I've said is correct.
We also cast the Strings two lists.
This is helpful so we can have one element of each list represent a single character at a time.
Next, we initialize an empty dictionary called True Mapping.
The rest of this code will populate this dictionary, so the next step is to randomize the second list
of letters letters to.
There's no need to randomize the first list since those will just be the keys of the dictionary, and
they'll map to the letters of the second list.
Next, we loop through both lists in corresponding order.
We can accomplish that by using the zip function.
So in the first iteration of this loop, K will be the first element of letters one, and we will be
the first element of letters to.
On the second iteration, Kay will be the second element of letters one, and we will be the second
element of letters to inside the loop.
We assign Kay to be the key of the true mapping and a V to be the value.
Once we complete this loop, the true mapping dictionary will represent our substitution cipher.
Conceptually, only the sender and receiver know what the true mapping is and the intruder does not.
The intruder will use the genetic algorithm we described previously.
So the rest of his lectures are under the assumption that you are the intruder and you're trying to
decrypt the message without knowing what this true mapping is.