#!/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";