I suppose I could write this to use a linear counting method of some sort, but that's for another day.use strict; use warnings; my $words = 1; # Phrase must contain at least 1 word my $size = 5; # Phrase must be at least 5 characters long my $matches = 2; # Must be at least 2 copies of phrase my (@words, @pos, @matches, $p, $c, $key); $_ = join '', <DATA>; $_ = lc($_); @words = m/\w+(?:'\w+)?/g; @pos = sort { mycmp($a, $b) } 0..$#words; for $p ($words..$#pos) { $c = mycount($p); for (1..$c) { $key = join ' ', @words[$pos[$p]..($pos[$p]+$_-1)]; next if length($key) < $size; $matches[$_]{$key}++; } } for (reverse $words..$#matches) { print "$_ words:\n"; $c = $matches[$_]; for (sort { $c->{$b} <=> $c->{$a} } keys %$c) { last if $c->{$_} < $matches - 1; print " $_ : ",($c->{$_}+1),"\n"; } } sub mycount { my $x = $pos[$_[0]]; my $y = $pos[$_[0]-1]; my $c = 0; $c++ while $x <= $#words && $y <= $#words && $words[$x++] eq $word +s[$y++]; return $c; } sub mycmp { my ($x, $y) = @_; while ($x <= $#words && $y <= $#words) { return $c if $c = $words[$x++] cmp $words[$y++]; } return $x <=> $y; } __DATA__ Section. 1. All legislative Powers herein granted shall be vested in a Congress of + the United States, which shall consist of a Senate and House of Repr +esentatives. Section. 2. The House of Representatives shall be composed of Members chosen every + second Year by the People of the several States, and the Electors in + each State shall have the Qualifications requisite for Electors of t +he most numerous Branch of the State Legislature. No Person shall be a Representative who shall not have attained to the + Age of twenty five Years, and been seven Years a Citizen of the Unit +ed States, and who shall not, when elected, be an Inhabitant of that +State in which he shall be chosen.
In reply to Re: Finding recurring phrases
by TedPride
in thread Finding recurring phrases
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |