#!/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','MinCount';
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";
####
it is not
is not probably
not probably that
that it is
it is the
is the end
##
##
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