Created
August 29, 2013 01:41
-
-
Save albertyw/6373408 to your computer and use it in GitHub Desktop.
Most Common Letters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Sort the letters of the alphabet by their frequency in the 1000 most common words | |
| """ | |
| # Read text file | |
| WORDS_LIST_FILE = "common-words-list.txt" | |
| file_handle = open(WORDS_LIST_FILE, 'r') | |
| text = file_handle.read() | |
| file_handle.close() | |
| text = text.lower().replace("\n",'') | |
| # Initialize frequency table of letters | |
| alphabet_ascii = range(97,123) | |
| alphabet = [chr(ascii) for ascii in alphabet_ascii] | |
| alphabet_frequency = {} | |
| for letter in alphabet: | |
| alphabet_frequency[letter] = 0 | |
| # Count frequencies | |
| for letter in text: | |
| alphabet_frequency[letter] += 1 | |
| # Sort frequencies | |
| alphabet = [] | |
| for letter, frequency in alphabet_frequency.items(): | |
| alphabet.append( (letter, frequency) ) | |
| alphabet.sort(key=lambda x: -x[1]) | |
| # Print nicely | |
| for letter, frequency in alphabet: | |
| print letter, frequency |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment