| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | shift @copy if @copy >= $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 13 | ▶ | if (@copy and $count = $biz_stop->{last}{$copy[-1]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@copy and $count = $String::Normal::Type::Business::biz_stop->{"last"}{$copy[-1]}
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]}
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | pop @copy if @copy >= $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # reverting back if overnormalization occurs | ||||||||||||||||||||||||||||
| 34 | 13 | ▶ | @words = @copy if @copy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'}{$_} || ''
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'}{$_}
| |||||||||||||||||||||||||||||
| 41 | 26 | ▶ | (length $count and @words >= $count) ? () : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $count and @words >= $count
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 5 | ▶ | next if $i + 1 > $#$tokens; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 4 | my $end = _walk_tree( $i + 1, $tokens, $biz_compress->{$token} ); | |||||||||||||||||||||||||||
| 93 | 4 | ▶ | if ($end) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | 4 | ▶ | if (ref $t eq 'HASH' and !%$t) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $t eq "HASH" and not %$t
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||