lib/Convert/Pheno/DB/Similarity.pm

filter: Statementstmt 100% 58 / 58 Branchbran 83.3% 15 / 18 Conditioncond 52.9% 9 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 79.1% 95 / 120 CC 18 SCAR 21.4
CC
18
Cov
88%
compute_single_token_spelling_similarity
15.1
compute_token_similarity
3.1
compute_normalized_levenshtein
3.0
CRAP 8.5
« lib/Convert/Pheno/DB/SQLite.pm ↑ Summary lib/Convert/Pheno/PXF/ToBFF/Individuals.pm »
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
l!l&&r!l&&!r
1460
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
ABresult
1X1
011
000
15 20 my ( $score, %scores ) = $ts->getSimilarityStrings( $query, $candidate );
16 20 ▶ return $scores{$method} // 0;
Condition50%1/2
$scores{$method} // 0
l!l
200
MC/DC0%0/1
$scores{$method} // 0
$scores{$method}
Truth table50%1/2
A
A= $scores{$method}
Aresult
11
00
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
BranchTrueFalseTotal
length $query > length $candidate ? : 2 20 22
23 length($query) > length($candidate) ? length($query) : length($candidate);
24 22 ▶ return $max_len ? 1 - ( $d / $max_len ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$max_len ? : 21 1 22
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
BranchTrueFalseTotal
unless defined $query and defined $candidate 0 10 10
Condition33%1/3
defined $query and defined $candidate
!ll&&!rl&&r
0010
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
ABresult
0X0
100
111
30 10 ▶ return unless $method eq 'cosine' || $method eq 'dice';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "cosine" or $method eq "dice" 0 10 10
Condition33%1/3
$method eq "cosine" or $method eq "dice"
l!l&&r!l&&!r
1000
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless @query_tokens and @query_tokens == @candidate_tokens 4 6 10
Condition66%2/3
@query_tokens and @query_tokens == @candidate_tokens
!ll&&!rl&&r
046
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $query_token eq $remaining_candidates[$index] 29 16 45
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
BranchTrueFalseTotal
unless @unmatched_query == 1 and @remaining_candidates == 1 1 5 6
Condition66%2/3
@unmatched_query == 1 and @remaining_candidates == 1
!ll&&!rl&&r
105
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $spelling_similarity < "0.8" 2 3 5
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
BranchTrueFalseTotal
$method eq 'cosine' ? : 3 0 3
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;
« lib/Convert/Pheno/DB/SQLite.pm ↑ Summary lib/Convert/Pheno/PXF/ToBFF/Individuals.pm »