| 1 | package Convert::Pheno::DB::Similarity; | ||||||||||||||||||||||||||||
| 2 | 144 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 144 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 144 | ▶ | use Text::Similarity::Overlaps; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 144 | ▶ | use Text::Levenshtein::XS qw(distance); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 144 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 144 | our @EXPORT_OK = | |||||||||||||||||||||||||||
| 8 | qw(combine_similarity_scores composite_similarity compute_token_similarity compute_normalized_levenshtein compute_single_token_spelling_similarity); | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 144 | ▶ | use constant MIN_SPELLING_TOKEN_SIMILARITY => 0.8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 20 | ▶ | sub compute_token_similarity { | ||||||||||||||||||||||||||
|
Subroutine
compute_token_similarity: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 20 | my ( $query, $candidate, $method, $ts ) = @_; | |||||||||||||||||||||||||||
| 14 | 20 | ▶ | $ts ||= Text::Similarity::Overlaps->new(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$ts ||= "Text::Similarity::Overlaps"->new
MC/DC0%0/2
$ts ||= "Text::Similarity::Overlaps"->new
$ts
"Text::Similarity::Overlaps"->new
Truth table66%2/3
A ||= B
A= $tsB= "Text::Similarity::Overlaps"->new
| |||||||||||||||||||||||||||||
| 15 | 20 | my ( $score, %scores ) = $ts->getSimilarityStrings( $query, $candidate ); | |||||||||||||||||||||||||||
| 16 | 20 | ▶ | return $scores{$method} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$scores{$method} // 0
MC/DC0%0/1
$scores{$method} // 0
$scores{$method}
Truth table50%1/2
A
A= $scores{$method}
| |||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | ▶ | sub compute_normalized_levenshtein { | ||||||||||||||||||||||||||
|
Subroutine
compute_normalized_levenshtein: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 22 | my ( $query, $candidate ) = @_; | |||||||||||||||||||||||||||
| 21 | 22 | my $d = distance( $query, $candidate ); | |||||||||||||||||||||||||||
| 22 | 22 | ▶ | my $max_len = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | length($query) > length($candidate) ? length($query) : length($candidate); | ||||||||||||||||||||||||||||
| 24 | 22 | ▶ | return $max_len ? 1 - ( $d / $max_len ) : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 10 | ▶ | sub compute_single_token_spelling_similarity { | ||||||||||||||||||||||||||
|
Subroutine
compute_single_token_spelling_similarity: called CC=14 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 10 | my ( $query, $candidate, $method ) = @_; | |||||||||||||||||||||||||||
| 29 | 10 | ▶ | return unless defined $query && defined $candidate; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $query and defined $candidate
MC/DC0%0/2
defined $query and defined $candidate
defined $query
defined $candidate
Truth table33%1/3
A and B
A= defined $queryB= defined $candidate
| |||||||||||||||||||||||||||||
| 30 | 10 | ▶ | return unless $method eq 'cosine' || $method eq 'dice'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$method eq "cosine" or $method eq "dice"
MC/DC0%0/2
$method eq "cosine" or $method eq "dice"
$method eq "cosine"
$method eq "dice"
Truth table33%1/3
A or B
A= $method eq "cosine"B= $method eq "dice"
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 10 | my @query_tokens = grep { length } | |||||||||||||||||||||||||||
| 33 | split /[^\p{L}\p{N}]+/u, lc $query; | ||||||||||||||||||||||||||||
| 34 | 10 | my @candidate_tokens = grep { length } | |||||||||||||||||||||||||||
| 35 | split /[^\p{L}\p{N}]+/u, lc $candidate; | ||||||||||||||||||||||||||||
| 36 | 10 | ▶ | return unless @query_tokens && @query_tokens == @candidate_tokens; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@query_tokens and @query_tokens == @candidate_tokens
MC/DC50%1/2
@query_tokens and @query_tokens == @candidate_tokens
@query_tokens
@query_tokens == @candidate_tokens
Truth table66%2/3
A and B
A= @query_tokensB= @query_tokens == @candidate_tokens
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 6 | my @remaining_candidates = @candidate_tokens; | |||||||||||||||||||||||||||
| 39 | 6 | my @unmatched_query; | |||||||||||||||||||||||||||
| 40 | QUERY_TOKEN: | ||||||||||||||||||||||||||||
| 41 | 6 | for my $query_token (@query_tokens) { | |||||||||||||||||||||||||||
| 42 | 23 | for my $index ( 0 .. $#remaining_candidates ) { | |||||||||||||||||||||||||||
| 43 | 45 | ▶ | next unless $query_token eq $remaining_candidates[$index]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 16 | splice @remaining_candidates, $index, 1; | |||||||||||||||||||||||||||
| 45 | 16 | next QUERY_TOKEN; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 7 | push @unmatched_query, $query_token; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | return unless @unmatched_query == 1 && @remaining_candidates == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@unmatched_query == 1 and @remaining_candidates == 1
MC/DC50%1/2
@unmatched_query == 1 and @remaining_candidates == 1
@unmatched_query == 1
@remaining_candidates == 1
Truth table66%2/3
A and B
A= @unmatched_query == 1B= @remaining_candidates == 1
| |||||||||||||||||||||||||||||
| 51 | 5 | my $spelling_similarity = compute_normalized_levenshtein( | |||||||||||||||||||||||||||
| 52 | $unmatched_query[0], $remaining_candidates[0] | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 5 | ▶ | return if $spelling_similarity < MIN_SPELLING_TOKEN_SIMILARITY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | my $query_count = scalar @query_tokens; | |||||||||||||||||||||||||||
| 57 | 3 | my $candidate_count = scalar @candidate_tokens; | |||||||||||||||||||||||||||
| 58 | 3 | my $soft_overlap = $query_count - 1 + $spelling_similarity; | |||||||||||||||||||||||||||
| 59 | 3 | ▶ | my $adjusted_similarity = $method eq 'cosine' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | ? $soft_overlap / sqrt( $query_count * $candidate_count ) | ||||||||||||||||||||||||||||
| 61 | : ( 2 * $soft_overlap ) / ( $query_count + $candidate_count ); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | return { | ||||||||||||||||||||||||||||
| 64 | 3 | adjusted_similarity => $adjusted_similarity, | |||||||||||||||||||||||||||
| 65 | query_token => $unmatched_query[0], | ||||||||||||||||||||||||||||
| 66 | candidate_token => $remaining_candidates[0], | ||||||||||||||||||||||||||||
| 67 | spelling_token_similarity => $spelling_similarity, | ||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 15 | ▶ | sub combine_similarity_scores { | ||||||||||||||||||||||||||
|
Subroutine
combine_similarity_scores: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 15 | my ( $token_sim, $lev_sim, $token_weight, $lev_weight ) = @_; | |||||||||||||||||||||||||||
| 73 | 15 | return ( $token_weight * $token_sim ) + ( $lev_weight * $lev_sim ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub composite_similarity { | ||||||||||||||||||||||||||
|
Subroutine
composite_similarity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 3 | my ( $query, $candidate, $token_weight, $lev_weight, $method ) = @_; | |||||||||||||||||||||||||||
| 78 | 3 | my $token_sim = compute_token_similarity( $query, $candidate, $method ); | |||||||||||||||||||||||||||
| 79 | 3 | my $lev_sim = compute_normalized_levenshtein( $query, $candidate ); | |||||||||||||||||||||||||||
| 80 | 3 | return combine_similarity_scores( $token_sim, $lev_sim, $token_weight, $lev_weight ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 144 | 1; | |||||||||||||||||||||||||||