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
|
#!/usr/bin/env perl
# This script is a very simple prototype to learn fann from rspamd logs
# For now, it is intended for internal use only
use strict;
use warnings FATAL => 'all';
use AI::FANN qw(:all);
use Getopt::Std;
my %sym_idx; # Symbols by index
my %sym_names; # Symbols by name
my $num = 1; # Number of symbols
my @spam;
my @ham;
my $max_samples = -1;
my $split = 1;
my $preprocessed = 0; # output is in format <score>:<0|1>:<SYM1,...SYMN>
my $score_spam = 12;
my $score_ham = -6;
sub process {
my ( $input, $spam, $ham ) = @_;
my $samples = 0;
while (<$input>) {
if ( !$preprocessed ) {
if (/^.*rspamd_task_write_log.*: \[(-?\d+\.?\d*)\/(\d+\.?\d*)\]\s*\[(.+)\].*$/) {
if ( $1 > $score_spam ) {
$_ = "$1:1: $3";
}
elsif ( $1 < $score_ham ) {
$_ = "$1:0: $3\n";
}
else {
# Out of boundary
next;
}
}
else {
# Not our log message
next;
}
}
$_ =~ /^(-?\d+\.?\d*):([01]):\s*(\S.*)$/;
my $is_spam = 0;
if ( $2 == 1 ) {
$is_spam = 1;
}
my @ar = split /,/, $3;
my %sample;
foreach my $sym (@ar) {
chomp $sym;
if ( !$sym_idx{$sym} ) {
$sym_idx{$sym} = $num;
$sym_names{$num} = $sym;
$num++;
}
$sample{ $sym_idx{$sym} } = 1;
}
if ($is_spam) {
push @{$spam}, \%sample;
}
else {
push @{$ham}, \%sample;
}
$samples++;
if ( $max_samples > 0 && $samples > $max_samples ) {
return;
}
}
}
# Shuffle array
sub fisher_yates_shuffle {
my $array = shift;
my $i = @$array;
while ( --$i ) {
my $j = int rand( $i + 1 );
@$array[ $i, $j ] = @$array[ $j, $i ];
}
}
# Train network
sub train {
my ( $ann, $sample, $result ) = @_;
my @row;
for ( my $i = 1 ; $i < $num ; $i++ ) {
if ( $sample->{$i} ) {
push @row, 1;
}
else {
push @row, 0;
}
}
#print "@row -> @{$result}\n";
$ann->train( \@row, \@{$result} );
}
sub test {
my ( $ann, $sample ) = @_;
my @row;
for ( my $i = 1 ; $i < $num ; $i++ ) {
if ( $sample->{$i} ) {
push @row, 1;
}
else {
push @row, 0;
}
}
my $ret = $ann->run( \@row );
return $ret;
}
my %opts;
getopts( 'o:i:s:n:t:hpS:H:', \%opts );
if ( $opts{'h'} ) {
print "$0 [-i input] [-o output] [-s scores] [-n max_samples] [-S spam_score] [-H ham_score] [-ph]\n";
exit;
}
my $input = *STDIN;
if ( $opts{'i'} ) {
open( $input, '<', $opts{'i'} ) or die "cannot open $opts{i}";
}
if ( $opts{'n'} ) {
$max_samples = $opts{'n'};
}
if ( $opts{'t'} ) {
# Test split
$split = $opts{'t'};
}
if ( $opts{'p'} ) {
$preprocessed = 1;
}
if ( $opts{'H'} ) {
$score_ham = $opts{'H'};
}
if ( $opts{'S'} ) {
$score_spam = $opts{'S'};
}
# ham_prob, spam_prob
my @spam_out = (1);
my @ham_out = (0);
process( $input, \@spam, \@ham );
fisher_yates_shuffle( \@spam );
fisher_yates_shuffle( \@ham );
my $nspam = int( scalar(@spam) / $split );
my $nham = int( scalar(@ham) / $split );
my $ann = AI::FANN->new_standard( $num - 1, ( $num + 2 ) / 2, 1 );
my @train_data;
# Train ANN
for ( my $i = 0 ; $i < $nham ; $i++ ) {
push @train_data, [ $ham[$i], \@ham_out ];
}
for ( my $i = 0 ; $i < $nspam ; $i++ ) {
push @train_data, [ $spam[$i], \@spam_out ];
}
fisher_yates_shuffle( \@train_data );
foreach my $train_row (@train_data) {
train( $ann, @{$train_row}[0], @{$train_row}[1] );
}
print "Trained $nspam SPAM and $nham HAM samples\n";
# Now run fann
if ( $split > 1 ) {
my $sample = 0.0;
my $correct = 0.0;
for ( my $i = $nham ; $i < $nham * $split ; $i++ ) {
my $ret = test( $ann, $ham[$i] );
#print "@{$ret}\n";
if ( @{$ret}[0] < 0.5 ) {
$correct++;
}
$sample++;
}
print "Tested $sample HAM samples, correct matched: $correct, rate: " . ( $correct / $sample ) . "\n";
$sample = 0.0;
$correct = 0.0;
for ( my $i = $nspam ; $i < $nspam * $split ; $i++ ) {
my $ret = test( $ann, $spam[$i] );
#print "@{$ret}\n";
if ( @{$ret}[0] > 0.5 ) {
$correct++;
}
$sample++;
}
print "Tested $sample SPAM samples, correct matched: $correct, rate: " . ( $correct / $sample ) . "\n";
}
if ( $opts{'o'} ) {
$ann->save( $opts{'o'} ) or die "cannot save ann into $opts{o}";
}
if ( $opts{'s'} ) {
open( my $scores, '>', $opts{'s'} ) or die "cannot open score file $opts{'s'}";
print $scores "{";
for ( my $i = 1 ; $i < $num ; $i++ ) {
my $n = $i - 1;
if ( $i != $num - 1 ) {
print $scores "\"$sym_names{$i}\":$n,";
}
else {
print $scores "\"$sym_names{$i}\":$n}\n";
}
}
}
|