Skip to content

Word counting and Zipf's Law

On the final day of my Learning Perl class, I talk about Zipf's Law because people now have enough Perl to read a large file, break it up into words, count those words, and sort them by their count.

The final piece of Perl involves sorting a hash by value, which we cover late in the book:

foreach ( @words ) {
	$Count{$_}++;
	}

foreach ( sort { $Count{$b} <=> $Count{$a} } keys %Count ) {
	printf "%d %s\n", $Count{$_}, $_;
	}

Zipf's Law states that the frequency of things has a power law relationship. The most frequent thing is twice as big as the second biggest thing and three times as large as the third most frequent thing. It doesn't matter what the thing is. A mysterious law that predicts the size of the world's biggest cities has a good discussion of it.

Here are a few plots where I've removed the labels and the scale. Each is a different topic.

An unlabeled bar chart of US box office receipts in descending order, dropping off steeply and then leveling out
Ranking box office returns
An unlabeled bar chart of the most frequent words in Moby Dick in descending order, dropping off steeply and then leveling out
Ranking words in Moby Dick
An unlabeled bar chart of the populations of the largest US cities in descending order, dropping off steeply and then leveling out
Ranking populations of cities

To illustrate this in class, I use the King James Bible and Moby Dick, two out-of-copyright texts I downloaded from Project Gutenberg. I have edited versions that removed the headers and footers (King James Bible and Moby Dick).

I use a simple program (limited to what we show in Learning Perl) to compile the frequencies:

use v5.14;
use List::Util qw(max);

LINE: while( <> ) {
	chomp;
	$_ = lc $_;
	s/\p{Punct}/ /;	# unsophisticated
	WORD: foreach ( split ) {
		next unless /[a-z]/;
		my $normalized = s/\P{Letter}//gr; # also unsophisticated
		$Count{$normalized}++;
		}
	}

my $max = max( values %Count );
my $width = length $max;
print STDERR "Max is $max\n";
printf STDERR "There are %d words\n", scalar keys %Count;

print "Count\tWord";
foreach my $key ( sort { $Count{$b} <=> $Count{$a} } keys %Count ) {
	printf "%${width}d\t%s\n", $Count{$key}, $key;
	last if $displayed++ > 24;
	}

Here are the results for those two books:

The bar chart Words in the King James Bible, with the most frequent word at about 64,000 uses, the next two at about 52,000 and 35,000, and the rest from about 13,000 down to 4,500
The 24 most common words in the King James Bible
The bar chart Words in Moby Dick, with the most frequent word at about 14,000 uses, the next two at about 6,400, and the rest falling to about 1,000
The 24 most common words in Moby Dick

The other unlabeled plot I showed at the beginning was the US box office receipts for the day I wrote this article:

The bar chart US Box Office Receipts 7/29/2014, falling from about $5.4 million for the top movie to under $100,000
US box office receipts for one day

And, a bonus plot, with the population of major US cities:

The bar chart Population of Top 25 US Cities, falling from about 8.2 million for the largest city to about 600,000
Populations of major US cities, 2010