-
Notifications
You must be signed in to change notification settings - Fork 4
/
logcluster.pl
executable file
·2051 lines (1624 loc) · 63.3 KB
/
logcluster.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# LogCluster 0.10 - logcluster.pl
# Copyright (C) 2015-2019 Risto Vaarandi
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
package main::LogCluster;
sub compile_func {
my($code) = $_[0];
my($ret, $error);
$ret = eval $code;
if ($@) {
$error = $@;
chomp $error;
return (0, $error);
} elsif (ref($ret) ne "CODE") {
return (0, "eval did not return a code reference");
} else {
return (1, $ret);
}
}
package main;
use strict;
no warnings 'recursion';
use vars qw(
$USAGE
$aggrsup
$ansicoloravail
%candidates
%clusters
$color
$color1
$color2
$csize
@csketch
$debug
$facility
$fpat
%fword_deps
%fwords
%gwords
$help
$ifile
%ifiles
@inputfilepat
@inputfiles
$lcfunc
$lcfuncptr
$lfilter
$lineregexp
$outlierfile
%outlierpat
$progname
$ptree
$ptreesize
$readdump
$readwords
$rsupport
$searchregexp
$separator
$sepregexp
$support
$syslogavail
$syslogopen
$template
$totalinputlines
$version
$wcfunc
$wcfuncptr
@weightfunction
$weightf
$wfileint
$wfilter
$wfreq
$wildcard
$wordregexp
$wordsonly
$wreplace
$writedump
$writewords
$wsearch
$wsize
@wsketch
$wweight
);
use Getopt::Long;
use Digest::MD5 qw(md5);
use Storable;
$ansicoloravail = eval { require Term::ANSIColor };
$syslogavail = eval { require Sys::Syslog };
######################### Functions #########################
# This function logs the message given with parameter2,..,parameterN to
# syslog, using the level parameter1. The message is also written to stderr.
sub log_msg {
my($level) = shift(@_);
my($msg) = join(" ", @_);
print STDERR scalar(localtime()), ": $msg\n";
if ($syslogopen) { Sys::Syslog::syslog($level, $msg); }
}
# This function compiles the function given with parameter1, returning
# a function pointer if the compilation is successful, and undef otherwise
sub compile_func_wrapper {
my($code) = $_[0];
my($ok, $value);
($ok, $value) = main::LogCluster::compile_func($code);
if ($ok) { return $value; }
log_msg("err", "Failed to compile the code '$code':", $value);
return undef;
}
# This function hashes the string given with parameter1 to an integer
# in the range (0...$wsize-1) and returns the integer. The $wsize integer
# can be set with the --wsize command line option.
sub hash_string {
return unpack('L', md5($_[0])) % $wsize;
}
# This function hashes the candidate ID given with parameter1 to an integer
# in the range (0...$csize-1) and returns the integer. The $csize integer
# can be set with the --csize command line option.
sub hash_candidate {
return unpack('L', md5($_[0])) % $csize;
}
# This function matches the line given with parameter1 with a regular
# expression $lineregexp (the expression can be set with the --lfilter
# command line option). If the $template string is defined (can be set
# with the --template command line option), the line is converted
# according to $template (match variables in $template are substituted
# with values from regular expression match, and the resulting string
# replaces the line). If the regular expression $lineregexp does not match
# the line, 0 is returned, otherwise the line (or converted line, if
# --template option has been given) is returned.
# If the --lfilter option has not been given but --lcfunc option is
# present, the Perl function given with --lcfunc is used for matching
# and converting the line. If the function returns 'undef', line is
# regarded non-matching, otherwise the value returned by the function
# replaces the original line.
# If neither --lfilter nor --lcfunc option has been given, the line
# is returned without a trailing newline.
sub process_line {
my($line) = $_[0];
my(%matches, @matches, $match, $i);
chomp($line);
if (defined($lfilter)) {
if (!defined($template)) {
if ($line =~ /$lineregexp/) { return $line; } else { return undef; }
}
if (@matches = ($line =~ /$lineregexp/)) {
%matches = %+;
$matches{"0"} = $line;
$i = 1;
foreach $match (@matches) { $matches{$i++} = $match; }
$line = $template;
$line =~ s/\$(?:\$|(\d+)|\{(\d+)\}|\+\{(\w+)\})/
!defined($+)?'$':(defined($matches{$+})?$matches{$+}:'')/egx;
return $line;
}
return undef;
} elsif (defined($lcfunc)) {
$line = eval { $lcfuncptr->($line) };
return $line;
} else {
return $line;
}
}
# This function opens input file and returns a file handle for opened
# file; if the open fails, the function will call exit(1)
sub open_input_file {
my($file) = $_[0];
my($handle);
if ($file eq "-") {
if (!open($handle, "<&STDIN")) {
log_msg("err", "Can't dup standard input: $!");
exit(1);
}
} elsif (!open($handle, $file)) {
log_msg("err", "Can't open input file $file: $!");
exit(1);
}
return $handle;
}
# This function makes a pass over the data set and builds the sketch
# @wsketch which is used for finding frequent words. The sketch contains
# $wsize counters ($wsize can be set with --wsize command line option).
sub build_word_sketch {
my($index, $ifile, $line, $word, $word2, $i, $fh);
my(@words, @words2, %words);
for ($index = 0; $index < $wsize; ++$index) { $wsketch[$index] = 0; }
$i = 0;
foreach $ifile (@inputfiles) {
$fh = open_input_file($ifile);
while (<$fh>) {
$line = process_line($_);
if (!defined($line)) { next; }
++$i;
@words = split(/$sepregexp/, $line);
%words = map { $_ => 1 } @words;
@words = keys %words;
foreach $word (@words) {
$index = hash_string($word);
++$wsketch[$index];
if (defined($wfilter) && $word =~ /$wordregexp/) {
$word =~ s/$searchregexp/$wreplace/g;
$index = hash_string($word);
++$wsketch[$index];
} elsif (defined($wcfunc)) {
@words2 = eval { $wcfuncptr->($word) };
foreach $word2 (@words2) {
if (!defined($word2)) { next; }
$index = hash_string($word2);
++$wsketch[$index];
}
}
}
}
close($fh);
}
if (!defined($support)) {
$support = int($rsupport * $i / 100);
log_msg("info", "Total $i lines read from input sources, using absolute support $support (relative support $rsupport percent)");
}
$i = 0;
for ($index = 0; $index < $wsize; ++$index) {
if ($wsketch[$index] >= $support) { ++$i; }
}
log_msg("info", "Word sketch successfully built, $i buckets >= $support");
}
# This function makes a pass over the data set, finds frequent words and
# stores them to %fwords hash table. The function returns the total number
# of lines in input file(s).
sub find_frequent_words {
my($ifile, $line, $word, $word2, $index, $i, $fh);
my(@words, @words2, %words);
$i = 0;
foreach $ifile (@inputfiles) {
$fh = open_input_file($ifile);
while (<$fh>) {
$line = process_line($_);
if (!defined($line)) { next; }
++$i;
@words = split(/$sepregexp/, $line);
%words = map { $_ => 1 } @words;
@words = keys %words;
if (defined($wsize)) {
foreach $word (@words) {
$index = hash_string($word);
if ($wsketch[$index] >= $support) { ++$fwords{$word}; }
if (defined($wfilter) && $word =~ /$wordregexp/) {
$word =~ s/$searchregexp/$wreplace/g;
$index = hash_string($word);
if ($wsketch[$index] >= $support) { ++$fwords{$word}; }
} elsif (defined($wcfunc)) {
@words2 = eval { $wcfuncptr->($word) };
foreach $word2 (@words2) {
if (!defined($word2)) { next; }
$index = hash_string($word2);
if ($wsketch[$index] >= $support) { ++$fwords{$word2}; }
}
}
}
} else {
foreach $word (@words) {
++$fwords{$word};
if (defined($wfilter) && $word =~ /$wordregexp/) {
$word =~ s/$searchregexp/$wreplace/g;
++$fwords{$word};
} elsif (defined($wcfunc)) {
@words2 = eval { $wcfuncptr->($word) };
foreach $word2 (@words2) {
if (!defined($word2)) { next; }
++$fwords{$word2};
}
}
}
}
}
close($fh);
}
if (!defined($support)) {
$support = int($rsupport * $i / 100);
log_msg("info", "Total $i lines read from input sources, using absolute support $support (relative support $rsupport percent)");
}
foreach $word (keys %fwords) {
if ($fwords{$word} < $support) { delete $fwords{$word}; }
}
if ($debug) {
foreach $word (sort { $fwords{$b} <=> $fwords{$a} } keys %fwords) {
log_msg("debug", "Frequent word: $word -- occurs in",
$fwords{$word}, "lines");
}
}
log_msg("info", "Total number of frequent words:", scalar(keys %fwords));
return $i;
}
# This function reads frequent words from a text file without making a pass
# over the data set, and stores them to %fwords hash table.
sub read_frequent_words {
my($ref);
if (defined($wfileint)) {
$ref = retrieve($readwords);
%fwords = %{$ref->{"FrequentWords"}};
} else {
if (!open(WORDFILE, $readwords)) {
log_msg("err", "Can't open word file $readwords: $!");
exit(1);
}
while (<WORDFILE>) {
chomp;
$fwords{$_} = 1;
}
close(WORDFILE);
}
log_msg("info", "Total number of frequent words:", scalar(keys %fwords),
"(read from $readwords)");
}
# This function writes frequent words (%fwords hash table) and their
# relative supports into a file.
sub write_frequent_words {
my($lines) = $_[0];
my(%rsupports, $word);
if (defined($wfileint)) {
if ($lines) {
foreach $word (keys %fwords) {
$rsupports{$word} = $fwords{$word} / $lines;
}
}
store({ "FrequentWords" => \%fwords,
"RelativeSupports" => \%rsupports }, $writewords);
} else {
if (!open(WORDFILE, ">$writewords")) {
log_msg("err", "Can't open word file $writewords: $!");
exit(1);
}
foreach $word (keys %fwords) { print WORDFILE "$word\n"; }
close(WORDFILE);
}
log_msg("info", scalar(keys %fwords),
"frequent words written to $writewords");
}
# This function makes a pass over the data set and builds the sketch
# @csketch which is used for finding frequent candidates. The sketch contains
# $csize counters ($csize can be set with --csize command line option).
sub build_candidate_sketch {
my($ifile, $line, $word, $word2, $candidate, $index, $i, $fh);
my(@words, @words2, @candidate);
for ($index = 0; $index < $csize; ++$index) { $csketch[$index] = 0; }
$i = 0;
foreach $ifile (@inputfiles) {
$fh = open_input_file($ifile);
while (<$fh>) {
$line = process_line($_);
if (!defined($line)) { next; }
++$i;
@words = split(/$sepregexp/, $line);
@candidate = ();
foreach $word (@words) {
if (exists($fwords{$word})) {
push @candidate, $word;
} elsif (defined($wfilter) && $word =~ /$wordregexp/) {
$word =~ s/$searchregexp/$wreplace/g;
if (exists($fwords{$word})) {
push @candidate, $word;
}
} elsif (defined($wcfunc)) {
@words2 = eval { $wcfuncptr->($word) };
foreach $word2 (@words2) {
if (!defined($word2)) { next; }
if (exists($fwords{$word2})) {
push @candidate, $word2;
last;
}
}
}
}
if (scalar(@candidate)) {
$candidate = join("\n", @candidate);
$index = hash_candidate($candidate);
++$csketch[$index];
}
}
close($fh);
}
# if support has not been identified yet (e.g., frequent words were loaded
# from file), calculate the support
if (!defined($support)) {
$support = int($rsupport * $i / 100);
log_msg("info", "Total $i lines read from input sources, using absolute support $support (relative support $rsupport percent)");
}
$i = 0;
for ($index = 0; $index < $csize; ++$index) {
if ($csketch[$index] >= $support) { ++$i; }
}
log_msg("info", "Candidate sketch successfully built, $i buckets >= $support");
}
# This function logs the description for candidate parameter1.
sub print_candidate {
my($candidate) = $_[0];
my($i, $msg);
$msg = "Cluster candidate with support " .
$candidates{$candidate}->{"Count"} . ": ";
for ($i = 0; $i < $candidates{$candidate}->{"WordCount"}; ++$i) {
if ($candidates{$candidate}->{"Vars"}->[$i]->[1] > 0) {
$msg .= "*{" . $candidates{$candidate}->{"Vars"}->[$i]->[0] . "," .
$candidates{$candidate}->{"Vars"}->[$i]->[1] . "} ";
}
$msg .= $candidates{$candidate}->{"Words"}->[$i] . " ";
}
if ($candidates{$candidate}->{"Vars"}->[$i]->[1] > 0) {
$msg .= "*{" . $candidates{$candidate}->{"Vars"}->[$i]->[0] . "," .
$candidates{$candidate}->{"Vars"}->[$i]->[1] . "}";
}
log_msg("debug", $msg);
}
# This function makes a pass over the data set, identifies cluster candidates
# and stores them to %candidates hash table. If the --wweight command line
# option has been provided, dependencies between frequent words are also
# identified during the data pass and stored to %fword_deps hash table.
sub find_candidates {
my($ifile, $line, $word, $word2, $varnum, $candidate, $index, $total, $i);
my(@words, @words2, %words, @candidate, @vars, $linecount, $fh, $n);
$linecount = 0;
foreach $ifile (@inputfiles) {
$fh = open_input_file($ifile);
while (<$fh>) {
$line = process_line($_);
if (!defined($line)) { next; }
++$linecount;
@words = split(/$sepregexp/, $line);
@candidate = ();
@vars = ();
$varnum = 0;
foreach $word (@words) {
if (exists($fwords{$word})) {
push @candidate, $word;
push @vars, $varnum;
$varnum = 0;
} elsif (defined($wfilter) && $word =~ /$wordregexp/) {
$word =~ s/$searchregexp/$wreplace/g;
if (exists($fwords{$word})) {
push @candidate, $word;
push @vars, $varnum;
$varnum = 0;
} else {
++$varnum;
}
} elsif (defined($wcfunc)) {
@words2 = eval { $wcfuncptr->($word) };
$i = 0;
foreach $word2 (@words2) {
if (!defined($word2)) { next; }
if (exists($fwords{$word2})) {
push @candidate, $word2;
push @vars, $varnum;
$varnum = 0;
$i = 1;
last;
}
}
if (!$i) { ++$varnum; }
} else {
++$varnum;
}
}
push @vars, $varnum;
if (scalar(@candidate)) {
$candidate = join("\n", @candidate);
# if the candidate sketch has been created previously, check the
# sketch bucket that corresponds to the candidate, and if it is
# smaller than support threshold, don't consider the candidate
if (defined($csize)) {
$index = hash_candidate($candidate);
if ($csketch[$index] < $support) { next; }
}
# if --wweight option was given, store word dependency information
# (word co-occurrence counts) to %fword_deps
if (defined($wweight)) {
%words = map { $_ => 1 } @candidate;
@words = keys %words;
foreach $word (@words) {
foreach $word2 (@words) { ++$fword_deps{$word}->{$word2}; }
}
}
# if the given candidate already exists, increase its support and
# adjust its wildcard information, otherwise create a new candidate
if (!exists($candidates{$candidate})) {
$candidates{$candidate} = {};
$candidates{$candidate}->{"Words"} = [ @candidate ];
$candidates{$candidate}->{"WordCount"} = scalar(@candidate);
$candidates{$candidate}->{"Vars"} = [];
for $varnum (@vars) {
push @{$candidates{$candidate}->{"Vars"}}, [ $varnum, $varnum];
}
$candidates{$candidate}->{"Count"} = 1;
} else {
$total = scalar(@vars);
for ($index = 0; $index < $total; ++$index) {
if ($candidates{$candidate}->{"Vars"}->[$index]->[0]
> $vars[$index]) {
$candidates{$candidate}->{"Vars"}->[$index]->[0] = $vars[$index];
}
elsif ($candidates{$candidate}->{"Vars"}->[$index]->[1]
< $vars[$index]) {
$candidates{$candidate}->{"Vars"}->[$index]->[1] = $vars[$index];
}
}
++$candidates{$candidate}->{"Count"};
}
}
}
close($fh);
}
# if support has not been identified yet (e.g., frequent words were loaded
# from file), calculate the support
if (!defined($support)) {
$support = int($rsupport * $linecount / 100);
log_msg("info", "Total $linecount lines read from input sources, using absolute support $support (relative support $rsupport percent)");
}
# If --wweight option was given, convert word dependency information
# (word co-occurrence counts) into range 0..1.
if (defined($wweight)) {
$i = 0;
foreach $word (keys %fwords) {
# since %fwords hash can be initialized with frequent words loaded
# from file, %fword_deps might not contain information for a frequent
# word if it has not been observed in input file
if (!exists($fword_deps{$word})) { next; }
# Note that $fword_deps{$word}->{$word} equals to the occurrence count
# of $word. Since %fwords hash can be initialized with frequent words
# loaded from file, $fword_deps{$word}->{$word} is used in calculations
# instead of $fwords{$word}.
$n = $fword_deps{$word}->{$word};
# convert word dependency information into range 0..1
foreach $word2 (keys %{$fword_deps{$word}}) {
$fword_deps{$word}->{$word2} /= $n;
++$i;
if ($debug) {
log_msg("debug", "Dependency $word -> $word2:",
$fword_deps{$word}->{$word2});
}
}
}
log_msg("info", "Total number of frequent word dependencies:", $i);
}
if ($debug) {
foreach $candidate (sort { $candidates{$b}->{"Count"} <=>
$candidates{$a}->{"Count"} } keys %candidates) {
print_candidate($candidate);
}
}
log_msg("info", "Total number of candidates:", scalar(keys %candidates));
}
# This function finds frequent candidates by removing candidates with
# insufficient support from the %candidates hash table.
sub find_frequent_candidates {
my($candidate);
foreach $candidate (keys %candidates) {
if ($candidates{$candidate}->{"Count"} < $support) {
delete $candidates{$candidate};
}
}
log_msg("info", "Total number of frequent candidates:",
scalar(keys %candidates));
}
# This function inserts a candidate into the prefix tree
sub insert_into_prefix_tree {
my($node, $cand, $i) = @_;
my($label);
if ($i == $candidates{$cand}->{"WordCount"}) {
$label = $candidates{$cand}->{"Vars"}->[$i]->[0] . "\n" .
$candidates{$cand}->{"Vars"}->[$i]->[1];
} else {
$label = $candidates{$cand}->{"Vars"}->[$i]->[0] . "\n" .
$candidates{$cand}->{"Vars"}->[$i]->[1] . "\n" .
$candidates{$cand}->{"Words"}->[$i];
}
if (!exists($node->{"Children"}->{$label})) {
$node->{"Children"}->{$label} = {};
$node = $node->{"Children"}->{$label};
$node->{"Min"} = $candidates{$cand}->{"Vars"}->[$i]->[0];
$node->{"Max"} = $candidates{$cand}->{"Vars"}->[$i]->[1];
if ($i < $candidates{$cand}->{"WordCount"}) {
$node->{"Children"} = {};
$node->{"Word"} = $candidates{$cand}->{"Words"}->[$i];
} else {
$node->{"Candidate"} = $cand;
}
++$ptreesize;
} else {
$node = $node->{"Children"}->{$label};
}
if ($i < $candidates{$cand}->{"WordCount"}) {
insert_into_prefix_tree($node, $cand, $i + 1);
}
}
# This function arranges all candidates into the prefix tree data structure,
# in order to facilitate fast matching between candidates
sub build_prefix_tree {
my($cand);
$ptree = { Children => {} };
$ptreesize = 0;
foreach $cand (keys %candidates) {
insert_into_prefix_tree($ptree, $cand, 0);
}
log_msg("info", "Total number of nodes in prefix tree:", $ptreesize);
}
# This function finds more specific candidates for the given candidate with
# the help of the prefix tree, and records more specific candidates into
# the SubClusters hash table of the given candidate
sub find_more_specific {
my($node, $cand, $i, $min, $max) = @_;
my($candidate, $children, $child, $cand2);
my($candmin, $candmax);
$candidate = $candidates{$cand};
$candmin = $candidate->{"Vars"}->[$i]->[0];
$candmax = $candidate->{"Vars"}->[$i]->[1];
$children = $node->{"Children"};
foreach $child (keys %{$children}) {
$node = $children->{$child};
if ($i == $candidate->{"WordCount"}) {
if (exists($node->{"Candidate"})) {
if ($candmin > $node->{"Min"} + $min ||
$candmax < $node->{"Max"} + $max) { next; }
$cand2 = $node->{"Candidate"};
if ($cand ne $cand2) {
$candidate->{"SubClusters"}->{$cand2} = 1;
}
} else {
find_more_specific($node, $cand, $i, $min + $node->{"Min"} + 1,
$max + $node->{"Max"} + 1);
}
next;
}
if (exists($node->{"Candidate"})) { next; }
if ($candmax < $node->{"Max"} + $max) { next; }
if ($candmin > $node->{"Min"} + $min ||
$candidate->{"Words"}->[$i] ne $node->{"Word"}) {
find_more_specific($node, $cand, $i, $min + $node->{"Min"} + 1,
$max + $node->{"Max"} + 1);
next;
}
find_more_specific($node, $cand, $i + 1, 0, 0);
find_more_specific($node, $cand, $i, $min + $node->{"Min"} + 1,
$max + $node->{"Max"} + 1);
}
}
# This function scans all cluster candidates (stored in %candidates hash
# table), and for each candidate X it finds all candidates Y1,..,Yk which
# represent more specific line patterns. After finding such clusters Yi
# for each X, the supports of Yi are added to the support of each X.
# For speeding up the process, previously created prefix tree is used.
# In order to facilitate the detection of outliers, for each X with sufficient
# support the clusters Yi are stored to %outlierpat hash table (this allows
# for fast detection of non-outliers which match X).
sub aggregate_supports {
my(@keys, @keys2, $cand, $cand2);
@keys = keys %candidates;
foreach $cand (@keys) {
$candidates{$cand}->{"OldCount"} = $candidates{$cand}->{"Count"};
$candidates{$cand}->{"Count2"} = $candidates{$cand}->{"Count"};
$candidates{$cand}->{"SubClusters"} = {};
find_more_specific($ptree, $cand, 0, 0, 0);
@keys2 = keys %{$candidates{$cand}->{"SubClusters"}};
foreach $cand2 (@keys2) {
$candidates{$cand}->{"Count2"} += $candidates{$cand2}->{"Count"};
}
}
foreach $cand (@keys) {
$candidates{$cand}->{"Count"} = $candidates{$cand}->{"Count2"};
@keys2 = keys %{$candidates{$cand}->{"SubClusters"}};
if (scalar(@keys2)) {
if (defined($outlierfile) && $candidates{$cand}->{"Count"} >= $support) {
foreach $cand2 (@keys2) { $outlierpat{$cand2} = 1; }
}
if ($debug) {
log_msg("debug",
"The support of the following candidate was increased from",
$candidates{$cand}->{"OldCount"}, "to",
$candidates{$cand}->{"Count"});
print_candidate($cand);
log_msg("debug", "with the following candidates being more specific:");
foreach $cand2 (@keys2) {
print_candidate($cand2);
log_msg("debug", "(original support:",
$candidates{$cand2}->{"OldCount"}, ")");
}
log_msg("debug", "----------------------------------------");
}
}
}
}
# This function makes a pass over the data set, find outliers and stores them
# to file $outlierfile (can be set with the --outliers command line option).
sub find_outliers {
my($ifile, $line, $word, $word2, $candidate, $i, $fh);
my(@words, @words2, @candidate);
if (!open(OUTLIERFILE, ">$outlierfile")) {
log_msg("err", "Can't open outlier file $outlierfile: $!");
exit(1);
}
$i = 0;
foreach $ifile (@inputfiles) {
$fh = open_input_file($ifile);
while (<$fh>) {
$line = process_line($_);
if (!defined($line)) { next; }
@words = split(/$sepregexp/, $line);
@candidate = ();
foreach $word (@words) {
if (exists($fwords{$word})) {
push @candidate, $word;
} elsif (defined($wfilter) && $word =~ /$wordregexp/) {
$word =~ s/$searchregexp/$wreplace/g;
if (exists($fwords{$word})) {
push @candidate, $word;
}
} elsif (defined($wcfunc)) {
@words2 = eval { $wcfuncptr->($word) };
foreach $word2 (@words2) {
if (!defined($word2)) { next; }
if (exists($fwords{$word2})) {
push @candidate, $word2;
last;
}
}
}
}
if (scalar(@candidate)) {
$candidate = join("\n", @candidate);
if (exists($candidates{$candidate})) { next; }
if (defined($aggrsup) && exists($outlierpat{$candidate})) { next; }
}
print OUTLIERFILE $_;
++$i;
}
close($fh);
}
close(OUTLIERFILE);
log_msg("info", "Total number of outliers:", $i);
}
# This function inspects the cluster candidate parameter1 and finds the weight
# of each word in the candidate description. The weights are calculated from
# word dependency information according to --weightf=1.
sub find_weights {
my($candidate) = $_[0];
my($ref, $total, $word, $word2, $weight);
$ref = $candidates{$candidate}->{"Words"};
$total = $candidates{$candidate}->{"WordCount"};
$candidates{$candidate}->{"Weights"} = [];
foreach $word (@{$ref}) {
$weight = 0;
foreach $word2 (@{$ref}) { $weight += $fword_deps{$word2}->{$word}; }
push @{$candidates{$candidate}->{"Weights"}}, $weight / $total;
}
}
# This function inspects the cluster candidate parameter1 and finds the weight
# of each word in the candidate description. The weights are calculated from
# word dependency information according to --weightf=2.
sub find_weights2 {
my($candidate) = $_[0];
my($ref, $total, $word, $word2);
my(%weights, @words);
$ref = $candidates{$candidate}->{"Words"};
$candidates{$candidate}->{"Weights"} = [];
%weights = map { $_ => 0 } @{$ref};
@words = keys %weights;
$total = scalar(@words) - 1;
foreach $word (@words) {
if (!$total) {
$weights{$word} = 1;
last;
}