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