AIE/Method/Walking.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 221 CC 27 SCAR 59.6
CC
27
Cov
0%
AIE
552.0
nLCSList2
12.0
AddToRegex
12.0
CRAP 387.2
1 package Org::FRDCSA::AIE::Method::Walking;
2  
3 # use PerlLib::SwissArmyKnife;
4  
5 use Algorithm::Diff qw(LCS);
6  
7 require Exporter;
8 @ISA = qw(Exporter);
9 @EXPORT = qw (AIE);
10  
11 my @regex;
12  
13 sub AIE {
14 my %args = @_;
15 my $entries = $args{Entries};
16  
17 @regex = ();
18 my @all;
19 foreach my $entry (@$entries) {
20 push @all, [split //, $entry];
21 }
22  
23 my $loud = 0;
24 my $finished = 0;
25 my $synced = 1;
26 my $indicies = {};
27 my $size = {};
28 my $i = 0;
29 foreach my $i (0.. ((scalar @all) - 1)) {
30 $indicies->{$i} = 0;
31 $size->{$i} = scalar @{$all[$i]};
32 }
33 while (! $finished) {
34 if ($synced) {
35 # see if the next character is the same
36 my $nextchars = {};
37 foreach my $key (keys %$indicies) {
38 $nextchars->{$all[$key]->[$indicies->{$key}]} = 1;
39 }
40 if ((scalar keys %$nextchars) == 1) {
41 AddToRegex(Char => [keys %$nextchars]->[0]);
42 foreach my $key (keys %$indicies) {
43 $indicies->{$key}++;
44 if ($indicies->{$key} >= $size->{$key}) {
45 # this string is ended
46 # what to do?
47  
48 # more or less we have a few options, we could just add
49 # everything that follows on every string as one last input
50  
51 # or we could continue without this
52  
53 # but for now, in development, let's just quit
54 $finished = 1;
55 }
56 }
57 } else {
58 $synced = 0;
59 AddToRegex
60 (
61 Control => "(.*?)",
62 );
63 # need to resync the lists
64  
65 # do a thing where we iterate and get the LCS
66  
67 my $finishedsyncing = 0;
68 my $window = 1;
69 my $lcs;
70 while (! $finishedsyncing) {
71 # take a window and make the window larger and larger
72 my @candidates;
73 my $overages = 0;
74 foreach my $key (keys %$indicies) {
75 my $last = $indicies->{$key} + $window;
76 if ($last >= ($size->{$key} - 1)) {
77 $last = ($size->{$key} - 1);
78 $overages++;
79 }
80 print "THING<".$indicies->{$key}."><$last>\n" if $loud;
81 my @entry;
82 foreach my $i ($indicies->{$key}..$last) {
83 push @entry, $all[$key]->[$i];
84 }
85 push @candidates, \@entry;
86 }
87 if ($overages == scalar keys %$indicies) {
88 $finishedsyncing = 1;
89 $finished = 1;
90 }
91  
92 print Dumper(\@candidates) if $loud;
93 # print Dumper({Candidates => \@candidates}) if $loud;
94 $lcs = nLCSList2
95 (
96 Entries => \@candidates,
97 );
98 my $string = join("",@$lcs);
99 print "$string\n" if $loud;
100 print Dumper({LCS0 => $lcs}) if $loud;
101 if ((scalar @$lcs) > 4) {
102 # go ahead and find the rest there
103 print Dumper({LCS1 => $lcs}) if $loud;
104 $finishedsyncing = 1;
105 }
106 ++$window;
107 print "\n" if $loud;
108 }
109  
110 print Dumper({LCS2 => $lcs}) if $loud;
111  
112 # okay now we have to sync everything
113 my $cant = 0;
114 foreach my $key (keys %$indicies) {
115 my $idx = $indicies->{$key};
116 my @copy = @$lcs;
117 print Dumper(\@copy) if $loud;
118 my $char = shift @copy;
119 my $first;
120 while (scalar @copy) {
121 if ($all[$key]->[$idx] ne $char) {
122 ++$idx;
123 } else {
124 if (! $first) {
125 $first = $idx;
126 }
127 $char = shift @copy;
128 }
129 }
130 ++$idx;
131 if (! defined $first) {
132 $first = $indicies->{$key};
133 }
134 # update the index
135 print "KeyIDX<$key><$idx>\n" if $loud;
136 my $diff = $idx - $first;
137 if ($diff != 4) {
138 print "<DIFF: $diff>\n" if $loud;
139 $cant = 1;
140 }
141 $indicies->{$key} = $idx + 1;
142 }
143 if ($cant) {
144 foreach my $item (@$lcs) {
145 AddToRegex
146 (
147 Char => $item,
148 );
149 AddToRegex
150 (
151 Control => "(.*?)",
152 );
153 }
154 } else {
155 AddToRegex
156 (
157 Char => join("", @$lcs),
158 );
159 }
160 $synced = 1;
161 }
162 } else {
163 # Hrm, don't we already handle this above?
164 }
165 }
166  
167 my $regex = join("",@regex);
168 print "REGEX: ".$regex."\n\n\n";
169  
170 my @final;
171 foreach my $entry (@$entries) {
172 my @results;
173 my @matches = $entry =~ /$regex/;
174 foreach my $match (@matches) {
175 if ($match ne "") {
176 push @results, $match;
177 }
178 }
179 push @final,\@results;
180 }
181 return \@final;
182 }
183  
184 sub nLCSString {
185 my %args = @_;
186 my $entries = $args{Entries};
187 my $size = scalar @$entries;
188 my @intermediate = split //, $entries->[0];
189 foreach my $i (0 .. ($size - 2)) {
190 @intermediate = LCS(\@intermediate,[split //, $entries->[$i + 1]]);
191 }
192 return join("",@intermediate);
193 }
194  
195 sub nLCSList {
196 my %args = @_;
197 my $entries = $args{Entries};
198 my $size = scalar @$entries;
199 my @intermediate = @{$entries->[0]};
200 foreach my $i (0 .. ($size - 2)) {
201 @intermediate = LCS(\@intermediate,$entries->[$i + 1]);
202 }
203 return \@intermediate;
204 }
205  
206 sub nLCSList2 {
207 my %args = @_;
208 my $entries = $args{Entries};
209 my $width = 5;
210 my $entryid = 0;
211 my $hash = {};
212 foreach my $entry (@$entries) {
213 my $size = scalar @$entry;
214 if ($size >= $width) {
215 foreach my $i (0..($size-$width)) {
216 my @copy;
217 foreach my $j (0..($width - 1)) {
218 push @copy, $entry->[$i+$j];
219 }
220 $hash->{join("",@copy)}->{$entryid} = 1;
221 }
222 } else {
223 return [];
224 }
225 ++$entryid;
226 }
227  
228 foreach my $key (keys %$hash) {
229 if (scalar keys %{$hash->{$key}} == scalar @$entries) {
230 # print "HOLYCOW: <$key>\n".length($key)."\n";
231 return [split //,$key];
232 }
233 }
234 }
235  
236 sub AddToRegex {
237 my %args = @_;
238 if ($args{Char}) {
239 my $char = $args{Char};
240 $char =~ s/([^a-zA-Z0-9])/\\$1/g;
241 push @regex, $char;
242 } elsif ($args{Control}) {
243 push @regex, $args{Control};
244 }
245 }
246  
247 1;