Chapter 12 – Detecting English Programmatically 169
Practice Exercises, Chapter 12, Section A
Practice exercises can be found at http://invpy.com/hackingpractice 12 A.
The Detect English Module
The detectEnglish.py program that we write in this chapter isn’t a program that runs by itself.
Instead, it will be imported by our encryption programs so that they can call the
detectEnglish.isEnglish() function. This is why we don’t give detectEnglish.py a
main() function. The other functions in the program are all provided for isEnglish() to
call.
Source Code for the Detect English Module
Open a new file editor window by clicking on File ► New Window. Type in the following code
into the file editor, and then save it as detectEnglish.py. Press F5 to run the program.
Source code for detectEnglish.py
Detect English module
http://inventwithpython.com/hacking (BSD Licensed)
To use, type this code:
import detectEnglish
detectEnglish.isEnglish(someString) # returns True or False
(There must be a "dictionary.txt" file in this directory with all English
words in it, one word per line. You can download this from
http://invpy.com/dictionary.txt))
- UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
- def loadDictionary():
- dictionaryFile = open('dictionary.txt')
- englishWords = {}
- for word in dictionaryFile.read().split('\n'):
- englishWords[word] = None
- dictionaryFile.close()
- return englishWords
- ENGLISH_WORDS = loadDictionary()
- def getEnglishCount(message):
- message = message.upper()
- message = removeNonLetters(message)
- possibleWords = message.split()