use strict; my (@lines, @words, $i, $pair, %hash); foreach (<DATA>) { $_ =~ s/[^\w\. ]//g; # Remove unneeded characters $_ =~ s/ +/ /g; # Many spaces to one $_ =~ s/ ?\. ?(\. ?)*/\./g; # Boundaries with . change to . $_ =~ s/^ //; $_ =~ s/ $//; # Spaces at start and end removed $_ = lc($_); # Lowercase @lines = split(/\./, $_); # Split on sentence boundaries foreach (@lines) { # Get words for each sentence @words = split(/[\. ]/, $_); for ($i = 0; $i < $#words; $i++) { # For each word pair $pair = @words[$i] . ' ' . @words[$i+1]; $hash{$pair}++; # Increment count for word pair } } } foreach (sort {$hash{$b} <=> $hash{$a}} keys %hash) { print $_ . ' ' . $hash{$_} . "\n"; } __DATA__ Four score and seven years ago our fathers brought forth, upon this co +ntinent, a new nation, conceived in liberty, and dedicated to the pro +position that "all men are created equal" Now we are engaged in a great civil war, testing whether that nation, +or any nation so conceived, and so dedicated, can long endure. We are + met on a great battle field of that war. We have come to dedicate a +portion of it, as a final resting place for those who died here, that + the nation might live. This we may, in all propriety do. But, in a l +arger sense, we can not dedicate -- we can not consecrate -- we can n +ot hallow, this ground -- The brave men, living and dead, who struggl +ed here, have hallowed it, far above our poor power to add or detract +. The world will little note, nor long remember what we say here; whi +le it can never forget what they did here. It is rather for us, the living, we here be dedicated to the great tas +k remaining before us -- that, from these honored dead we take increa +sed devotion to that cause for which they here, gave the last full me +asure of devotion -- that we here highly resolve these dead shall not + have died in vain; that the nation, shall have a new birth of freedo +m, and that government of the people by the people for the people, sh +all not perish from the earth.
In reply to Re: Word Pairs and Lines
by TedPride
in thread Word Pairs and Lines
by bob
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |