| 1 | package Lingua::EN::SimilarNames::Levenshtein; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use MooseX::Declare; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Text::LevenshteinXS qw(distance); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Math::Combinatorics; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 Name | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Lingua::EN::SimilarNames::Levenshtein - Compare people first and last names. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 Synopsis | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $people = [ | ||||||||||||||||||||||||||||
| 19 | [ 'John', 'Wayne' ], | ||||||||||||||||||||||||||||
| 20 | [ 'Sundance', 'Kid' ], | ||||||||||||||||||||||||||||
| 21 | [ 'Jose', 'Wales' ], | ||||||||||||||||||||||||||||
| 22 | [ 'John', 'Wall' ], | ||||||||||||||||||||||||||||
| 23 | ]; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my @people_objects = map { | ||||||||||||||||||||||||||||
| 26 | Person->new( | ||||||||||||||||||||||||||||
| 27 | first_name => $_->[0], | ||||||||||||||||||||||||||||
| 28 | last_name => $_->[1], | ||||||||||||||||||||||||||||
| 29 | ) | ||||||||||||||||||||||||||||
| 30 | } @{$people}; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Build list of name pairs within 5 character edits of each other | ||||||||||||||||||||||||||||
| 33 | my $similar_people = SimilarNames->new( | ||||||||||||||||||||||||||||
| 34 | list_of_people => \@people_objects, | ||||||||||||||||||||||||||||
| 35 | maximum_distance => 5 | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Get the people name pairs as an ArrayRef[ArrayRef[ArrayRef[Str]]] | ||||||||||||||||||||||||||||
| 39 | print Dumper $similar_people->list_of_similar_name_pairs; | ||||||||||||||||||||||||||||
| 40 | # which results in: | ||||||||||||||||||||||||||||
| 41 | [ | ||||||||||||||||||||||||||||
| 42 | [ [ "Jose", "Wales" ], [ "John", "Wall" ] ], | ||||||||||||||||||||||||||||
| 43 | [ [ "Jose", "Wales" ], [ "John", "Wayne" ] ], | ||||||||||||||||||||||||||||
| 44 | [ [ "John", "Wall" ], [ "John", "Wayne" ] ] | ||||||||||||||||||||||||||||
| 45 | ] | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 Description | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Given a list of people objects, find the people whose names are within a | ||||||||||||||||||||||||||||
| 50 | specified edit distance. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 Classes | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head2 Person | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | This class defines people objects with first and last name attributes. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | class Person { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 63 | 1 | has 'first_name' => (isa => 'Str', is => 'ro', default => ''); | |||||||||||||||||||||||||||
| 64 | 1 | has 'last_name' => (isa => 'Str', is => 'ro', default => ''); | |||||||||||||||||||||||||||
| 65 | 1 | has 'full_name' => ( | |||||||||||||||||||||||||||
| 66 | isa => 'Str', | ||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||
| 68 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | method say_name() { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 72 | 0 | say $self->full_name; | |||||||||||||||||||||||||||
| 73 | 1 | } | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | method _build_full_name { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 76 | 0 | return $self->first_name . ' ' . $self->last_name; | |||||||||||||||||||||||||||
| 77 | 1 | } | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 CompareTwoNames | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | This class defines comparator objects. Given two Person objects, | ||||||||||||||||||||||||||||
| 83 | it computes the edit distance between their names. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | class CompareTwoNames { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 1 | has 'one_person' => (isa => 'Person', is => 'rw'); | |||||||||||||||||||||||||||
| 89 | 1 | has 'another_person' => (isa => 'Person', is => 'rw'); | |||||||||||||||||||||||||||
| 90 | 1 | has 'distance_between' => ( | |||||||||||||||||||||||||||
| 91 | isa => 'Int', | ||||||||||||||||||||||||||||
| 92 | is => 'ro', | ||||||||||||||||||||||||||||
| 93 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | method _build_distance_between() { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_build_distance_between: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 7 | return Text::LevenshteinXS::distance($self->one_person->first_name, | |||||||||||||||||||||||||||
| 98 | $self->another_person->first_name) + | ||||||||||||||||||||||||||||
| 99 | Text::LevenshteinXS::distance($self->one_person->last_name, | ||||||||||||||||||||||||||||
| 100 | $self->another_person->last_name); | ||||||||||||||||||||||||||||
| 101 | 1 | }; | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 SimilarNames | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | This class takes a list of Person objects and uses CompareTwoNames to | ||||||||||||||||||||||||||||
| 107 | generate a list of people with similar names based on an edit distance range. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | One can get at the list of Person object pairs with similar name via the | ||||||||||||||||||||||||||||
| 110 | C<list_of_people_with_similar_names> attribute. Alternatively, one can | ||||||||||||||||||||||||||||
| 111 | get at list of the names pairs themselves (no Person object) via the | ||||||||||||||||||||||||||||
| 112 | C<list_of_similar_name_pairs> attribute. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | class SimilarNames { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 1 | has 'list_of_people' => ( | |||||||||||||||||||||||||||
| 118 | isa => 'ArrayRef[Person]', | ||||||||||||||||||||||||||||
| 119 | is => 'ro', | ||||||||||||||||||||||||||||
| 120 | lazy_build => 1 | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | 1 | has 'minimum_distance' => (isa => 'Int', is => 'rw', default => 1); | |||||||||||||||||||||||||||
| 123 | 1 | has 'maximum_distance' => (isa => 'Int', is => 'rw', default => 3); | |||||||||||||||||||||||||||
| 124 | 1 | has 'list_of_people_with_similar_names' => ( | |||||||||||||||||||||||||||
| 125 | isa => 'ArrayRef[ArrayRef[Person]]', | ||||||||||||||||||||||||||||
| 126 | is => 'ro', | ||||||||||||||||||||||||||||
| 127 | lazy_build => 1 | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | 1 | has 'list_of_similar_name_pairs' => ( | |||||||||||||||||||||||||||
| 130 | isa => 'ArrayRef[ArrayRef[ArrayRef[Str]]]', | ||||||||||||||||||||||||||||
| 131 | is => 'ro', | ||||||||||||||||||||||||||||
| 132 | lazy_build => 1 | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | ▶ | method _build_list_of_people_with_similar_names() { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 136 | 1 | my $people_tuples = Math::Combinatorics->new( | |||||||||||||||||||||||||||
| 137 | count => 2, # This could be abstracted | ||||||||||||||||||||||||||||
| 138 | data => $self->list_of_people, | ||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||
| 140 | 1 | my @list_of_people_with_similar_names; | |||||||||||||||||||||||||||
| 141 | 1 | ▶ | while (my ($first_person, $second_person) = $people_tuples->next_combination()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 6 | my $name_comparison = CompareTwoNames->new( | |||||||||||||||||||||||||||
| 143 | one_person => $first_person, | ||||||||||||||||||||||||||||
| 144 | another_person => $second_person, | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | 6 | my $distance_between_names = $name_comparison->distance_between(); | |||||||||||||||||||||||||||
| 147 | 6 | ▶ | if ( ($distance_between_names >= $self->minimum_distance) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$distance_between_names >= $self->minimum_distance and $distance_between_names <= $self->maximum_distance
MC/DC50%1/2
$distance_between_names >= $self->minimum_distance and $distance_between_names <= $self->maximum_distance
$distance_between_names >= $self->minimum_distance
$distance_between_names <= $self->maximum_distance
Truth table66%2/3
A and B
A= $distance_between_names >= $self->minimum_distanceB= $distance_between_names <= $self->maximum_distance
| |||||||||||||||||||||||||||||
| 148 | && ($distance_between_names <= $self->maximum_distance)) | ||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 3 | push @list_of_people_with_similar_names, [ $first_person, $second_person ]; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | return \@list_of_people_with_similar_names | ||||||||||||||||||||||||||||
| 155 | 1 | }; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | ▶ | method _build_list_of_similar_name_pairs() { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_build_list_of_similar_name_pairs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 158 | 1 | my @list_of_similar_name_pairs; | |||||||||||||||||||||||||||
| 159 | 1 | foreach my $pair_of_people (@{ $self->list_of_people_with_similar_names }) { | |||||||||||||||||||||||||||
| 160 | 3 | push @list_of_similar_name_pairs, | |||||||||||||||||||||||||||
| 161 | [ | ||||||||||||||||||||||||||||
| 162 | [ $pair_of_people->[0]->first_name, $pair_of_people->[0]->last_name ], | ||||||||||||||||||||||||||||
| 163 | [ $pair_of_people->[1]->first_name, $pair_of_people->[1]->last_name ] | ||||||||||||||||||||||||||||
| 164 | ]; | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | return \@list_of_similar_name_pairs | ||||||||||||||||||||||||||||
| 167 | 1 | }; | |||||||||||||||||||||||||||
| 168 | 1 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||