Source

pythonwise / ll-freq.py

#!/usr/bin/env python
'''Show last letter frequency in English words'''

from urllib import urlopen
from collections import Counter
from matplotlib import pyplot as plt
import numpy as np


def get_words():
    '''Get list of dictionary words.'''
    # The North American Scrabble Tournament Word List (178,690 words)
    fo = urlopen('http://norvig.com/ngrams/TWL06.txt')
    return (w.strip() for w in fo)


def plot(freq, filename):
    '''Bar chart of last letter frequency.'''
    width = 0.35  # Bar width (also used for xticks positioning)

    idx = np.arange(len(freq))
    xs = sorted(freq)  # Letters in alphabetic order
    ys = [freq[x] for x in xs]  # Frequency

    plt.bar(idx, ys, width=width)
    plt.xticks(idx + width/2., xs)  # Annotate X axis
    plt.title('Last Letter Frequency')
    plt.savefig(filename)


if __name__ == '__main__':
    freq = Counter(w[-1].upper() for w in get_words())
    plot(freq, 'll-freq.png')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.