CrackingCodes.Ch11 package¶
Submodules¶
CrackingCodes.Ch11.PracticeQuestions module¶
Chapter 11 Practice Questions
Answers Chapter 11 Practice Questions via Python code.
CrackingCodes.Ch11.detectEnglish module¶
Detect English Module
Provides functions to determine whether a given string is in the English language.
-
CrackingCodes.Ch11.detectEnglish.
UPPERLETTERS
¶ String containing all latin-based letters in uppercase.
Type: str
-
CrackingCodes.Ch11.detectEnglish.
LETTERS_AND_SPACE
¶ String containing upper and lowercase letters as well as space, newline, and tab.
Type: str
-
CrackingCodes.Ch11.detectEnglish.
DICTIONARY_FILE
¶ String containing absolute path of dictionary.txt file.
Type: str
-
CrackingCodes.Ch11.detectEnglish.
ENGLISH_WORDS
¶ Dictionary containing all words from dictionary.txt as keys.
Type: dict
Example
>>> import pythontutorials.books.CrackingCodes.Ch11.detectEnglish as detectEnglish
>>> someString = 'Enthusiasm is contagious. Not having enthusiasm is also contagious.'
>>> detectEnglish.isEnglish(someString) # Returns True or False
True
Note
- https://www.nostarch.com/crackingcodes/ (BSD Licensed)
- 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 https://www.nostarch.com/crackingcodes/.
-
CrackingCodes.Ch11.detectEnglish.
getEnglishCount
(message: str) → float[source]¶ Get count of English words
For given message, counts number of words in English dictionary and returns ratio of English words out of total words.
Parameters: message – String with message to check for English words. Returns: Ratio of number of English words / total number of words.
-
CrackingCodes.Ch11.detectEnglish.
isEnglish
(message: str, wordPercentage: int = 20, letterPercentage: int = 85) → bool[source]¶ Determines whether message is English
Using given word percentage and letter percentage, determines if a given message is in the English language.
Parameters: - message – String containing message to determine if it is English.
- wordPercentage – Integer representing percentage of words in message that must be English.
- letterPercentage – Integer representing percentage of characters in message that must be letters or spaces.
Returns: True if message is in English language, False otherwise.
Note
- By default, 20% of the words must exist in the dictionary file, and 85% of all the characters in the message must be letters or spaces (not punctuation or numbers).