blib/lib/Text/Ngrams.pm

filter: Statementstmt 88.4% 299 / 338 Branchbran 79.8% 139 / 174 Conditioncond 76.4% 39 / 51 MC/DCmcdc 60.0% 18 / 30 Subroutinesub 95.4% 21 / 22 Podpod 100% 8 / 8 totaltotal 84.1% 524 / 623 CC 114 SCAR 27.4
CC
114
Cov
85%
to_string
25.6
_process_text
23.0
new
17.0
CRAP 15.5
↑ Summary blib/script/ngrams.pl »
1 # (c) 2003-2022 Vlado Keselj http://web.cs.dal.ca/~vlado
2 #
3 # Text::Ngrams - A Perl module for N-grams processing
4  
5 package Text::Ngrams;
6  
7 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 require Exporter;
9 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 @ISA = qw(Exporter);
12 15 %EXPORT_TAGS = ( 'all' => [ qw(new encode_S decode_S) ] );
13 15 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
14 15 @EXPORT = qw(new);
15 15 $VERSION = '2.007';
16  
17 15 ▶use vars qw($Version);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 $Version = $VERSION;
19  
20 15 ▶use vars @EXPORT_OK;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use vars qw(); # non-exported package globals go here
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 18 ▶sub new {
Subroutine new: called CC=14 SCAR=28.3
Pod documented
24 18 my $package = shift;
25 18 ▶ $package = ref($package) || $package;
Condition33%1/3
ref $package || $package
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table33%1/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
26 18 my $self = {};
27  
28 18 my (%params) = @_;
29  
30 $self->{windowsize} = exists($params{windowsize}) ?
31 18 ▶ $params{windowsize} : 3;
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'windowsize'} ? : 12 6 18
32 die "nonpositive window size: $self->{windowsize}"
33 18 ▶ unless $self->{windowsize} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"windowsize"} > 0 0 18 18
34 18 delete($params{windowsize});
35  
36 18 ▶ if (! exists($params{type}) or $params{type} eq 'character') {
Branch: 5 branches
BranchTrueFalseTotal
if (not exists $params{'type'} or $params{'type'} eq 'character') { } 5 13 18
elsif ($params{'type'} eq 'utf8') { } 0 13 13
elsif ($params{'type'} eq 'utf8_character') { } 0 13 13
elsif ($params{'type'} eq 'byte') { } 6 7 13
elsif ($params{'type'} eq 'word') { } 7 0 7
Condition100%3/3
not exists $params{"type"} or $params{"type"} eq "character"
l!l&&r!l&&!r
3213
MC/DC100%2/2
not exists $params{"type"} or $params{"type"} eq "character"
not exists $params{"type"} $params{"type"} eq "character"
Truth table100%3/3
not(A) or B
A= not exists $params{"type"}B= $params{"type"} eq "character"
ABresult
1X1
011
000
37 5 $self->{skiprex} = '';
38 5 $self->{tokenrex} = qr/([a-zA-Z]|[^a-zA-Z]+)/;
39 5 ▶ $self->{processtoken} = sub { s/[^a-zA-Z]+/ /; $_ = uc $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
40 5 $self->{allow_iproc} = 1;
41 }
42 elsif ($params{type} eq 'utf8') {
43 0 $self->{skiprex} = '';
44 0 $self->{tokenrex} = qr/([\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF]
45 |[\xE0-\xEF][\x80-\xBF][\x80-\xBF]
46 |[\xC2-\xDF][\x80-\xBF]
47 |[\x00-\xFF])/x;
48 0 $self->{processtoken} = '';
49 }
50 # MJ ->
51 # This type is analogous to the "character" type but defined for the utf8
52 # characters
53 elsif ($params{type} eq 'utf8_character') {
54 # $self->{inputlayer}
55 # input layer to be put on the input stream by the function binmode
56 # before reading from a given stream and to be removed by
57 # ***binmode HANDLE,":pop"*** after the reading from the particular
58 # stream is done has to be a real layer (like ":encoding(utf8)"), not a
59 # pseudo layer (like ":utf8") so that the psuedo layer ":pop" is able to
60 # remove this input layer
61
62 0 $self->{inputlayer} = ':encoding(utf8)';
63 # this will automatically decode input text from utf8 into Perl internal
64 # reporesentation of Unicode strings and so the regular expressions for
65 # Unicode as well as the uc function can be performed on them
66  
67 0 $self->{skiprex} = '';
68
69 0 $self->{tokenrex} = qr/(\p{IsAlpha}|\P{IsAlpha}+)/;
70
71 0 ▶ $self->{processtoken} = sub { s/\P{IsAlpha}+/ /; $_ = uc $_ ;
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 $_ = Encode::encode_utf8( $_ ); };
73 # the last operation ***$_=Encode::encode_utf8( $_ )*** is necessary
74 # to go back to utf8 encoding from the internal Perl representation
75 # so that for the output the n-grams are in utf8 (encoded by encode_S though)
76
77 0 $self->{allow_iproc} = 0;
78 # allow_iproc has to be 0. Otherwise the last token in the read block will
79 # be preprocessed and encoded in utf8,
80 # and then attached at the beginning of the next block read from input,
81 # which will be in the internal Perl representation
82 }
83 #MJ <-
84 elsif ($params{type} eq 'byte') {
85 6 $self->{skiprex} = '';
86 6 $self->{tokenrex} = '';
87 6 $self->{processtoken} = '';
88 }
89 elsif ($params{type} eq 'word') {
90 7 $self->{skiprex} = qr/[^a-zA-Z0-9]+/;
91 7 $self->{skipinsert} = ' ';
92 $self->{tokenrex} =
93 7 qr/([a-zA-Z]+|(\d+(\.\d+)?|\d*\.\d+)([eE][-+]?\d+)?)/;
94 $self->{processtoken} = sub
95 63 ▶ { s/(\d+(\.\d+)?|\d*\.\d+)([eE][-+]?\d+)?/<NUMBER>/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
96 7 }
97 0 else { die "unknown type: $params{type}" }
98 18 delete($params{type});
99  
100 18 $self->{'table'} = [ ];
101 18 $self->{'total'} = [ ];
102 18 $self->{'total_distinct_count'} = 0;
103 18 $self->{'lastngram'} = [ ];
104 18 $self->{'next_token_id'} = 0;
105 18 $self->{'token_dict'} = { };
106 18 $self->{'token_S'} = [ ];
107 18 $self->{'token'} = [ ];
108  
109 18 foreach my $i ( 1 .. $self->{windowsize} ) {
110 70 $self->{table}[$i] = { };
111 70 $self->{total}[$i] = 0;
112 70 $self->{firstngram}[$i] = '';
113 70 $self->{lastngram}[$i] = '';
114 }
115  
116 18 ▶ if (exists($params{'limit'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"limit"}) 1 17 18
117 1 ▶ die "limit=$params{'limit'}" if $params{'limit'} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $params{"limit"} < 1 0 1 1
118 1 $self->{'limit'} = $params{'limit'};
119 }
120 18 delete($params{'limit'});
121  
122 18 ▶ die "unknown parameters:".join(' ', %params) if %params;
Branch: 1 branch
BranchTrueFalseTotal
if %params 0 18 18
123  
124 18 bless($self, $package);
125 18 return $self;
126 }
127  
128 53 ▶sub feed_tokens {
Subroutine feed_tokens: called CC=10 SCAR=23.0
Pod documented
129 53 my $self = shift;
130 # count all n-grams sizes starting from max to 1
131 53 foreach my $t1 (@_) {
132 21117 my $t = $t1;
133 21117 ▶ if (defined($self->{token_dict}->{$t})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'token_dict'}{$t}) { } 20875 242 21117
134 20875 $t = $self->{token_dict}->{$t};
135 } else {
136 242 my $id = $self->{next_token_id}++;
137 242 $self->{token_S}->[$id] = &encode_S($t);
138 242 $self->{token}->[$id] = $t;
139 242 $t = $self->{token_dict}->{$t} = $id;
140 }
141 21117 ▶ for (my $n=$self->{windowsize}; $n > 0; $n--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 209167 21117 230284
142 209167 ▶ if ($n > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 1) { } 188050 21117 209167
143 188050 ▶ next unless $self->{lastngram}[$n-1] ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lastngram"}[$n - 1] ne "" 172 187878 188050
144 187878 $self->{lastngram}[$n] = $self->{lastngram}[$n-1] .
145 ' ' . $t;
146 21117 } else { $self->{lastngram}[$n] = $t }
147  
148 208995 ▶ if ( ($self->{table}[$n]{$self->{lastngram}[$n]} += 1)==1)
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"table"}[$n]{$self->{"lastngram"}[$n]} += 1) == 1) 121439 87556 208995
149 121439 { $self->{'total_distinct_count'} += 1 }
150  
151 208995 $self->{'total'}[$n] += 1;
152 208995 ▶ if ($self->{'firstngram'}[$n] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"firstngram"}[$n] eq "") 68 208927 208995
153 68 { $self->{'firstngram'}[$n] = $self->{lastngram}[$n] }
154 }
155 }
156 53 ▶ if (exists($self->{'limit'}) and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limit"} and $self->{"total_distinct_count"} > 2 * $self->{"limit"}) 1 52 53
Condition100%3/3
exists $self->{"limit"} and $self->{"total_distinct_count"} > 2 * $self->{"limit"}
!ll&&!rl&&r
5111
MC/DC100%2/2
exists $self->{"limit"} and $self->{"total_distinct_count"} > 2 * $self->{"limit"}
exists $self->{"limit"} $self->{"total_distinct_count"} > 2 * $self->{"limit"}
Truth table100%3/3
A and B
A= exists $self->{"limit"}B= $self->{"total_distinct_count"} > 2 * $self->{"limit"}
ABresult
0X0
100
111
157 $self->{'total_distinct_count'} > 2 * $self->{'limit'})
158 1 { $self->_reduce_to_limit }
159 }
160  
161 4 ▶sub process_text {
Subroutine process_text: called CC=2 SCAR=7.6
Pod documented
162 4 my $self = shift;
163 4 $self->_process_text(0, @_);
164 0 ▶ if (exists($self->{'limit'})) { $self->_reduce_to_limit }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limit"}) 0 4 4
165 }
166  
167 49 ▶sub _process_text {
Subroutine _process_text: called CC=21 SCAR=31.4
168 49 my $self = shift;
169 49 my $cont = shift; # the minimal number of chars left for
170 # continuation (the new-line problem, and the
171 # problem with too long lines)
172 # The remainder of unprocessed string is
173 # returned.
174 0 ▶ if ($cont < 0) { $cont = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cont < 0) 0 49 49
175  
176 49 ▶ if ( # type is byte
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq "" and $cont == 0) 19 30 49
Condition77%7/9
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq "" and $cont == 0
!ll&&!rl&&r
30019
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq ""
!ll&&!rl&&r
30019
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq ""
!ll&&!rl&&r
131719
MC/DC50%2/4
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq "" and $cont == 0
$self->{"skiprex"} eq "" $self->{"tokenrex"} eq "" $self->{"processtoken"} eq "" $cont == 0
Truth table60%3/5
A and B and C and D
A= $self->{"skiprex"} eq ""B= $self->{"tokenrex"} eq ""C= $self->{"processtoken"} eq ""D= $cont == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
177 $self->{skiprex} eq '' and
178 $self->{tokenrex} eq '' and
179 $self->{processtoken} eq '' and
180 $cont == 0
181 )
182 19 { return $self->_process_text_byte(@_) }
183  
184 30 my (@tokens);
185 my $text;
186 30 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 30 30 60
187 30 $text .= shift @_;
188 30 ▶ while (length($text) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text > 0) 10992 4 10996
189 10992 my $textl = $text;
190 10992 my $skip = '';
191 10992 ▶ if ($self->{skiprex} ne '' && $textl =~ /^$self->{skiprex}/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skiprex"} ne "" and $textl =~ /^$self->{"skiprex"}/) 74 10918 10992
Condition100%3/3
$self->{"skiprex"} ne "" and $textl =~ /^$self->{"skiprex"}/
!ll&&!rl&&r
10916274
MC/DC100%2/2
$self->{"skiprex"} ne "" and $textl =~ /^$self->{"skiprex"}/
$self->{"skiprex"} ne "" $textl =~ /^$self->{"skiprex"}/
Truth table100%3/3
A and B
A= $self->{"skiprex"} ne ""B= $textl =~ /^$self->{"skiprex"}/
ABresult
0X0
100
111
192 74 { $skip = $&; $textl = $'; }
193 10992 ▶ if (defined($self->{skipinsert})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"skipinsert"}) 76 10916 10992
194 76 $skip = $self->{skipinsert};
195 76 $text = $skip.$textl;
196 }
197 10992 ▶ if (length($textl) < $cont) { last }
Branch: 1 branch
BranchTrueFalseTotal
if (length $textl < $cont) 6 10986 10992
198 10986 ▶ if (length($textl)==0) { $text=$textl; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $textl == 0) 7 10979 10986
199  
200 10979 local $_;
201 10979 ▶ if ($self->{tokenrex} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tokenrex'} ne '') { } 10979 0 10979
202 10979 ▶ if ($textl =~ /^$self->{tokenrex}/)
Branch: 1 branch
BranchTrueFalseTotal
if ($textl =~ /^$self->{"tokenrex"}/) 10979 0 10979
203 10979 { $_ = $&; $textl = $'; }
204 }
205 else
206 0 { $_ = substr($textl, 0, 1); $textl = substr($textl, 1) }
207 10979 ▶ last if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 10979 10979
208  
209 10979 ▶ if (length($textl) < $cont) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $textl < $cont) 13 10966 10979
210 13 ▶ if (defined($self->{allow_iproc}) && $self->{allow_iproc}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"allow_iproc"} and $self->{"allow_iproc"} and ref $self->{"processtoken"} eq "CODE") 13 0 13
Condition33%2/6
defined $self->{"allow_iproc"} and $self->{"allow_iproc"} and ref $self->{"processtoken"} eq "CODE"
!ll&&!rl&&r
0013
defined $self->{"allow_iproc"} and $self->{"allow_iproc"}
!ll&&!rl&&r
0013
MC/DC0%0/3
defined $self->{"allow_iproc"} and $self->{"allow_iproc"} and ref $self->{"processtoken"} eq "CODE"
defined $self->{"allow_iproc"} $self->{"allow_iproc"} ref $self->{"processtoken"} eq "CODE"
Truth table25%1/4
A and B and C
A= defined $self->{"allow_iproc"}B= $self->{"allow_iproc"}C= ref $self->{"processtoken"} eq "CODE"
ABCresult
0XX0
10X0
1100
1111
211 && ref($self->{processtoken}) eq 'CODE')
212 13 { &{$self->{processtoken}} }
213 13 $text = $skip.$_.$textl;
214 13 last;
215 }
216 10966 ▶ if (ref($self->{processtoken}) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"processtoken"} eq "CODE") 10966 0 10966
217 10966 { &{$self->{processtoken}} }
218 10966 push @tokens, $_;
219 10966 $text = $textl;
220 }
221 }
222 30 $self->feed_tokens(@tokens);
223 30 return $text;
224 }
225  
226 19 ▶sub _process_text_byte {
Subroutine _process_text_byte: called CC=3 SCAR=11.0
227 19 my $self = shift;
228  
229 19 ▶ for (my $i=0; $i<=$#_; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 19 19 38
230 19 my @a = split('', $_[$i]);
231 19 ▶ next if $#a==-1;
Branch: 1 branch
BranchTrueFalseTotal
if $#a == -1 4 15 19
232 15 $self->feed_tokens( @a );
233 }
234 19 return '';
235 }
236  
237 13 ▶sub process_files {
Subroutine process_files: called CC=13 SCAR=26.8
Pod documented
238 13 my $self = shift;
239  
240 #MJ ->
241 13 my $input_layer='';
242 0 ▶ if (defined($self->{inputlayer})) {$input_layer=$self->{inputlayer};}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"inputlayer"}) 0 13 13
243 #MJ <-
244  
245 13 foreach my $f (@_) {
246 13 my $f1;
247 13 local *F;
248 13 ▶ if (not ref($f))
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $f) { } 13 0 13
249 13 ▶ { open(F, "$f") or die "cannot open $f:$!"; $f1 = *F }
Branch: 1 branch
BranchTrueFalseTotal
open F, "$f" or die "cannot open ${f}:$!" 13 0 13
250 0 else { $f1 = $f }
251 13 binmode $f1; # avoid text mode
252  
253 #MJ ->
254 #put the encoding layer on the input when requested
255 13 ▶ if ($input_layer ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_layer ne "") 0 13 13
256 0 binmode $f1, $input_layer;
257 }
258 #MJ <-
259  
260 13 my ($text, $text_l, $cont) = ('', 0, 1);
261 13 ▶ if ( # type is byte
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq "") 4 9 13
Condition83%5/6
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq ""
!ll&&!rl&&r
904
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq ""
!ll&&!rl&&r
634
MC/DC66%2/3
$self->{"skiprex"} eq "" and $self->{"tokenrex"} eq "" and $self->{"processtoken"} eq ""
$self->{"skiprex"} eq "" $self->{"tokenrex"} eq "" $self->{"processtoken"} eq ""
Truth table75%3/4
A and B and C
A= $self->{"skiprex"} eq ""B= $self->{"tokenrex"} eq ""C= $self->{"processtoken"} eq ""
ABCresult
0XX0
10X0
1100
1111
262 $self->{skiprex} eq '' and
263 $self->{tokenrex} eq '' and
264 $self->{processtoken} eq ''
265 )
266 4 { $cont = 0 }
267  
268 13 while (1) {
269 45 $text_l = length($text);
270 45 read($f1, $text, 1024, length($text));
271 45 ▶ last if length($text) <= $text_l;
Branch: 1 branch
BranchTrueFalseTotal
if length $text <= $text_l 13 32 45
272 32 $text = $self->_process_text($cont, $text);
273 }
274 13 $text = $self->_process_text(0, $text);
275  
276 #MJ ->
277 #remove the encoding layer from the input stream if it was added
278 #Caution: here is what the Perl documentation says about the pseudo layer ":pop"
279 #"Should be considered as experimental. (...) A more elegant (and safer) interface is needed."
280 13 ▶ if ($input_layer ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_layer ne "") 0 13 13
281 0 binmode $f1,":pop";
282 }
283 #MJ <-
284  
285 13 ▶ close($f1) if not ref($f);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $f 13 0 13
286 13 ▶ if (exists($self->{'limit'})) { $self->_reduce_to_limit }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"limit"}) 1 12 13
287 }
288 }
289  
290 2 ▶sub _reduce_to_limit {
Subroutine _reduce_to_limit: called CC=10 SCAR=23.2
291 2 my $self = shift;
292 return unless exists($self->{'limit'}) and
293 2 ▶ $self->{'limit'} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"limit"} and $self->{"limit"} > 0 0 2 2
Condition33%1/3
exists $self->{"limit"} and $self->{"limit"} > 0
!ll&&!rl&&r
002
MC/DC0%0/2
exists $self->{"limit"} and $self->{"limit"} > 0
exists $self->{"limit"} $self->{"limit"} > 0
Truth table33%1/3
A and B
A= exists $self->{"limit"}B= $self->{"limit"} > 0
ABresult
0X0
100
111
294  
295 2 ▶ while ($self->{'total_distinct_count'} > $self->{'limit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"total_distinct_count"} > $self->{"limit"}) 1 1 2
296 1 my $nextprunefrequency = 0;
297 1 for (my $prunefrequency=1;; $prunefrequency = $nextprunefrequency) {
298 2 $nextprunefrequency = $self->{'total'}[1];
299  
300 2 foreach my $n (1 .. $self->{'windowsize'}) {
301  
302 4 my @keys = keys(%{$self->{table}[$n]});
303 4 foreach my $ngram (@keys) {
304 22 my $f = $self->{table}[$n]{$ngram};
305 22 ▶ if ($f <= $prunefrequency) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f <= $prunefrequency) { } 11 11 22
elsif ($nextprunefrequency > $f) { } 3 8 11
306 11 delete $self->{'table'}[$n]{$ngram};
307 11 $self->{'total'}[$n] -= $prunefrequency;
308 11 $self->{'total_distinct_count'} -= 1;
309 }
310 elsif ($nextprunefrequency > $f)
311 3 { $nextprunefrequency = $f }
312 }
313  
314 4 ▶ return if $self->{'total_distinct_count'} <= $self->{'limit'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"total_distinct_count"} <= $self->{"limit"} 1 3 4
315 3 ▶ die if $nextprunefrequency <= $prunefrequency;
Branch: 1 branch
BranchTrueFalseTotal
if $nextprunefrequency <= $prunefrequency 0 3 3
316 } } } }
317  
318 # Sorts keys according to the lexicographic order.
319 79 ▶sub _keys_sorted {
Subroutine _keys_sorted: called CC=3 SCAR=11.0
320 79 my $self = shift;
321 79 my $n = shift;
322 79 my @k = keys(%{$self->{table}[$n]});
323 79 my %k1 = ();
324 79 foreach my $k (@k) {
325 $k1{
326 95499 join (' ', map { $self->{token}->[$_] } split(/ /, $k) )
327 } = $k;
328 }
329 79 @k = ();
330 79 foreach my $k (sort(keys(%k1))) {
331 95499 push @k, $k1{$k};
332 }
333 79 return @k;
334 }
335  
336 55 ▶sub get_ngrams {
Subroutine get_ngrams: called CC=14 SCAR=27.1
Pod documented
337 55 my $self = shift;
338 55 my (%params) = @_;
339 55 ▶ my $n = exists($params{'n'})? $params{'n'} : $self->{windowsize};
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'n'} ? : 54 1 55
340 55 ▶ my $onlyfirst = exists($params{'onlyfirst'}) ? $params{'onlyfirst'} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'onlyfirst'} ? : 23 32 55
341 55 ▶ my $opt_normalize = exists($params{'normalize'}) ?$params{'normalize'} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'normalize'} ? : 23 32 55
342  
343 55 my $total = $self->{total}[$n]; my @keys = ();
344 55 ▶ if (!exists($params{'orderby'}) or $params{'orderby'} eq 'ngram') {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $params{'orderby'} or $params{'orderby'} eq 'ngram') { } 24 31 55
elsif ($params{'orderby'} eq 'none') { } 0 31 31
elsif ($params{'orderby'} eq 'frequency') { } 31 0 31
Condition100%3/3
not exists $params{"orderby"} or $params{"orderby"} eq "ngram"
l!l&&r!l&&!r
12331
MC/DC100%2/2
not exists $params{"orderby"} or $params{"orderby"} eq "ngram"
not exists $params{"orderby"} $params{"orderby"} eq "ngram"
Truth table100%3/3
not(A) or B
A= not exists $params{"orderby"}B= $params{"orderby"} eq "ngram"
ABresult
1X1
011
000
345 24 @keys = $self->_keys_sorted($n);
346 } elsif ($params{'orderby'} eq 'none') {
347 0 ▶ die "onlyfirst requires order" if $onlyfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $onlyfirst 0 0 0
348 0 @keys = keys(%{$self->{table}[$n]})
349 }
350 elsif ($params{'orderby'} eq 'frequency') {
351 31 @keys = $self->_keys_sorted($n);
352 31 my %keysord = ();
353 31 ▶ for (my $i=0; $i<=$#keys; ++$i) { $keysord{$keys[$i]} = $i }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#keys) 95039 31 95070
354 @keys = sort { $self->{table}[$n]{$b} <=> $self->{table}[$n]{$a}
355 1074193 ▶ or $keysord{$a} <=> $keysord{$b} }
Condition66%2/3
$self->{'table'}[$n]{$b} <=> $self->{'table'}[$n]{$a} || $keysord{$a} <=> $keysord{$b}
l!l&&r!l&&!r
7391410002790
MC/DC0%0/2
$self->{'table'}[$n]{$b} <=> $self->{'table'}[$n]{$a} || $keysord{$a} <=> $keysord{$b}
$self->{'table'}[$n]{$b} <=> $self->{'table'}[$n]{$a} $keysord{$a} <=> $keysord{$b}
Truth table66%2/3
A || B
A= $self->{'table'}[$n]{$b} <=> $self->{'table'}[$n]{$a}B= $keysord{$a} <=> $keysord{$b}
ABresult
1X1
011
000
356 31 keys(%{$self->{table}[$n]});
357 }
358 0 else { die }
359  
360 55 ▶ @keys = splice(@keys,0,$onlyfirst) if $onlyfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $onlyfirst 23 32 55
361  
362 55 my @ret;
363 55 foreach my $ngram (@keys) {
364 2370 my $count = $self->{table}[$n]{$ngram};
365 2370 ▶ $count = ($opt_normalize ? ($count / $total ) : $count);
Branch: 1 branch
BranchTrueFalseTotal
$opt_normalize ? : 1999 371 2370
366 2370 push @ret, $self->_encode_S($ngram), $count;
367 }
368  
369 55 return @ret;
370 }
371  
372 17 ▶sub to_string {
Subroutine to_string: called CC=25 SCAR=32.4
Pod documented
373 17 my $self = shift;
374 17 my (%params) = @_;
375 17 ▶ my $n = exists($params{'n'})? $params{'n'} : $self->{windowsize};
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'n'} ? : 0 17 17
376 17 ▶ my $onlyfirst = exists($params{'onlyfirst'}) ? $params{'onlyfirst'} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'onlyfirst'} ? : 4 13 17
377 17 ▶ my $opt_normalize = exists($params{'normalize'}) ?$params{'normalize'} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'normalize'} ? : 4 13 17
378
379 #my $onlyfirst = exists($params{'onlyfirst'}) ?
380 #$params{'onlyfirst'} : '';
381 #delete $params{'onlyfirst'};
382  
383 17 ▶ my $out = exists($params{'out'}) ? $params{'out'} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'out'} ? : 7 10 17
384 17 delete $params{'out'};
385 17 my $outh = $out;
386 17 ▶ if ($out and (not ref($out))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out and not ref $out) 1 16 17
Condition100%3/3
$out and not ref $out
!ll&&!rl&&r
1061
MC/DC100%2/2
$out and not ref $out
$out not ref $out
Truth table100%3/3
A and not(B)
A= $outB= not ref $out
ABresult
0X0
100
111
387 1 ▶ local *FH; open(FH, ">$out") or die "cannot open $out:$!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$out" or die "cannot open ${out}:$!" 1 0 1
388 1 $outh = *FH;
389 }
390  
391 #my $opt_normalize = $params{'normalize'};
392 #delete $params{'normalize'};
393  
394 17 my $spartan = $params{'spartan'};
395 17 delete $params{'spartan'};
396  
397 17 my $ret='';
398 17 ▶ $ret = "BEGIN OUTPUT BY Text::Ngrams version $VERSION\n\n" unless $spartan;
Branch: 1 branch
BranchTrueFalseTotal
unless $spartan 15 2 17
399  
400 17 foreach my $n (1 .. $self->{windowsize}) {
401 67 ▶ if ($spartan and $n < $self->{windowsize}) { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($spartan and $n < $self->{"windowsize"}) 13 54 67
Condition100%3/3
$spartan and $n < $self->{"windowsize"}
!ll&&!rl&&r
52213
MC/DC100%2/2
$spartan and $n < $self->{"windowsize"}
$spartan $n < $self->{"windowsize"}
Truth table100%3/3
A and B
A= $spartanB= $n < $self->{"windowsize"}
ABresult
0X0
100
111
402 54 ▶ if (! $spartan ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spartan) 52 2 54
403 52 my $tmp = "$n-GRAMS (total count: $self->{total}[$n])";
404 $ret .= "$tmp\n" .
405 "FIRST N-GRAM: ". $self->_encode_S($self->{firstngram}[$n]).
406 52 "\n LAST N-GRAM: ".$self->_encode_S($self->{lastngram}[$n])."\n".
407 ('-' x length($tmp)) . "\n";
408 }
409 54 my $total = $self->{total}[$n];
410  
411 54 my @keys;
412 54 ▶ if (!exists($params{'orderby'}) or $params{'orderby'} eq 'ngram')
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $params{'orderby'} or $params{'orderby'} eq 'ngram') { } 24 30 54
elsif ($params{'orderby'} eq 'none') { } 0 30 30
elsif ($params{'orderby'} eq 'frequency') { } 30 0 30
Condition100%3/3
not exists $params{"orderby"} or $params{"orderby"} eq "ngram"
l!l&&r!l&&!r
12330
MC/DC100%2/2
not exists $params{"orderby"} or $params{"orderby"} eq "ngram"
not exists $params{"orderby"} $params{"orderby"} eq "ngram"
Truth table100%3/3
not(A) or B
A= not exists $params{"orderby"}B= $params{"orderby"} eq "ngram"
ABresult
1X1
011
000
413 24 { @keys = $self->_keys_sorted($n) }
414 elsif ($params{'orderby'} eq 'none') {
415 0 ▶ die "onlyfirst requires order" if $onlyfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $onlyfirst 0 0 0
416 0 @keys = keys(%{$self->{table}[$n]})
417 }
418 elsif ($params{'orderby'} eq 'frequency') {
419 @keys = sort { $self->{table}[$n]{$b} <=>
420 298536 $self->{table}[$n]{$a} }
421 30 keys(%{$self->{table}[$n]});
422 }
423 0 else { die }
424  
425 54 ▶ @keys = splice(@keys,0,$onlyfirst) if $onlyfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $onlyfirst 23 31 54
426  
427 54 my %params1=%params; $params1{n}=$n;
428 54 my @a = $self->get_ngrams(%params1);
429 54 ▶ for (my $i=0; $i<=$#a; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#a) 2337 54 2391
430 2337 my $ng = $a[$i]; my $f = $a[$i+1];
431 2337 $ret.="$ng\t$f\n";
432 }
433 54 ▶ if ($out) { print $outh $ret; $ret = '' }
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 29 25 54
434  
435 54 ▶ $ret .= "\n" unless $spartan;
Branch: 1 branch
BranchTrueFalseTotal
unless $spartan 52 2 54
436 }
437  
438 17 ▶ $ret .= "END OUTPUT BY Text::Ngrams\n" unless $spartan;
Branch: 1 branch
BranchTrueFalseTotal
unless $spartan 15 2 17
439  
440 17 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 7 10 17
441 7 print $outh $ret; $ret = '';
442 7 ▶ close($outh) if not ref($out);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out 1 6 7
443 }
444  
445 17 return $ret;
446 }
447  
448 # http://web.cs.dal.ca/~vlado/srcperl/snip/decode_S
449 2 ▶sub decode_S ( $ ) {
Subroutine decode_S: called CC=8 SCAR=24.8
Pod documented
450 2 local $_ = shift;
451 2 my $out;
452  
453 2 ▶ while (length($_) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > 0) 10 2 12
454 10 ▶ if (/^\\(\S)/) {
Branch: 6 branches
BranchTrueFalseTotal
if (/^\\(\S)/) { } 4 6 10
elsif (/^\^_/) { } 0 6 6
elsif (/^\^(\S)/) { } 2 4 6
elsif (/^\`(\S)/) { } 0 4 4
elsif (/^_+/) { } 0 4 4
elsif (/^[^\\^\`\s_]+/) { } 4 0 4
455 4 $_ = $'; my $tmp = $1;
456 4 $tmp =~ tr/0-5Aabtnvfroil6-9NSTcEseFGRUd/\x00-\x1F\x7F/;
457 4 $out .= $tmp;
458 }
459 0 elsif (/^\^_/) { $_ = $'; $out .= "\240" }
460 2 elsif (/^\^(\S)/) { $_ = $'; $out .= pack('C',ord($1)+128); }
461 elsif (/^\`(\S)/) {
462 0 $_ = $'; my $tmp = $1;
463 0 $tmp =~ tr/0-5Aabtnvfroil6-9NSTcEseFGRUd/\x00-\x1F\x7F/;
464 0 $out .= pack('C', ord($tmp)+128);
465 }
466 0 elsif (/^_+/) { $_ = $'; my $tmp = $&; $tmp =~ tr/_/ /; $out .= $tmp; }
467 4 elsif (/^[^\\^\`\s_]+/) { $_ = $'; $out .= $&; }
468 0 else { die "decode_S unexpected:$_" }
469 }
470  
471 2 return $out;
472 }
473  
474 2474 ▶sub _encode_S {
Subroutine _encode_S: called CC=2 SCAR=6.9
475 2474 my $self = shift;
476 2474 my @r = ();
477 2474 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2474 2474 4948
478 push @r,
479 2474 map { $self->{token_S}->[$_] } split(/ /, shift @_);
480 }
481 2474 return join(' ', @r);
482 }
483  
484 # http://www.cs.dal.ca/~vlado/srcperl/snip/encode_S
485 244 ▶sub encode_S( $ ) {
Subroutine encode_S: called CC=1 SCAR=0.0
Pod documented
486 244 local $_ = shift;
487  
488 244 s/=/=0/g; # first hide a special character (=)
489 244 s/\\/=b/g; # encode backslashes
490  
491 244 s/([\x80-\xFF])/=x$1/g; # replace >127 with 127
492 244 tr/\x80-\xFF/\x00-\x7F/;
493 244 s/=x=/=X/g; # hide again =
494  
495 244 s/([\x00-\x1F\x5C\x5E-\x60\x7F])/=B$1/g;
496 244 tr/\x20\x00-\x1F\x7F/_0-5Aabtnvfroil6-9NSTcEseFGRUd/;
497  
498 244 s/=x=B(\S)/`$1/g; # hex backslash
499 244 s/=x(\S)/^$1/g; # hex other
500 244 s/=B(\S)/\\$1/g; # backslashed
501 244 s/=b/\\\\/g; # original backslashes
502 244 s/=X/^=0/g;
503 244 s/=0/=/g; # put back =
504  
505 244 return $_;
506 }
507  
508 15 1;
509 __END__
↑ Summary blib/script/ngrams.pl »