#! perl -slw use strict; use Data::Dumper; # Read an array of the DB sentences and index them. my @lines; my %lookup; while( my $line = ) { push @{ $lookup{ $_ } }, $. for split ' ', $line; $lines[ $. ] = $line; } close DATA; chomp @lines; # Process each newline by while( 1 ) { printf "\nEnter a sentence: "; my $newline = ; last unless $newline; my @words = split ' ', $newline; # building a hash with the sentence (line) number as key # and the number of words it has in common # with the new sentence as the value my %occurs; $occurs{ $_ }++ for map{ my $aref = $lookup{ $_ }; defined $aref ? @$aref : () } @words; print "No common words found" and next unless keys %occurs; # Process (display) only thise lines that have some correlation # sort by the strength of the correlation. print "Line $_: '$lines[ $_ ]' has $occurs{ $_ } words in common", for sort{ $occurs{ $b } <=> $occurs{ $a } } keys %occurs } __DATA__ The quick brown fox jumps over the lazy dog The fox jumps the dog The dog lazily watched as the fox quickly jumped over him The quick red and brown foxes jumped easily over the lazy dog The lazy fox was caught as it tried to jump over the quick dogs. The dog lazed as the fox, quick and brown, jumped over. The smart dog foxed the fox by lazing in the drop zone until he jumped.