``````package Set::Similarity::Jaccard;

use strict;
use warnings;

use parent 'Set::Similarity';

our \$VERSION = '0.027';

sub from_sets {
my (\$self, \$set1, \$set2) = @_;

my \$intersection = \$self->intersection(\$set1,\$set2);
my \$union = \$self->combined_length(\$set1,\$set2) - \$intersection;
# ( A intersect B ) / (A union B)
return (\$intersection / \$union);
}

1;

__END__

Set::Similarity::Jaccard - Jaccard coefficent for sets

use Set::Similarity::Jaccard;

my \$jaccard = Set::Similarity::Jaccard->new;
my \$similarity = \$jaccard->similarity('Photographer','Fotograf');

The Jaccard coefficient measures similarity between sample sets, and is defined as the
size of the intersection divided by the size of the union of the sample sets

( A intersect B ) / (A union B)

The Tanimoto coefficient is the ratio of the number of elements common to both sets to
the total number of elements, i.e.

( A intersect B ) / ( A + B - ( A intersect B ) ) # the same as Jaccard

The range is 0 to 1 inclusive.

L<Set::Similarity::Jaccard> inherits all methods from L<Set::Similarity> and implements the
following new ones.

my \$similarity = \$object->from_sets(['a'],['b']);

This method expects two arrayrefs of strings as parameters. The parameters are not checked, thus can lead to funny results or uncatched divisions by zero.

If you want to use this method directly, you should take care that the elements are unique. Also you should catch the situation where one of the arrayrefs is empty (similarity is 0), or both are empty (similarity is 1).

L<http://github.com/wollmers/Set-Similarity>