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

In reply to need help in extending the code by sarvan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.