Everyone and welcome back to this class natural language processing in Python.
In this lecture, we will continue looking at our code to implement a cipher, a decryption algorithm.
This lecture will focus on creating functionality to encode and decode a message.
The first thing we need, obviously, is an actual message.
This message is a paragraph I took from another classic book The Adventures of Sherlock Holmes.
So below this, I've commented out a few more lines, which are other paragraphs from the same book.
The results we get at the end will depend on what the original message was.
So you can try adding these to the message if you like to see what kind of results you get and what
kind of tuning you have to do, if any, to get it to work.
For example, you might change the size of the DNA pool.
The number of children per parent or the number of epochs.
Next, we have a function to encode a message it takes as input, a raw message such as the above and
returns cipher text representing that message using the substitution cipher.
We defined at the start of the scripts inside the function.
The first thing we do is lowercase all the letters.
This is because our mapping only contains lowercase letters.
Next, we use our regex familiar to replace all non alpha characters with a space.
This is because we do not have any mappings for non alpha characters.
Another option would be to simply leave those characters as is, although that would take some additional
work.
Next, we enter a loop to build up the coded message.
We start by initializing the coded message as an empty list will be converting and storing each character
one at a time.
Inside the loop will initialize coated C-H to be just C-H.
This is because if the character is a non alphabetical character, then there is no mapping for it.
Next, we check if C-H is in our dictionary.
True mapping If it is, we indexed through mapping by C-H to get the cipher character.
We assign this to coded C-H.
Next, we append coded C-H to the coded message.
Finally, when we've completed the loop, we join each character in coded message to create a final
string.
Next, outside the function, we use our encode message function to encode the original message, and
we'll call this variable and coded message.
Next, we have a function to decode the message this takes in two arguments a cipher text message and
a word map.
This function largely works the same as the encode function.
It just maps each character in the input to a different character, according to the word map.
It's just that this word map is an argument to the function, whereas for encoding the word map was
the true mapping we defined earlier.
So to start, we create an empty list called Decoded Message.
Then we lived through each character in the message Inside the Loop.
We assign C-H to decoded C-H.
Remember, this might be a non alpha character, in which case we would not do anything to it.
Next, we check if C-H is in our word map.
If it is, we indexed the word map using C-H and assign the result to decoded C-H.
Then we append that decoded C-H to the variable decoded message.
Finally, we return the decoded message joined into a single string.