http://qs1969.pair.com?node_id=917093

sarvan has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I have the following script for computing similarity between two sentences.
#!/usr/bin/perl -w use strict; my $candidate = "it is not probable that it is the end"; my $reference = "but it is unlikely that this is the end"; my @candidate_words = split (/\W+/, $candidate); my $candidate_count=@candidate_words; my %cand_histogram; my %ref_histogram; my $valHolder=""; our $res=0; $cand_histogram{$_}++ foreach (split(/\s+/,$candidate)); $ref_histogram{$_}++ foreach (split(/\s+/,$reference)); my %seen; printf "%-6s %-10s %-10s %10s\n", 'Key','Candidate','Reference','MinCo +unt'; foreach my $key ( sort { $seen{$b} <=> $seen{$a} #descending word cnt or $a cmp $b #alphabetic otherwise } grep {!$seen{$_}++} # each key just once, # but count 'em also! (keys %cand_histogram) ) { #$valHolder=$cand_histogram{$key}; $valHolder=0; if($cand_histogram{$key} && $ref_histogram{$key}){ $valHolder=$cand_histogram{$key}; if($cand_histogram{$key} >= $ref_histogram{$key}){ $valHolder=$ref_histogram{$key}; } } $res+=$valHolder; printf "%-6s %-10s %-10s %10s\n", $key, $cand_histogram{$key}||='0', $ref_histogram{$key} ||='0',$valHolder; } my $BS=$res/$candidate_count; print "The calculated bleu Score is:$BS\n";

Here i have computed the similarity score between two sentences by taking unigrams(i.e individual words from sentences).

Now, i want to turn this same script to do the same computation but with n-gram(with n=3 i.e taking three words together through out the computation).

for eg. in unigram computation if i have a sentence "it is not probably that it is the end". then in n-gram words should be in the form

it is not is not probably not probably that that it is it is the is the end

In this way i want to compute the similarity score between the two sentences.. any help please Thanks..

Output:
Key Candidate Reference MinCount end 1 1 1 is 2 2 2 it 2 1 1 not 1 0 0 probable 1 0 0 that 1 1 1 the 1 1 1 The calculated bleu Score is:0.666666666666667