AIE2.pm untested

filter: Statementstmt 0.0% 0 / 209 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 283 CC 28 SCAR 47.8
CC
28
Cov
0%
ExtractMajorPatterns
240.0
GetAllSubstringsLengthLessThanSize
56.0
GenerateRegexFromCycle
42.0
CRAP 119.2
1 package Org::FRDCSA::AIE2;
2  
3 # this has been modified to allow us to explicitly set the cycles,
4 # i.e. if we have repeated data from which we wish to extract the
5 # patterns
6  
7 # use PerlLib::SwissArmyKnife;
8  
9 use Algorithm::Diff qw(LCS);
10 use Data::Dumper;
11  
12 use Class::MethodMaker
13 new_with_init => 'new',
14 get_set =>
15 [
16 qw /
17 Verbose
18 /
19 ];
20  
21 sub init {
22 my ($self,%args) = @_;
23 $self->Verbose(1);
24 $self->ExtractMajorPatterns
25 (String => $args{Contents});
26 }
27  
28 sub ExtractMajorPatterns {
29 my ($self,%args) = @_;
30 print "Extracting major patterns\n" if $self->Verbose;
31 my $occurances = $self->GetAllSubstringsLengthLessThanSize
32 (Size => 10,
33 String => $args{String});
34 my $res = $self->ExtractMostUsefulTerms
35 (Occurances => $occurances);
36 # now spot a major item, do a spot check and print out repeating lists
37 # convert to the list format
38 my $order = {};
39 foreach my $k1 (keys %$occurances) {
40 foreach my $k2 (keys %{$occurances->{$k1}}) {
41 $order->{$k2}->{$k1} = 1;
42 }
43 }
44 # now we have the list, so select a random instance of a
45 # popular token, and then extract the next instance
46  
47 my $token;
48 my $counter = 10;
49 if (0) {
50 do {
51 $token = $res->{List}->[int rand $counter];
52 ++$counter;
53 } while (length($token) < 5 or $token =~ /^\s*$/);
54 } else {
55 foreach my $token2 (@{$res->{List}}) {
56 if ($token2 =~ /DFSJDKFL/) {
57 $token = $token2;
58 last;
59 }
60 }
61 }
62 print "Chose token: ".Dumper($token)."\n" if $self->Verbose;
63  
64 my $l = [sort {$a <=> $b} keys %{$occurances->{$token}}];
65 my $listsize = scalar @$l;
66 my $i = int rand $listsize;
67 print "Selected instance $i of $listsize\n" if $self->Verbose;
68 # print the remaining cycles
69 my $cycles = [];
70 for (my $cycle = 0; $cycle < 10; ++$cycle) {
71 $cycles->[$cycle] = [];
72 for (my $j = $l->[$i+$cycle]; $j < $l->[$i+1+$cycle]; ++$j) {
73 if (exists $order->{$j}) {
74 my $neworder;
75 foreach my $k (keys %{$order->{$j}}) {
76 if (length($k) > 1) {
77 my $tmp = $res->{Ilist}->{$k};
78 if (defined $tmp) { # and $tmp < 450) {
79 $neworder->{$k} = $j;
80 }
81 }
82 }
83 push @{$cycles->[$cycle]}, $neworder if $neworder;
84 }
85 }
86 }
87  
88 # remove tokens that are not common to all three
89 my $cyclecount = 0;
90 my $seen = {};
91 foreach my $cycle (@$cycles) {
92 foreach my $hash (@$cycle) {
93 foreach my $k (keys %$hash) {
94 $seen->{$k}->{$cyclecount} = 1;
95 }
96 }
97 ++$cyclecount;
98 }
99 my $keep = {};
100 foreach my $k (keys %$seen) {
101 if (scalar keys %{$seen->{$k}} == $cyclecount) {
102 $keep->{$k} = 1;
103 }
104 }
105  
106 # now reconstruct the cycles using the keep predicate
107 my $newcycles = [];
108 foreach my $cycle (@$cycles) {
109 my $newcycle = [];
110 foreach my $hash (@$cycle) {
111 my $newhash = {};
112 foreach my $key (keys %$hash) {
113 if (exists $keep->{$key}) {
114 $newhash->{$key} = $hash->{$key};
115 }
116 }
117 if (scalar keys %$newhash) {
118 push @$newcycle, $newhash;
119 }
120 }
121 push @$newcycles, $newcycle;
122 }
123  
124 my $regex = $self->ExtractRegexFromCycles
125 (
126 Index => $i,
127 Cycles => $newcycles,
128 TokenList => $l,
129 );
130  
131 # clean the regular expression
132 $regex =~ s/(\(\.\*\))+/(.*)/g;
133 $regex =~ s/^(\(\.\*\))//;
134 $regex =~ s/(\(\.\*\))$//;
135 my @seeks = $regex =~ /(\(\.\*\))/g;
136 my $size = scalar @seeks;
137  
138 print Dumper($regex)."\n" if $self->Verbose;
139  
140 my @elements = $args{String} =~ /$regex/g;
141  
142 my @entries;
143 while (@elements) {
144 push @entries, [splice(@elements,0,$size)];
145 }
146 print "Extracted ".(scalar @entries)." records\n" if $self->Verbose;
147 print Dumper(\@entries);
148 }
149  
150 sub GetAllSubstringsLengthLessThanSize {
151 my ($self,%args) = @_;
152 my $occurances = {};
153 # now we want to efficiently generate this
154  
155 # hash H->{STRING}->{OFFSET} = 1;
156  
157 # then repetition frequency is just counting the scalar keys of H->{STRING}
158  
159 # to efficiently compute this list we can do simple iteration
160 my @l = split //, $args{String};
161 print "Length: ".(scalar @l)."\n" if $self->Verbose;
162 for (my $i = 0; $i < scalar @l; ++$i) {
163 if (!($i % 1000)) {
164 print "." if $self->Verbose;
165 }
166 if (!($i % 50000)) {
167 print "\n" if $self->Verbose;
168 }
169 for (my $j = $i + 1; $j <= Min(Items => [$i + $args{Size},scalar @l]); ++$j) {
170 $occurances->{substr($args{String},$i,$j-$i)}->{$i} = 1;
171 }
172 }
173 print "\n" if $self->Verbose;
174 return $occurances;
175 }
176  
177 sub ExtractMostUsefulTerms {
178 my ($self,%args) = @_;
179 print "Extracting most useful terms\n" if $self->Verbose;
180 my $occurances = $args{Occurances};
181 my $res;
182 my $usefulness = {};
183 my @list;
184 foreach my $k1 (keys %$occurances) {
185 if (scalar keys %{$occurances->{$k1}} > 1) {
186 $res->{$k1} = $occurances->{$k1};
187 }
188 }
189 my $count = 0;
190 my $ilist = {};
191 foreach my $k1 (sort {
192 (scalar keys %{$res->{$b}}) * length($b) <=>
193 (scalar keys %{$res->{$a}}) * length($a)}
194 keys %$res) {
195 my $x = (scalar keys %{$res->{$k1}})*length($k1);
196 # print $x."\t{{{".$k1."}}}\n";
197 push @list, $k1;
198 $ilist->{$k1} = $count++;
199 $usefulness->{$k1} = $x;
200 }
201 return {List => \@list,
202 Ilist => $ilist,
203 Usefulness => $usefulness};
204 }
205  
206 sub ExtractRegexFromCycles {
207 my ($self,%args) = @_;
208 # take the cycles, extract longest common subsequences check rest of the sequences
209 # take 3 random cycles
210  
211 my $size = scalar @{$args{Cycles}};
212 my @l = 0..($size - 1);
213 my @n;
214 for (my $i = 0; $i < 8; ++$i) {
215 my $index = int rand scalar @l;
216 push @n, $l[$index];
217 $l[$index] = $l[scalar @l - 1];
218 pop @l;
219 }
220  
221 # now for each of these cycles
222 # compute the lengths of the cycles and exit
223 my @regexes;
224 foreach my $index (@n) {
225 # print $index."\n";
226 my $newhash = {};
227 foreach my $hash (@{$args{Cycles}->[$index]}) {
228 foreach my $key (keys %$hash) {
229 $newhash->{$key} = $hash->{$key};
230 }
231 }
232 my $length = (scalar keys %$newhash);
233 my $regex = $self->GenerateRegexFromCycle
234 (
235 Cycle => $args{Cycles}->[$index],
236 TokenList => $args{TokenList},
237 Index => $args{Index},
238 );
239 # print $regex."\n\n\n";
240 push @regexes, $regex;
241 }
242  
243 # print Dumper(\@regexes);
244  
245 my $regex = shift @regexes;
246 while (@regexes) {
247 my $r2 = shift @regexes;
248 $regex = join("",LCS([split //,$regex], [split //,$r2]));
249 }
250  
251 $regex =~ s/\\{1}//g; # IS THIS REALLY NECESSARY?
252 return $regex;
253 }
254  
255 sub GenerateRegexFromCycle {
256 my ($self,%args) = @_;
257 # print Dumper(\%args);
258 # exit(0);
259 my @string;
260 my $last;
261 foreach my $hash (@{$args{Cycle}}) {
262 foreach my $string (keys %$hash) {
263 my $x = 0;
264 foreach my $char (split //,$string) {
265 $string[$hash->{$string} - $args{TokenList}->[$args{Index}] + $x++] = $char;
266 $last = $hash->{$string};
267 }
268 }
269 }
270  
271 # splice off anything after last
272 splice(@string,$last - $args{TokenList}->[$args{Index}] + 1);
273  
274 # print Dumper(\@string);
275  
276 my @constant;
277 my $r = [];
278 my $undefperiod = 0;
279  
280 # See({String => \@string});
281  
282 foreach my $element (@string) {
283 if (! defined $element) {
284 if ($undefperiod) {
285 # just move along
286 } else {
287 $undefperiod = 1;
288 if (@constant) {
289 my @cp = @constant;
290 push @$r,\@cp;
291 @constant = ();
292 }
293 push @$r,undef;
294 }
295 } else {
296 $undefperiod = 0;
297 push @constant, $element;
298 }
299 }
300 if (@constant) {
301 my @cp = @constant;
302 push @$r, \@cp;
303 }
304  
305 # now print this structure
306 # print Dumper($r);
307  
308 my $regex;
309 foreach my $list (@$r) {
310 if (defined $list) {
311 my $constant = join("",@$list);
312 $constant =~ s/ /\r/g;
313 $constant =~ s/([^\w\s\n])/\\$1/g;
314 # $constant =~ s/\n/\\n/g;
315 $regex .= $constant;
316 } else {
317 $regex .= "(.*)";
318 }
319 }
320 return $regex;
321 }
322  
323 # how to extract valid patterns
324  
325 # the longer the pattern in terms of numbers of entries the better
326 # the more often the pattern repeats
327  
328 # we can search over pattern space, pruning by the various factors
329  
330 # a pattern consists of a repeating sequence of tokens
331  
332 # first search by the largest and most useful tokens
333  
334  
335 # detecting repeating subsequences optimized over sequence value
336  
337 # frequency window (prune by frequency for starters)
338  
339 # lower bound
340 # upper bound
341 # sequence
342  
343 # detect all repeating subsequences of 2 items,
344 # merge
345  
346 1;