| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Tree-BK | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2014 by Nathan Glenn. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Tree::BK; | ||||||||||||||||||||||||||||
| 10 | 2 | $Tree::BK::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 11 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Text::Levenshtein::XS qw(distance); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # ABSTRACT: Structure for efficient fuzzy matching | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | my ($class, $metric) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | if(defined $metric){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | if((ref $metric) ne 'CODE'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 1 | croak 'argument to new() should be ' . | |||||||||||||||||||||||||||
| 24 | 'a code reference implementing a metric'; | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | }else{ | ||||||||||||||||||||||||||||
| 27 | 2 | $metric = \&Text::Levenshtein::XS::distance; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 3 | my $tree = bless { | |||||||||||||||||||||||||||
| 30 | metric => $metric, | ||||||||||||||||||||||||||||
| 31 | root => undef, | ||||||||||||||||||||||||||||
| 32 | size => 0, | ||||||||||||||||||||||||||||
| 33 | }, $class; | ||||||||||||||||||||||||||||
| 34 | 3 | return $tree; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub insert { | ||||||||||||||||||||||||||||
| 39 | 18 | ▶ | my ($self, $object) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 18 | ▶ | if(!defined $self->{root}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 3 | $self->{root} = { object=>$object }; | |||||||||||||||||||||||||||
| 42 | 3 | $self->{size}++; | |||||||||||||||||||||||||||
| 43 | 3 | return $object; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 15 | my $current = $self->{root}; | |||||||||||||||||||||||||||
| 47 | 15 | my $dist = $self->{metric}->($current->{object}, $object); | |||||||||||||||||||||||||||
| 48 | 15 | ▶ | while(exists $current->{$dist}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | # object was already in the tree | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | if($dist == 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 2 | $current = $current->{$dist}; | |||||||||||||||||||||||||||
| 54 | 2 | $dist = $self->{metric}->($current->{object}, $object); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | # prevent adding the root node multiple times | ||||||||||||||||||||||||||||
| 57 | 15 | ▶ | if($dist == 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 2 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 13 | $current->{$dist} = {object => $object}; | |||||||||||||||||||||||||||
| 61 | 13 | $self->{size}++; | |||||||||||||||||||||||||||
| 62 | 13 | return $object; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub insert_all { | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | my ($self, @objects) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert_all: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | if(@objects < 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | croak 'Must pass at least one object to insert_all method'; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 3 | my $size_before = $self->size; | |||||||||||||||||||||||||||
| 71 | 3 | $self->insert($_) for @objects; | |||||||||||||||||||||||||||
| 72 | 3 | return $self->size - $size_before; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub find { | ||||||||||||||||||||||||||||
| 76 | 2 | ▶ | my ($self, $target, $threshold) = @_; | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 2 | my @return; | |||||||||||||||||||||||||||
| 78 | 2 | $self->_find($self->{root}, \@return, $target, $threshold); | |||||||||||||||||||||||||||
| 79 | 2 | return \@return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub _find { | ||||||||||||||||||||||||||||
| 83 | 8 | ▶ | my ($self, $node, $current_list, $target, $threshold) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 84 | 8 | my $distance = $self->{metric}->($node->{object}, $target); | |||||||||||||||||||||||||||
| 85 | 8 | my $min_dist = $distance - $threshold; | |||||||||||||||||||||||||||
| 86 | 8 | my $max_dist = $distance + $threshold; | |||||||||||||||||||||||||||
| 87 | 8 | ▶ | if($distance <= $threshold){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 7 | push @$current_list, $node->{object}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | # recursively search the children where nodes with the threshold | ||||||||||||||||||||||||||||
| 91 | # distance might reside | ||||||||||||||||||||||||||||
| 92 | 8 | for(keys %$node){ | |||||||||||||||||||||||||||
| 93 | 18 | ▶ | next if $_ eq 'object'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 10 | ▶ | next unless $_ >= $min_dist && $_ <= $max_dist; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_ >= $min_dist and $_ <= $max_dist
MC/DC50%1/2
$_ >= $min_dist and $_ <= $max_dist
$_ >= $min_dist
$_ <= $max_dist
Truth table66%2/3
A and B
A= $_ >= $min_distB= $_ <= $max_dist
| |||||||||||||||||||||||||||||
| 95 | 6 | $self->_find($node->{$_}, $current_list, $target, $threshold); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub size { | ||||||||||||||||||||||||||||
| 100 | 9 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 9 | return $self->{size}; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||