~ubuntu-branches/ubuntu/lucid/spamassassin/lucid-proposed

« back to all changes in this revision

Viewing changes to masses/bayes-testing/bayes-10pcv-driver

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2006-12-06 15:25:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20061206152533-0lq5d0c8f7le7ugq
Tags: 3.1.7-1ubuntu1
* Merge from Debian unstable. The remaining Ubuntu changes are:
  - debian/control:
    + libmail-spf-query-perl from Recommends to Depends for spamassassin 
      binary (Ubuntu: #28486)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
#
3
 
# bayes-10pcv-driver - run 10-fold cross-validation test on SpamAssassin Bayes
4
 
#
5
 
# Since Bayesish probability analysis requires training on a corpus, the
6
 
# traditional SpamAssassin 10-pass cross-validation suite can't be used.  Also,
7
 
# Bayes requires its own ten-pass testing, separately, to judge the effects of
8
 
# tweaks.  So that's what this is.
9
 
#
10
 
# Before running, you need to create a test corpus, as "cor/spam" and
11
 
# "cor/ham".  Here's how to do this:
12
 
 
13
 
#   cd TEST
14
 
#   SADIR/tools/split_corpora -n 10 -l 2000 -p cor/spam/bucket spf1 spf2 spf3 ...
15
 
#   SADIR/tools/split_corpora -n 10 -l 2000 -p cor/ham/bucket ham1 ham2 ham3 ...
16
 
#
17
 
# SADIR = top-level directory of SpamAssassin distro
18
 
# TEST  = the directory where the corpus and results are to be written
19
 
# spfN   = mail folders full of spam
20
 
# hamN   = mail folders full of ham
21
 
 
22
 
# It will produce a directory of results called "results".  The most important
23
 
# are "hist_all": a histogram of scores and frequencies, and "thresholds_all":
24
 
# the output of analysis of all scores and frequencies from the
25
 
# bayes-thresholds script.
26
 
 
27
 
# NOTE: by default you will need *AT LEAST* 2000 of either type to use
28
 
# this, since bayes will not be activated without 200 messages in the db,
29
 
# and each fold is run using 10% of the corpus -- and 2000/10 = 200.
30
 
 
31
 
###########################################################################
32
 
 
33
 
testdir=`pwd`
34
 
 
35
 
learnargs=
36
 
if [ "$#" -gt 0 ] ; then
37
 
  learnargs="$*"
38
 
fi
39
 
 
40
 
cd $SADIR/masses
41
 
PATH=$SADIR:$SADIR/masses:$PATH
42
 
 
43
 
results=$testdir/results
44
 
tmpdir=$results/config
45
 
 
46
 
rm -rf $results $tmpdir
47
 
 
48
 
# now, just copy in the Bayes ruleset
49
 
mkdir -p $results $tmpdir/rules
50
 
cp ../rules/23_bayes.cf $tmpdir/rules
51
 
cp ../rules/50*.cf $tmpdir/rules
52
 
 
53
 
# tell SpamAssassin to use this path for DBs
54
 
# TODO: for tests of these settings, read from a test-specific file
55
 
echo "
56
 
 
57
 
bayes_path                $tmpdir/dbs/bayes
58
 
bayes_auto_learn          0
59
 
bayes_min_ham_num         10
60
 
bayes_min_spam_num        10
61
 
 
62
 
" > $tmpdir/rules/30bayes_path.cf
63
 
mkdir $tmpdir/dbs
64
 
 
65
 
INTERLEAVE_TESTS=0
66
 
TEST_AGAINST_10PC=0
67
 
LEARN_ALL_THEN_FORGET_TEST_SET=0
68
 
 
69
 
backup_dbs () {
70
 
  echo "Backing up full learned DBs..."
71
 
  ( cd $tmpdir; tar cvf learned-all.tar dbs )
72
 
}
73
 
restore_dbs () {
74
 
  echo "Restoring full learned DBs..."
75
 
  ( cd $tmpdir; rm -rf dbs; tar xf learned-all.tar )
76
 
}
77
 
 
78
 
if [ $LEARN_ALL_THEN_FORGET_TEST_SET = 1 ] ; then
79
 
 
80
 
  # learn the lot, then forget the ones we're testing on each time.
81
 
  # faster than learning from scratch for each fold
82
 
 
83
 
  # note: we use randseed=1 so that every run will always pick the
84
 
  # same messages if --learnprob is used.
85
 
 
86
 
  (
87
 
  echo -n "Learning from all ham buckets..." ; date
88
 
  time sa-learn --ham --randseed=1 --no-sync $learnargs \
89
 
          --showdots --mbox --config-file=$tmpdir/rules $testdir/cor/ham/*
90
 
 
91
 
  echo -n "Learning from all spam buckets..." ; date
92
 
  time sa-learn --spam --randseed=1 --no-sync $learnargs \
93
 
          --showdots --mbox --config-file=$tmpdir/rules $testdir/cor/spam/*
94
 
 
95
 
  time sa-learn --sync $learnargs --config-file=$tmpdir/rules
96
 
 
97
 
  echo -n "Done learning. " ; date
98
 
  ) 2>&1 | tee $results/learn.log
99
 
 
100
 
  echo "Dumping bayes DB..."
101
 
  ( cd .. ; tools/check_bayes_db --dbpath=$tmpdir/dbs/bayes ) \
102
 
        > $results/bayes_db.dump
103
 
 
104
 
fi
105
 
 
106
 
backup_dbs
107
 
 
108
 
(
109
 
 
110
 
echo -n "Starting test..." ; date
111
 
for bucket in 1 2 3 4 5 6 7 8 9 10 ; do
112
 
  echo -n "Bucket $bucket..." ; date
113
 
 
114
 
  if [ $bucket != 1 ] ; then restore_dbs ; fi
115
 
 
116
 
  rdir=$results/bucket$bucket
117
 
  mkdir $rdir
118
 
 
119
 
  : > $rdir/hbucketlearn
120
 
  : > $rdir/sbucketlearn
121
 
  : > $rdir/hbuckettest
122
 
  : > $rdir/sbuckettest
123
 
  for subbucket in 1 2 3 4 5 6 7 8 9 10 ; do
124
 
    type=l
125
 
    [ $TEST_AGAINST_10PC = 1 -a $subbucket = $bucket ] && type=t
126
 
    [ $TEST_AGAINST_10PC = 0 -a $subbucket != $bucket ] && type=t
127
 
 
128
 
    if [ $type = l ] ; then
129
 
      echo "Using bucket for learn: $subbucket ..."
130
 
      cat $testdir/cor/ham/bucket.$subbucket >> $rdir/hbucketlearn
131
 
      cat $testdir/cor/spam/bucket.$subbucket >> $rdir/sbucketlearn
132
 
    else
133
 
      echo "Using bucket for test: $subbucket ..."
134
 
      cat $testdir/cor/ham/bucket.$subbucket >> $rdir/hbuckettest
135
 
      cat $testdir/cor/spam/bucket.$subbucket >> $rdir/sbuckettest
136
 
    fi
137
 
  done
138
 
 
139
 
  if [ $LEARN_ALL_THEN_FORGET_TEST_SET = 1 ] ; then
140
 
    echo "Forgetting contents of test ham bucket..."
141
 
    time sa-learn --forget --config-file=$tmpdir/rules --showdots \
142
 
                          --mbox $rdir/hbuckettest
143
 
 
144
 
    echo "Forgetting contents of test spam bucket..."
145
 
    time sa-learn --forget --config-file=$tmpdir/rules --showdots \
146
 
                          --mbox $rdir/sbuckettest
147
 
 
148
 
  else
149
 
    echo "Learning contents of learn ham bucket..."
150
 
    time sa-learn --ham --randseed=1 --no-sync $learnargs \
151
 
            --showdots --mbox --config-file=$tmpdir/rules $rdir/hbucketlearn
152
 
 
153
 
    echo "Learning contents of learn spam bucket..."
154
 
    time sa-learn --spam --randseed=1 --no-sync $learnargs \
155
 
            --showdots --mbox --config-file=$tmpdir/rules $rdir/sbucketlearn
156
 
 
157
 
    time sa-learn --sync $learnargs --config-file=$tmpdir/rules
158
 
 
159
 
    echo "Dumping bayes DB..."
160
 
    ( cd .. ; sa-learn --dump --dbpath=$tmpdir/dbs/bayes ) \
161
 
          > $rdir/bayes_db.dump
162
 
  fi
163
 
 
164
 
  time sa-learn --sync --config-file=$tmpdir/rules
165
 
 
166
 
  if [ $INTERLEAVE_TESTS = 1 ] ; then
167
 
    # now split the ham and spam test bucket into 10 sub-buckets,
168
 
    # so we interleave ham and spam while testing. important for
169
 
    # judging expiry effects
170
 
    : > $rdir/nonspam.log
171
 
    : > $rdir/spam.log
172
 
    
173
 
    mkdir $rdir/testbuckets
174
 
    (
175
 
      cd ..
176
 
      tools/split_corpora -n 10 -p $rdir/testbuckets/ham \
177
 
                        $rdir/hbuckettest
178
 
      tools/split_corpora -n 10 -p $rdir/testbuckets/spam \
179
 
                        $rdir/sbuckettest
180
 
    )
181
 
 
182
 
    for subbucket in 1 2 3 4 5 6 7 8 9 10 ; do
183
 
      echo "Running mass-check on ham test-bucket $subbucket..."
184
 
      time ./mass-check -c=$tmpdir/rules -p=$tmpdir/rules --showdots \
185
 
            --bayes --mbox $rdir/testbuckets/ham.$subbucket \
186
 
            >> $rdir/nonspam.log
187
 
 
188
 
      echo "Running mass-check on spam test-bucket $subbucket..."
189
 
      time ./mass-check -c=$tmpdir/rules -p=$tmpdir/rules --showdots \
190
 
            --bayes --mbox $rdir/testbuckets/spam.$subbucket \
191
 
            >> $rdir/spam.log
192
 
    done
193
 
 
194
 
  else
195
 
    echo "Running mass-check on ham bucket..."
196
 
    time ./mass-check -c=$tmpdir/rules -p=$tmpdir/rules --showdots \
197
 
          --bayes --mbox $rdir/hbuckettest \
198
 
          > $rdir/nonspam.log
199
 
 
200
 
    echo "Running mass-check on spam bucket..."
201
 
    time ./mass-check -c=$tmpdir/rules -p=$tmpdir/rules --showdots \
202
 
          --bayes --mbox $rdir/sbuckettest \
203
 
          > $rdir/spam.log
204
 
  fi
205
 
 
206
 
  echo "Reporting..."
207
 
  ./bayes-testing/draw-bayes-histogram \
208
 
        $rdir/spam.log $rdir/nonspam.log \
209
 
        > $rdir/hist
210
 
 
211
 
  ./bayes-testing/bayes-thresholds \
212
 
        $rdir/spam.log $rdir/nonspam.log \
213
 
        > $rdir/thresholds
214
 
 
215
 
  ./bayes-testing/bayes-static-thresholds \
216
 
        $rdir/spam.log $rdir/nonspam.log \
217
 
        > $rdir/thresholds.static
218
 
 
219
 
  # remove these, they're too big.
220
 
  rm -f $rdir/hbucketlearn $rdir/sbucketlearn $rdir/hbuckettest $rdir/sbuckettest
221
 
 
222
 
done
223
 
echo -n "Done test..." ; date
224
 
 
225
 
) 2>&1 | tee $results/test.log
226
 
 
227
 
cat $results/bucket*/spam.log > $results/spam_all.log
228
 
cat $results/bucket*/nonspam.log > $results/nonspam_all.log
229
 
 
230
 
./bayes-testing/draw-bayes-histogram \
231
 
        $results/spam_all.log $results/nonspam_all.log \
232
 
        > $results/hist_all
233
 
./bayes-testing/bayes-thresholds \
234
 
        $results/spam_all.log $results/nonspam_all.log \
235
 
        > $results/thresholds_all
236
 
./bayes-testing/bayes-static-thresholds \
237
 
        $results/spam_all.log $results/nonspam_all.log \
238
 
        > $results/thresholds.static
239
 
 
240
 
echo "Done."
241
 
ls -l $results
242