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
|
#!/bin/bash
#
# Copyright (C) 2002 Laird Breyer
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Laird Breyer <laird@lbreyer.com>
#
PROGNAME="$0"
PROGNAME2=`basename $0`
VERSION="$PROGNAME version @VERSION@\nTrain On Error simulator"
MXDIR="$PWD/mailtoe.d"
ALOG="$MXDIR/log/activity.log"
CLOG="$MXDIR/log/toe.log"
SLOG="$MXDIR/log/summary.log"
TEMPDIR="$MXDIR/tmp"
BOOTSTRAP="@PKGDATADIR@/testsuite"
FILTERS="$MXDIR/filters"
# use this for debugging
# set -x
usage() {
echo "
Usage:
$PROGNAME2 prepare size
$PROGNAME2 add category [MBOX]...
$PROGNAME2 run
$PROGNAME2 summarize [LEVEL]
$PROGNAME2 plot [ps|logscale]
$PROGNAME2 review TRUECAT PREDCAT
$PROGNAME2 clean
$PROGNAME2 killall
$PROGNAME2 testsuite select [FILTER]...
$PROGNAME2 testsuite deselect [FILTER]...
$PROGNAME2 testsuite list
$PROGNAME2 testsuite status
$PROGNAME2 testsuite run [plots]
$PROGNAME2 testsuite summarize
"
exit 1
}
mbox_multiplex() {
perl -e '
# usage: $0 seqno seed [MBOX]... -s COMMAND ARGS
use strict;
$SIG{PIPE} = "IGNORE";
my %mbox;
my %line;
my $cmd = "";
my $args = "";
my $n = shift;
srand shift;
foreach my $g (@ARGV) {
if( "$g" eq "-s" ) {
$cmd = "|";
} elsif( $cmd ne "" ) {
$cmd .= " $g";
} else {
if( open($mbox{$g}, "<$g") ) {
$args .= " $g";
} else {
delete $mbox{$g};
}
}
}
$args =~ s/\.mbox//g;
$args =~ s|/mbox/|/$n/|g;
$| = 1;
while( scalar keys %mbox > 0 ) {
my $j = int(rand scalar keys %mbox);
foreach my $f (keys %mbox) {
if( $j-- <= 0 ) {
if( eof($mbox{$f}) ) {
close($mbox{$f});
delete $mbox{$f};
} else {
my $c = $f;
$c =~ s|\.mbox$||;
$c =~ s|/mbox/|/$n/|;
open(CPIPE, "$cmd $c $args");
while( ($line{$f} !~ /^From /) && !eof($mbox{$f}) ) {
$line{$f} = readline $mbox{$f};
}
my $fromline = $line{$f};
$fromline =~ s/^From//;
$c =~ s|^.*/||;
print "$n $c ";
print CPIPE $line{$f};
$line{$f} = readline $mbox{$f};
while( !eof($mbox{$f}) && ($line{$f} !~ /^From /) ) {
print CPIPE $line{$f};
$line{$f} = readline $mbox{$f};
}
close(CPIPE);
# expect the piped command to output result without trailing newline
print $fromline;
}
last;
}
}
}
' "$@"
}
# this is the default filter
if [ -z "$MAILTOE_FILTER" ]; then
MAILTOE_FILTER="@PKGDATADIR@/dbaclB toe"
fi
# begin mailtest.functions
# end mailtest.functions
# check this for environment variable overrrides
[ -e $HOME/.mailtoerc ] && . $HOME/.mailtoerc
export TEMPDIR
# main switch statement - this processes commands
case $1 in
'-V')
echo $VERSION
;;
clean) # delete working tree
clean_working_tree
;;
killall)
prerequisite_command "killall" "killall"
killall -9 -g mailtoe
;;
prepare) # create directory tree
shift
prepare_working_tree "$@"
NUM=`expr $1 - 1`
# use parent process id to randomize
RANDOM=$PPID
for i in `seq 0 $NUM`; do
echo $RANDOM > "$MXDIR/$i/seed"
done
;;
add)
shift
CATNAME="$1"
if [ -z "$CATNAME" ]; then
echo "error: missing category name."
usage
fi
prerequisite_command "formail" "mailutils"
get_number_of_subsets
echo "=== $PROGNAME $*" >> $ALOG
shift
# use formail to ensure mbox format is clean
if [ -n "$*" ]; then
cat "$@" | formail -s /bin/bash -c \
"cat >> $MXDIR/mbox/$CATNAME.mbox"
else
formail -s /bin/bash -c \
"cat >> $MXDIR/mbox/$CATNAME.mbox"
fi
;;
learn)
echo "This command is not meaningful."
;;
run)
shift
get_number_of_subsets
NUM=`expr $NUM - 1` # we count from zero to NUM-1
get_categories
STUFF="$MXDIR/log/run.stuff"
prerequisite_command "perl" "perl"
prerequisite_command "sed" "sed"
prerequisite_command "seq" "shellutils"
echo "=== $PROGNAME run $*" >> $ALOG
echo "# location | true | predicted | from" > $CLOG
for i in `seq 0 $NUM`; do
COMMAND="$MAILTOE_FILTER "
SEED=`cat $MXDIR/$i/seed`
CATPATHS=`for n in $CATS; do echo -ne "$MXDIR/mbox/$n "; done`
echo "| $COMMAND" >> $ALOG
mbox_multiplex $i $SEED $CATPATHS -s $COMMAND >> $CLOG
echo " toe $COMMAND |" >> $ALOG
done
;;
summarize)
shift
get_number_of_subsets # includes check that directory tree is present
get_categories
prerequisite_command "awk" "awk or equivalent"
echo "=== $PROGNAME summarize $*" >> $ALOG
if [ -s $CLOG ]; then
cat $CLOG | summarize_log
else
echo "Error: No results found. You must run the TOE simulations first."
usage
fi
;;
review)
shift
prerequisite_command "formail" "mailutils"
prerequisite_command "grep" "grep"
if [ -z "$1" -o -z "$2" ]; then
echo "Error: Missing category, e.g. $PROGNAME review notspam spam"
else
review_misclassified "$MXDIR/tmp/save_msg.sh" "$1" "$2"
fi
;;
plot)
shift
prerequisite_command "gnuplot" "gnuplot"
if [ -e "$MXDIR/log/toe.log" ]; then
plot_errors "$MXDIR/log/toe.log" "Misclassifications over time in TOE simulation\n$MAILTOE_FILTER" "$@"
fi
;;
testsuite)
shift
case $1 in
list)
testsuite_list_wrappers
;;
deselect)
testsuite_deselect_wrappers "$@"
;;
select)
testsuite_select_wrappers "$@"
;;
status)
echo -e "The following categories are ready to be TOE tested:\n"
get_categories
for c in $CATS; do
echo -n "$c - counting... "
NUM=`grep '^From ' $MXDIR/*/$c | wc -l`
echo "$NUM messages"
done
echo -e "\nThe following classifiers are ready to be TOE tested:\n"
get_filters
for f in $FILTS; do
echo "$f - `$FILTERS/$f describe`"
done
;;
run)
get_filters
get_categories
get_number_of_subsets
NUM=`expr $NUM - 1`
make_dummy_mbox
for f in $FILTS; do
echo -ne "Now testing: "
"$FILTERS/$f" describe
echo "Cleanup."
"$FILTERS/$f" clean "$MXDIR"
# before we can classify, we need to create all the
# category databases - we use a dummy mailbox for this
for i in `seq 0 $NUM`; do
for j in $CATS; do
cat $MXDIR/mbox/dummy.mailbox | "$FILTERS/$f" learn "$MXDIR/$i/${j/.mbox/}"
done
done
export MAILTOE_FILTER="$FILTERS/$f toe"
echo "Running."
time "$PROGNAME" run
echo "Writing results."
echo -e "\n---------------" >> "$SLOG"
"$FILTERS/$f" describe >> "$SLOG"
date >> "$CLOG"
echo "---------------" >> "$SLOG"
"$PROGNAME" summarize >> "$SLOG"
if [ "$2" = "plots" ]; then
prerequisite_command "gnuplot" "gnuplot"
plot_errors "$MXDIR/plots/$f.toe.ps" "Misclassifications over time in TOE simulation\n$f" ps
fi
done
;;
summarize)
if [ -s "$SLOG" ]; then
cat "$SLOG"
else
echo "Error: No results found. You must run the testsuite first."
fi
;;
*)
usage
;;
esac
;;
*)
usage
;;
esac
exit 0
|