Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How do I find the percentage of similarity between strings?

by Superman (Acolyte)
on Apr 25, 2002 at 17:01 UTC ( #162038=perlquestion: print w/replies, xml ) Need Help??

Superman has asked for the wisdom of the Perl Monks concerning the following question: (strings)

I have 2 strings which should be identical:
$PDBline; $XMLline;
and I test for this normally:
if ($PDBline eq $XMLline){etc...}
However in the event that they dont match I want to do a percentage comparison where I match each word in $PDBline against the words in $XMLline.

So say I want a 75 % similarity between the 2 strings...

my $threshold = 0.75; my $counter = 0; foreach my $word ( split ' ', $PDBline ) { $counter++ if $XMLline =~ /$word/; }
Does this seem right or is there an easier way to do this??

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I find the percentage of similarity between strings?
by I0 (Priest) on Apr 25, 2002 at 20:43 UTC
    use String::Approx 'adistr';
Re: How do I find the percentage of similarity between strings?
by The Mad Hatter (Priest) on Apr 07, 2003 at 22:31 UTC
Re: How do I find the percentage of similarity between strings?
by chicks (Scribe) on Apr 26, 2002 at 01:50 UTC
    I'm personally quite fond of Algorithm::Diff. It's line-oriented and you asked for word-oriented, so it might not work for what you're trying to do precisely.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://162038]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2021-10-26 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?