Normal/Type/Business.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 68.7% 22 / 32 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 84.4% 114 / 135 CC 24 SCAR 20.9
CC
24
Cov
85%
transform
16.6
_tokenize_value
5.2
_mark_pairs
5.0
CRAP 8.1
« Normal/Type/Address.pm ↑ Summary Normal.pm »
1 package String::Normal::Type::Business;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use String::Normal::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use String::Normal::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Lingua::Stem;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 our $STEM;
9 9 our $biz_stop;
10 9 our $biz_compress;
11  
12 13 ▶sub transform {
Subroutine transform: called CC=12 SCAR=28.1
Pod documented
13 13 my ($self,$value) = @_;
14  
15 # tokenize and stem
16 13 my (@digits,@words);
17 13 _tokenize_value( $value, \@digits, \@words );
18 13 $STEM->stem_in_place( @words );
19  
20 # Remove "special" beginning and/or ending stopwords, if such words are present
21 # and enough tokens are in place to remove them safely.
22 13 ▶ if (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 13 0 13
23 # make a copy of @words and whittle it down
24 13 my @copy = @words;
25 13 my $count;
26 13 ▶ if ($count = $biz_stop->{first}{$copy[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count = $String::Normal::Type::Business::biz_stop->{"first"}{$copy[0]}) 0 13 13
27 0 ▶ shift @copy if @copy >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if @copy >= $count 0 0 0
28 }
29 13 ▶ if (@copy and $count = $biz_stop->{last}{$copy[-1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@copy and $count = $String::Normal::Type::Business::biz_stop->{"last"}{$copy[-1]}) 0 13 13
Condition33%1/3
@copy and $count = $String::Normal::Type::Business::biz_stop->{"last"}{$copy[-1]}
!ll&&!rl&&r
0130
MC/DC0%0/2
@copy and $count = $String::Normal::Type::Business::biz_stop->{"last"}{$copy[-1]}
@copy $count = $String::Normal::Type::Business::biz_stop->{"last"}{$copy[-1]}
Truth table33%1/3
A and B
A= @copyB= $count = $String::Normal::Type::Business::biz_stop->{"last"}{$copy[-1]}
ABresult
0X0
100
111
30 0 ▶ pop @copy if @copy >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if @copy >= $count 0 0 0
31 }
32  
33 # reverting back if overnormalization occurs
34 13 ▶ @words = @copy if @copy;
Branch: 1 branch
BranchTrueFalseTotal
if @copy 13 0 13
35 }
36  
37 # Remove all middle stop words that are safe to remove, based on the number of
38 # tokens, of course.
39 my @filtered = map {
40 13 ▶ my $count = $biz_stop->{middle}{$_} || '';
Condition100%2/2
$String::Normal::Type::Business::biz_stop->{'middle'}{$_} || ''
l!l
719
MC/DC100%1/1
$String::Normal::Type::Business::biz_stop->{'middle'}{$_} || ''
$String::Normal::Type::Business::biz_stop->{'middle'}{$_}
Truth table100%2/2
A
A= $String::Normal::Type::Business::biz_stop->{'middle'}{$_}
Aresult
11
00
41 26 ▶ (length $count and @words >= $count) ? () : $_;
Branch: 1 branch
BranchTrueFalseTotal
length $count && @words >= $count ? : 7 19 26
Condition66%2/3
length $count and @words >= $count
!ll&&!rl&&r
1907
MC/DC50%1/2
length $count and @words >= $count
length $count @words >= $count
Truth table66%2/3
A and B
A= length $countB= @words >= $count
ABresult
0X0
100
111
42 } @words;
43  
44 # If we filtered all words out, "revert" to the full array of stemmed tokens.
45 13 ▶ @filtered = @words unless @filtered;
Branch: 1 branch
BranchTrueFalseTotal
unless @filtered 1 12 13
46  
47 # The canon name is the sorted filtered stemmed words plus the original digits.
48 13 return join ' ', sort @digits, @filtered;
49 }
50  
51 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 3 my $self = shift;
53 3 $STEM = Lingua::Stem->new;
54 3 $STEM->add_exceptions( String::Normal::Config::BusinessStem::_data( @_ ) );
55 3 $biz_stop = String::Normal::Config::BusinessStop::_data( @_ );
56 3 $biz_compress = String::Normal::Config::BusinessCompress::_data( @_ );
57 3 return bless {@_}, $self;
58 }
59  
60 13 ▶sub _tokenize_value {
Subroutine _tokenize_value: called CC=5 SCAR=16.4
61 13 my ($value,$digits,$words) = @_;
62  
63 13 $value = String::Normal::Type::_scrub_value( $value );
64  
65 # split tokens on more than just whitespace:
66 # split digits from words but keep things like 3D and 1st combined,
67 # also split things like abcd#efgh but keep pound signs for #2 and # 1 and #
68 # prevent the empty string from finding its way into the token list as well
69 13 ▶ my @tokens = map { map length $_ ? $_ : (), split /##+|\s+|#+\b|\b#+/, $_ } $value =~ /(?:\d+\w{1,2}\b|\d+|\D+)/g;
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 30 0 30
70  
71 # walk each token thru the tree and create markers
72 13 my @pairs = _mark_pairs( \@tokens );
73 13 ▶ _compress_list( \@tokens, \@pairs ) if @pairs;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs 3 10 13
74  
75 # separate out tokens that contain digits (snowball stemmer will scrub all digits)
76 13 for (@tokens) {
77 26 ▶ if (/\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\d/) { } 0 26 26
78 0 push @$digits, $_;
79 } else {
80 26 push @$words, $_;
81 }
82 }
83 }
84  
85 13 ▶sub _mark_pairs {
Subroutine _mark_pairs: called CC=5 SCAR=16.1
86 13 my $tokens = shift;
87 13 my @pairs = ();
88 13 for my $i (0 .. $#$tokens) {
89 30 my $token = $tokens->[$i];
90 30 ▶ next unless exists $biz_compress->{$token};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $String::Normal::Type::Business::biz_compress->{$token} 25 5 30
91 5 ▶ next if $i + 1 > $#$tokens;
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 > $tokens->$#* 1 4 5
92 4 my $end = _walk_tree( $i + 1, $tokens, $biz_compress->{$token} );
93 4 ▶ if ($end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end) 3 1 4
94 3 push @pairs, [$i,$end];
95 3 $i = $end;
96 }
97 }
98 13 return @pairs;
99 }
100  
101  
102 5 ▶sub _walk_tree {
Subroutine _walk_tree: called CC=4 SCAR=13.9
103 5 my ($i, $list, $tree) = @_;
104  
105 5 ▶ if (my $t = $tree->{$list->[$i]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $$tree{$list->[$i]}) 4 1 5
106 4 ▶ if (ref $t eq 'HASH' and !%$t) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t eq 'HASH' and not %$t) { } 3 1 4
Condition66%2/3
ref $t eq "HASH" and not %$t
!ll&&!rl&&r
013
MC/DC50%1/2
ref $t eq "HASH" and not %$t
ref $t eq "HASH" not %$t
Truth table66%2/3
A and not(B)
A= ref $t eq "HASH"B= not %$t
ABresult
0X0
100
111
107 3 return $i;
108 } else {
109 1 _walk_tree( $i + 1, $list, $t );
110 }
111 }
112 }
113  
114 3 ▶sub _compress_list {
Subroutine _compress_list: called CC=2 SCAR=6.9
115 3 my ($list,$pairs) = @_;
116 3 for my $pair (reverse @$pairs) {
117 3 my ($s,$e) = @$pair;
118 3 splice @$list, $s, $e - $s + 1, join '', @$list[$s .. $e];
119 }
120 }
121  
122 9 1;
123  
124 __END__
« Normal/Type/Address.pm ↑ Summary Normal.pm »