in reply to Word density

This should get you started - extract the words into a stack (array). Then in a loop: pull the first word off the stack, process it, check that there is a word remaining, combine with the previous top word and process, check that there is a third word, combine and process.

use warnings; use strict; use Text::ExtractWords; my $text = do {local $/; <DATA>}; # Slurp the text my @words; my %hash; words_list (\@words, $text); while (@words) { ++$hash{$_ = shift @words}; ++$hash{"$_ $words[0]"} if exists $words[0]; ++$hash{"$_ $words[0] $words[1]"} if exists $words[1]; } print "$_: $hash{$_}\n" for sort keys %hash; __DATA__ Three blind mice. Three blind mice. See how they run. See how they +run. The butcher's wife came after them with a knife, three blind mice.

Prints:

a: 1 a knife: 1 a knife three: 1 after: 1 after them: 1 after them with: 1 blind: 3 blind mice: 3 blind mice see: 1 blind mice three: 1 butcher's: 1 butcher's wife: 1 butcher's wife came: 1 came: 1 came after: 1 came after them: 1 how: 2 how they: 2 how they run: 2 knife: 1 knife three: 1 knife three blind: 1 mice: 3 mice see: 1 mice see how: 1 mice three: 1 mice three blind: 1 run: 2 run see: 1 run see how: 1 run the: 1 run the butcher's: 1 see: 2 see how: 2 see how they: 2 the: 1 the butcher's: 1 the butcher's wife: 1 them: 1 them with: 1 them with a: 1 they: 2 they run: 2 they run see: 1 they run the: 1 three: 3 three blind: 3 three blind mice: 3 wife: 1 wife came: 1 wife came after: 1 with: 1 with a: 1 with a knife: 1

DWIM is Perl's answer to Gödel