Info/BASE.pm

filter: Statementstmt 85.0% 97 / 114 Branchbran 91.6% 11 / 12 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.8% 16 / 18 Podpod 100% 1 / 1 totaltotal 83.5% 132 / 158 CC 16 SCAR 12.1
CC
16
Cov
84%
_build_sentences
6.0
_build_avg_sentence_length
6.0
_build_avg_word_length
6.0
CRAP 3.4
« Info/Readability.pm ↑ Summary Info/Utils.pm »
1 package Text::Info::BASE;
2 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 with 'Text::Info::Utils';
6  
7 6 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 has 'text' => (
11 isa => 'Str',
12 is => 'rw',
13 default => '',
14 );
15  
16 6 has 'tld' => (
17 isa => 'Maybe[Str]',
18 is => 'rw',
19 default => '',
20 );
21  
22 has 'language' => (
23 isa => 'Str',
24 is => 'rw',
25 lazy => 1,
26 5 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
27 5 my $self = shift;
28  
29 5 my @lang = $self->CLD->identify( $self->text, tld => $self->tld );
30 5 my $lang = $lang[1];
31  
32 5 ▶ if ( $lang eq 'nb' || $lang eq 'nn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq "nb" or $lang eq "nn") 1 4 5
Condition66%2/3
$lang eq "nb" or $lang eq "nn"
l!l&&r!l&&!r
104
MC/DC50%1/2
$lang eq "nb" or $lang eq "nn"
$lang eq "nb" $lang eq "nn"
Truth table66%2/3
A or B
A= $lang eq "nb"B= $lang eq "nn"
ABresult
1X1
011
000
33 1 $lang = 'no';
34 }
35  
36 5 return $lang;
37 },
38 6 );
39  
40 81 ▶around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
41 81 my $orig = shift;
42 81 my $self = shift;
43  
44 81 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 23 58 81
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
58023
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
45 23 return $self->$orig( text => $_[0] );
46 }
47 else {
48 58 return $self->$orig( @_ );
49 }
50 6 };
51  
52 6 has 'sentences' => ( isa => 'ArrayRef[Text::Info::Sentence]', is => 'ro', lazy_build => 1 );
53  
54 25 ▶sub _build_sentences {
Subroutine _build_sentences: called CC=6 SCAR=17.9
55 25 my $self = shift;
56  
57 25 my $marker = '</marker/>';
58 25 my $text = $self->text;
59 25 my $separators = '.?!:;';
60  
61 # Mark separators with a marker.
62 25 $text =~ s/([\Q$separators\E]+\s*)/$1$marker/sg;
63  
64 # Markers immediately prefixed by a lowercase + uppercased characters, and again followed
65 # by a marker, a space and "normal" stuff should be removed.
66 # Expample: If you want cake, open door A. </marker/>If you want a car, open door C.</marker/>
67 # die $text;
68 # $text =~ s/([[:lower:]]\s+[[:upper:]]\.\s+)<\/marker\/>([[:upper:]].*?)\Q$marker\E/$1$2/sg;
69  
70 # Abbreviations.
71 25 foreach ( qw( Prof Ph Dr Mr Mrs Ms Hr St ) ) {
72 200 $text =~ s/($_\.\s+)\Q$marker\E/$1/sg;
73 }
74  
75 # U.N., U.S.A.
76 25 $text =~ s/([[:upper:]]{1}\.)\Q$marker\E/$1/sg;
77  
78 # Clockwork.
79 25 $text =~ s/(kl\.\s+)\Q$marker\E(\d+.)(\d+.)\Q$marker\E(\d+)/$1$2$3$4/sg;
80 25 $text =~ s/(kl\.\s+)\Q$marker\E(\d+.)\Q$marker\E(\d+)/$1$2$3/sg;
81 25 $text =~ s/(\d+.)\Q$marker\E(\d+)/$1$2/sg;
82 25 $text =~ s/(\d+.)\Q$marker\E(\d+.)\Q$marker\E(\d+)/$1$2$3/sg;
83 25 $text =~ s/(\d+\s+[ap]\.)\Q$marker\E(m\.\s*)\Q$marker\E/$1$2/sg;
84 25 $text =~ s/(\d+\s+[ap]m\.\s+)\Q$marker\E/$1/sg;
85  
86 # Remove marker if it looks like we're dealing with a date abbrev., like "Nov. 29" etc.
87 25 my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
88  
89 25 foreach my $month ( @months ) {
90 300 $text =~ s/($month\.\s+)\Q$marker\E(\d+)/$1$2/sg;
91 }
92  
93 # Markers immediately followed by a (possible space and) lowercased character should be removed.
94 # This is useful for TLDs/domain names like "cnn.com".
95 25 $text =~ s/\Q$marker\E\s*([[:lower:]])/$1/sg;
96  
97 # Markers immediately prefixed by a space + single uppercased characters should be removed.
98 # This is fine for f.ex. names like "Tore O. Aursand".
99 25 $text =~ s/(\s+[[:upper:]]\.\s+)\Q$marker\E/$1/sg;
100  
101 # Build sentences.
102 25 my @sentences = ();
103  
104 25 foreach my $sentence ( split(/\Q$marker\E/, $text) ) {
105 48 ▶ 1 while ( $sentence =~ s/[\Q$separators\E\s]$// );
Branch: 1 branch
BranchTrueFalseTotal
$sentence =~ s/[\Q$separators\E\s]$// and do { '???' } 77 48 125
106  
107 48 $sentence = $self->squish( $sentence );
108 48 $sentence =~ s/^\-+\s*//sg;
109  
110 48 ▶ if ( length $sentence ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sentence) 48 0 48
111 48 push( @sentences, Text::Info::Sentence->new(text => $sentence, tld => $self->tld) );
112 }
113 }
114  
115 # Return
116 25 return \@sentences;
117 }
118  
119 6 has 'sentence_count' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
120  
121 23 ▶sub _build_sentence_count {
Subroutine _build_sentence_count: called CC=1 SCAR=0.0
122 23 my $self = shift;
123  
124 23 return scalar( @{$self->sentences} );
125 }
126  
127 6 has 'avg_sentence_length' => ( isa => 'Num', is => 'ro', lazy_build => 1 );
128  
129 0 ▶sub _build_avg_sentence_length {
Subroutine _build_avg_sentence_length: not called CC=2 SCAR=17.9
130 0 my $self = shift;
131  
132 0 my $total_length = 0;
133  
134 0 foreach my $sentence ( @{$self->sentences} ) {
135 0 $total_length += length( $sentence->text );
136 }
137  
138 0 return $total_length / $self->sentence_count;
139 }
140  
141 6 has 'words' => ( isa => 'ArrayRef[Str]', is => 'ro', lazy_build => 1 );
142  
143 6 ▶sub _build_words {
Subroutine _build_words: called CC=1 SCAR=0.0
144 6 my $self = shift;
145  
146 6 return $self->text2words( $self->text );
147 }
148  
149 =item word_count
150  
151 =cut
152  
153 6 has 'word_count' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
154  
155 2 ▶sub _build_word_count {
Subroutine _build_word_count: called CC=1 SCAR=0.0
156 2 my $self = shift;
157  
158 2 return scalar( @{$self->words} );
159 }
160  
161 =item avg_word_length()
162  
163 Returns the average length of the words in the text.
164  
165 =cut
166  
167 6 has 'avg_word_length' => ( isa => 'Num', is => 'ro', lazy_build => 1 );
168  
169 0 ▶sub _build_avg_word_length {
Subroutine _build_avg_word_length: not called CC=2 SCAR=17.9
170 0 my $self = shift;
171  
172 0 my $total_length = 0;
173  
174 0 foreach my $word ( @{$self->words} ) {
175 0 $total_length += length( $word );
176 }
177  
178 0 return $total_length / $self->word_count;
179 }
180  
181 =item syllable_count
182  
183 =cut
184  
185 6 has 'syllable_count' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
186  
187 2 ▶sub _build_syllable_count {
Subroutine _build_syllable_count: called CC=2 SCAR=10.9
188 2 my $self = shift;
189  
190 2 my $class_name = 'Lingua::' . uc( $self->language ) . '::Syllable';
191 2 autoload $class_name;
192  
193 0 my $count = 0;
194  
195 0 foreach my $word ( @{$self->words} ) {
196 0 $count += syllable( Unicode::Normalize::NFD($word) );
197 }
198  
199 0 return $count;
200 }
201  
202 =item ngrams( $size )
203  
204 =cut
205  
206 13 ▶sub ngrams {
Subroutine ngrams: called CC=4 SCAR=13.9
Pod documented
207 13 my $self = shift;
208 13 ▶ my $size = shift || 2;
Condition50%1/2
shift() || 2
l!l
130
MC/DC0%0/1
shift() || 2
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
209  
210 13 my @ngrams = ();
211 13 my @words = @{ $self->words };
212  
213 13 ▶ for ( my $word_idx = 0; $word_idx < @words; $word_idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_idx < @words) 75 13 88
214 75 my @w = @words[ $word_idx .. $word_idx + ($size - 1) ];
215 75 ▶ if ( defined $w[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w[-1]) 56 19 75
216 56 push( @ngrams, join(' ', @w) );
217 }
218 }
219  
220 13 return \@ngrams;
221 }
222  
223 =item unigrams
224  
225 =cut
226  
227 6 has 'unigrams' => ( isa => 'ArrayRef[Str]', is => 'ro', lazy_build => 1 );
228  
229 1 ▶sub _build_unigrams {
Subroutine _build_unigrams: called CC=1 SCAR=0.0
230 1 my $self = shift;
231  
232 1 return $self->ngrams( 1 );
233 }
234  
235 =item bigrams
236  
237 =cut
238  
239 6 has 'bigrams' => ( isa => 'ArrayRef[Str]', is => 'ro', lazy_build => 1 );
240  
241 1 ▶sub _build_bigrams {
Subroutine _build_bigrams: called CC=1 SCAR=0.0
242 1 my $self = shift;
243  
244 1 return $self->ngrams( 2 );
245 }
246  
247 =item trigrams
248  
249 =cut
250  
251 6 has 'trigrams' => ( isa => 'ArrayRef[Str]', is => 'ro', lazy_build => 1 );
252  
253 2 ▶sub _build_trigrams {
Subroutine _build_trigrams: called CC=1 SCAR=0.0
254 2 my $self = shift;
255  
256 2 return $self->ngrams( 3 );
257 }
258  
259 =item quadgrams
260  
261 =cut
262  
263 6 has 'quadgrams' => ( isa => 'ArrayRef[Str]', is => 'ro', lazy_build => 1 );
264  
265 1 ▶sub _build_quadgrams {
Subroutine _build_quadgrams: called CC=1 SCAR=0.0
266 1 my $self = shift;
267  
268 1 return $self->ngrams( 4 );
269 }
270  
271 6 __PACKAGE__->meta->make_immutable;
272  
273 6 1;
« Info/Readability.pm ↑ Summary Info/Utils.pm »