Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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..

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2023-12-11 19:55 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.