blib/lib/Games/Dissociate.pm

filter: Statementstmt 64.8% 59 / 91 Branchbran 31.2% 15 / 48 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 83.3% 5 / 6 Podpod 100% 2 / 2 totaltotal 48.6% 87 / 179 CC 33 SCAR 42.7
CC
33
Cov
50%
dissociate_filter
132.0
dissociate
54.5
BEGIN
1.0
CRAP 71.3
↑ Summary
1 ### The POD is at the end. ###
2 3 require 5.000;
3 package Games::Dissociate;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 require Exporter;
6  
7 3 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $Debug $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 @ISA = qw(Exporter);
10 3 @EXPORT = qw(dissociate_filter dissociate);
11 3 $VERSION = 1.0;
12 3 $Debug = 0;
13  
14 ###########################################################################
15  
16 0 ▶sub dissociate_filter {
Subroutine dissociate_filter: not called CC=11 SCAR=48.8
Pod documented
17 0 require Text::Wrap;
18 0 require Getopt::Std;
19 0 my %o;
20 0 ▶ if(@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 0 0 0
21 0 ▶ Getopt::Std::getopts('c:w:m:', \%o)
Branch: 1 branch
BranchTrueFalseTotal
Getopt::Std::getopts("c:w:m:", \%o) or die "Options:\n -cNUMBER\n Run a by-character dissociation with that number of\n characters as the group size.\n -wNUMBER\n Run a by-word dissociation with that number of\n words as the group size.\n -mNUMBER\n Specifies how many iterations the dissociator loop should make.\n" 0 0 0
22 or die "Options:
23 -cNUMBER
24 Run a by-character dissociation with that number of
25 characters as the group size.
26 -wNUMBER
27 Run a by-word dissociation with that number of
28 words as the group size.
29 -mNUMBER
30 Specifies how many iterations the dissociator loop should make.
31 ";
32 }
33  
34 0 my $o;
35 my $max;
36 0 ▶ if($_[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
elsif ($o{'w'}) { } 0 0 0
elsif ($o{'c'}) { } 0 0 0
37 0 $o = $_[0];
38 } elsif($o{'w'}){
39 0 $o = - abs($o{'w'});
40 } elsif($o{'c'}){
41 0 $o = abs($o{'c'});
42 }
43 0 ▶ $o ||= 2;
Condition0%0/2
$o ||= 2
l!l
00
MC/DC0%0/1
$o ||= 2
$o
Truth table0%0/2
A
A= $o
Aresult
11
00
44  
45 0 ▶ if($_[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1]) { } 0 0 0
elsif ($o{'m'}) { } 0 0 0
46 0 $max = $_[1];
47 } elsif ($o{'m'}) {
48 0 $max = abs($o{'m'});
49 }
50 0 ▶ $max ||= 100;
Condition0%0/2
$max ||= 100
l!l
00
MC/DC0%0/1
$max ||= 100
$max
Truth table0%0/2
A
A= $max
Aresult
11
00
51  
52 0 ▶ print "group_length: $o. max_length: $max\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Dissociate::Debug 0 0 0
53 0 print Text::Wrap::wrap( '','', dissociate(join('', <>), $o, $max) ), "\n";
54 0 return;
55 }
56  
57 #==========================================================================
58 5 ▶sub dissociate {
Subroutine dissociate: called CC=23 SCAR=40.0
Pod documented
59 5 my $in = $_[0];
60 5 ▶ my $arg = int($_[1] || 2);
Condition50%1/2
$_[1] || 2
l!l
05
MC/DC0%0/1
$_[1] || 2
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
61 5 ▶ my $iteration_limit = $_[2] || 100;
Condition50%1/2
$_[2] || 100
l!l
05
MC/DC0%0/1
$_[2] || 100
$_[2]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
62 5 my @out;
63  
64 5 my $by_word = ($arg < 0);
65 5 my $degree = abs($arg);
66 5 my $last_match_point;
67  
68 5 ▶ $degree = 2 if $degree == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $degree == 1 0 5 5
69  
70 3 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 5 $in =~ tr<\cm\cj \t>< >s;
73 5 ▶ die "No input\n" unless length $in;
Branch: 1 branch
BranchTrueFalseTotal
unless length $in 0 5 5
74 5 study $in;
75  
76 5 my $new_matcher;
77 5 ▶ if($by_word) {
Branch: 1 branch
BranchTrueFalseTotal
if ($by_word) { } 0 5 5
78 0 $new_matcher = "\\W+(" . join("\\W+", ("\\w+") x $degree) . ")";
79 } else {
80 5 $new_matcher = "(" . ('.' x $degree) . ")";
81 }
82  
83 # In use in the loop.
84 5 my($re, @orig, $matched, $remainder,
85 $i, $last_matched, $iteration);
86 5 $iteration = 0;
87  
88 5 $last_match_point = -1;
89 5 ▶ while($iteration < $iteration_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iteration < $iteration_limit) 500 5 505
90 500 ++$iteration;
91 500 ▶ if($last_matched) { # last thing we matched -- '' means take a stab
Branch: 1 branch
BranchTrueFalseTotal
if ($last_matched) { } 495 5 500
92 495 $last_match_point = pos($in);
93 495 ▶ if($by_word) { # By word...
Branch: 1 branch
BranchTrueFalseTotal
if ($by_word) { } 0 495 495
94 0 @orig = map(quotemeta($_), $last_matched =~ m/(\w+)/sg );
95 0 $re = "\\b"
96 . join("\\W+", @orig) # overlap
97 . "\\W+("
98 . join("\\W+", ("\\w+") x $degree) # new tokens
99 . ")(\\W+)"
100 ;
101 0 $matched = $remainder = '';
102 0 $last_match_point = pos($in);
103  
104 0 ▶ if($in =~ m/$re/sig || $in =~ m/$re/sig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /$re/gils or $in =~ /$re/gils) 0 0 0
Condition0%0/3
$in =~ /$re/gils or $in =~ /$re/gils
l!l&&r!l&&!r
000
MC/DC0%0/2
$in =~ /$re/gils or $in =~ /$re/gils
$in =~ /$re/gils $in =~ /$re/gils
Truth table0%0/3
A or B
A= $in =~ /$re/gilsB= $in =~ /$re/gils
ABresult
1X1
011
000
105 0 $matched = $1;
106 0 $remainder = $2;
107 }
108  
109 } else { # By char...
110 495 @orig = map(quotemeta($_), $last_matched =~ m/(.)/sg );
111 495 $re = join('', @orig) # overlap
112 . '('
113 . ("." x $degree) # new tokens
114 . ')';
115  
116 495 ▶ $matched = $1 if $in =~ m/$re/sig || $in =~ m/$re/sig;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /$re/gils or $in =~ /$re/gils 495 0 495
Condition66%2/3
$in =~ /$re/gils or $in =~ /$re/gils
l!l&&r!l&&!r
460350
MC/DC0%0/2
$in =~ /$re/gils or $in =~ /$re/gils
$in =~ /$re/gils $in =~ /$re/gils
Truth table66%2/3
A or B
A= $in =~ /$re/gilsB= $in =~ /$re/gils
ABresult
1X1
011
000
117 }
118  
119 495 ▶ if( $last_match_point == pos($in) # This was a hapax legomenon.
Branch: 1 branch
BranchTrueFalseTotal
if ($last_match_point == pos $in or pos $in == 0) 0 495 495
Condition33%1/3
$last_match_point == pos $in or pos $in == 0
l!l&&r!l&&!r
00495
MC/DC0%0/2
$last_match_point == pos $in or pos $in == 0
$last_match_point == pos $in pos $in == 0
Truth table33%1/3
A or B
A= $last_match_point == pos $inB= pos $in == 0
ABresult
1X1
011
000
120 || pos($in) == 0 # We didn't match anything.
121  
122 # hm, this seems to be not just unnecessary, but BAD.
123 # || abs($last_match_point - pos($in)) < length($in)
124 )
125 {
126 0 ▶ print "Hm, dead end at pos ", (0 + pos($in)), "\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Dissociate::Debug 0 0 0
127  
128 0 $last_matched = '';
129 0 next;
130 }
131  
132 495 $last_matched = $matched;
133 495 ▶ print "Matched ($matched) at ", pos($in), "\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Dissociate::Debug 0 495 495
134 495 ▶ push @out, $by_word ? ($last_matched . $remainder)
Branch: 1 branch
BranchTrueFalseTotal
$by_word ? : 0 495 495
135 : $last_matched;
136 495 next;
137  
138 } else {
139 # We don't have a last_matched -- take a stab.
140 5 my($frame, $frame_size);
141 5 pos($in) = 0; # Ever necessary?
142 5 ▶ if($by_word) {
Branch: 1 branch
BranchTrueFalseTotal
if ($by_word) { } 0 5 5
143 0 $frame_size = ($degree + 3) * 8;
144 # Generously assume 8 chars per word.
145 } else {
146 5 $frame_size = ($degree + 1) * 4;
147 # Generously assume 4 bytes per "." char.
148 }
149  
150 5 my $i = int(rand(length($in) - $frame_size));
151 5 pos($in) = $i;
152 5 ▶ print "Taking a stab at pos $i\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Dissociate::Debug 0 5 5
153 5 ▶ if( $in =~ m/$new_matcher/isg
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /$new_matcher/gils or $in =~ /$new_matcher/gils) { } 5 0 5
Condition33%1/3
$in =~ /$new_matcher/gils or $in =~ /$new_matcher/gils
l!l&&r!l&&!r
500
MC/DC0%0/2
$in =~ /$new_matcher/gils or $in =~ /$new_matcher/gils
$in =~ /$new_matcher/gils $in =~ /$new_matcher/gils
Truth table33%1/3
A or B
A= $in =~ /$new_matcher/gilsB= $in =~ /$new_matcher/gils
ABresult
1X1
011
000
154 || $in =~ m/$new_matcher/isg ) # Yes, try TWICE! Magic, wooo.
155 {
156 5 $last_matched = $1;
157 } else {
158 0 ▶ print "Can't get an initial $degree-token match" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Dissociate::Debug 0 0 0
159 0 last;
160 }
161 }
162  
163 } # end while
164  
165 5 return join('', @out);
166 }
167  
168 #==========================================================================
169 3 1;
170  
171 __END__
↑ Summary