in reply to Smart Comparison of Buffy Strings
Here's how it hands the "gonna" -> "going to" change.
To make this work in practice, you would need to do things like ensure that the shorter quote was at least n words long, so that a 1 word candidate quote didn't match at 100%.use strict; use Algorithm::Diff qw(traverse_sequences); my @quote1 = split(' ', <<QUOTE); I walk. I talk. I shop, I sneeze. I'm gonna be a fireman when the floods roll back. There's trees in the desert since you moved out. And I don't sleep on a bed of bones. QUOTE my @quote2 = split(' ', <<QUOTE); I'm going to be a fireman when the floods roll back. There's trees in the desert since you moved out. QUOTE my $match = 0; traverse_sequences(\@quote1, \@quote2, { MATCH => sub { $match++ } } ); my $smaller = @quote1 < @quote2 ? @quote1 : @quote2; my $percent_match = int(($match / $smaller) * 100); print "$match words matched ($percent_match%)\n"; __END__ 18 words matched (90%)
A few minutes perusing the Algorithm::Diff POD might give you some additional ideas.
|
---|