Hacking Secret Ciphers with Python

(Ann) #1
Chapter 12 – Detecting English Programmatically 177


  1. englishWords[word] = None


In our program, we only use a dictionary for the englishWords variable so that the in
operator can find keys in it. We don’t care what is stored for each key, so we will just use the
None value. The for loop that starts on line 16 will iterate over each word in the dictionary file,
and line 17 will use that word as a key in englishWords with None stored for that key.


Back to the Code


detectEnglish.py


  1. dictionaryFile.close()

  2. return englishWords


After the for loop finishes, the englishWords dictionary will have tens of thousands of keys
in it. At this point, we close the file object since we are done reading from it and then return
englishWords.


detectEnglish.py


  1. ENGLISH_WORDS = loadDictionary()


Line 21 calls loadDictionary() and stores the dictionary value it returns in a variable
named ENGLISH_WORDS. We want to call loadDictionary() before the rest of the code in
the detectEnglish module, but Python has to execute the def statement for
loadDictionary() before we can call the function. This is why the assignment for
ENGLISH_WORDS comes after the loadDictionary() function’s code.


detectEnglish.py


  1. def getEnglishCount(message):

  2. message = message.upper()

  3. message = removeNonLetters(message)

  4. possibleWords = message.split()


The getEnglishCount() function will take one string argument and return a float value
indicating the amount of recognized English words in it. The value 0.0 will mean none of the
words in message are English words and 1.0 will mean all of the words in message are
English words, but most likely getEnglishCount() will return something in between 0.0
and 1.0. The isEnglish() function will use this return value as part of whether it returns
True or False.

Free download pdf