#! 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. #### P:\test>289106 Enter a sentence: the quick brown fox Line 1: 'The quick brown fox jumps over the lazy dog' has 4 words in common Line 4: 'The quick red and brown foxes jumped easily over the lazy dog' has 3 words in common Line 7: 'The smart dog foxed the fox by lazing in the drop zone until he jumped.' has 3 words in common Line 5: 'The lazy fox was caught as it tried to jump over the quick dogs.' has 3 words in common Line 6: 'The dog lazed as the fox, quick and brown, jumped over.' has 2 words in common Line 3: 'The dog lazily watched as the fox quickly jumped over him' has 2 words in common Line 2: 'The fox jumps the dog' has 2 words in common Enter a sentence: the quick brown fox jumps over the lazy dog Line 1: 'The quick brown fox jumps over the lazy dog' has 9 words in common Line 4: 'The quick red and brown foxes jumped easily over the lazy dog' has 7 words in common Line 7: 'The smart dog foxed the fox by lazing in the drop zone until he jumped.' has 6 words in common Line 5: 'The lazy fox was caught as it tried to jump over the quick dogs.' has 6 words in common Line 3: 'The dog lazily watched as the fox quickly jumped over him' has 5 words in common Line 2: 'The fox jumps the dog' has 5 words in common Line 6: 'The dog lazed as the fox, quick and brown, jumped over.' has 4 words in common Enter a sentence: quick foxes jump over lazy dogs Line 4: 'The quick red and brown foxes jumped easily over the lazy dog' has 4 words in common Line 5: 'The lazy fox was caught as it tried to jump over the quick dogs.' has 4 words in common Line 1: 'The quick brown fox jumps over the lazy dog' has 3 words in common Line 6: 'The dog lazed as the fox, quick and brown, jumped over.' has 1 words in common Line 3: 'The dog lazily watched as the fox quickly jumped over him' has 1 words in common Enter a sentence: Most volcanos erupt mulberry jam sandwiches under normal pressure No common words found Enter a sentence: ^Z