~ubuntu-branches/ubuntu/precise/dbacl/precise

« back to all changes in this revision

Viewing changes to ts/popfile

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2005-05-07 12:59:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050507125953-xzy2bwkb2qamglwm
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#
 
3
# Copyright (C) 2002 Laird Breyer
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
#
 
19
# Author:   Laird Breyer <laird@lbreyer.com>
 
20
#
 
21
# IMPLEMENTATION NOTES
 
22
#
 
23
# This script follows the mailcross testsuite interface
 
24
# requirements. Type man mailcross for details.
 
25
#
 
26
# The script accepts one of more commands on the command line,
 
27
# and may read STDIN and write STDOUT as follows:
 
28
#
 
29
# If $1 == "filter":
 
30
# In this case, a single email is expected on STDIN,
 
31
# and a list of category filenames is expected in $2, $3, etc.
 
32
# The script writes the category name corresponding to the 
 
33
# input email on STDOUT.
 
34
#
 
35
# If $1 == "learn":
 
36
# In this case, a standard mbox stream is expected on STDIN,
 
37
# while a suitable category file name is expected in $2. No output
 
38
# is written to STDOUT.
 
39
#
 
40
# If $1 == "clean":
 
41
# In this case, a directory is expected in $2, which is examined
 
42
# for old database information. If any old databases are found, they
 
43
# are purged or reset. No output is written to STDOUT.
 
44
#
 
45
# If $1 == "describe":
 
46
# In this case, STDIN and the command line are ignored. A single
 
47
# line is written on STDOUT, describing the filter functionality.
 
48
#
 
49
# If $1 == "bootstrap":
 
50
# In this case, the current script is copied to the directory $2,
 
51
# provided the classifier we're wrapping exists on the system.
 
52
#
 
53
 
 
54
# The POPFile API code is a quick and dirty adaptation of insert.pl
 
55
# I have no idea how this really works, and I wouldn't call it robust.
 
56
# It's been tested on popfile 0.20.1 and may not work on any other version.
 
57
# It seems that the corpus directory is always created in the current working
 
58
# directory. That's not thrilling, but we can make do.
 
59
 
 
60
# We call the API code directly instead of using the insert.pl and bayes.pl 
 
61
# scripts. This is necessary because those scripts don't create buckets, and
 
62
# assume a single usual corpus location. For our tests, we need lots of 
 
63
# different corpus locations.
 
64
 
 
65
# this variable is modified by bootstrap
 
66
POPDIR=""
 
67
[ -z "$TEMPDIR" ] && TEMPDIR=/tmp
 
68
 
 
69
case "$1" in
 
70
    filter)
 
71
        shift
 
72
        export BOOKAY=`basename $1`
 
73
        export DBPATH=`dirname $1`
 
74
        if [ -n "$POPDIR" ] ; then
 
75
            cd "$DBPATH"
 
76
            cat > mailcross.tmp
 
77
            # assume POPDIR is filled (done by bootstrap)
 
78
            # also, we assume that the buckets on the command line are 
 
79
            # the only ones in the current corpus
 
80
            perl -I"$POPDIR" -e '
 
81
use strict;
 
82
use locale;
 
83
use Classifier::Bayes;
 
84
use POPFile::Configuration;
 
85
use POPFile::MQ;
 
86
 
 
87
my $c = new POPFile::Configuration;
 
88
my $mq = new POPFile::MQ;
 
89
my $b = new Classifier::Bayes;
 
90
 
 
91
$c->configuration( $c );
 
92
$c->mq( $mq );
 
93
 
 
94
$mq->configuration( $c );
 
95
$mq->mq( $mq );
 
96
 
 
97
$b->configuration( $c );
 
98
$b->mq( $mq );
 
99
 
 
100
$b->initialize();
 
101
 
 
102
$c->load_configuration();
 
103
 
 
104
$b->start();
 
105
 
 
106
print $b->classify("mailcross.tmp");
 
107
 
 
108
$b->stop();
 
109
$mq->stop();
 
110
$c->stop();
 
111
'
 
112
            cd -
 
113
        fi
 
114
        ;;
 
115
    learn)
 
116
        shift
 
117
        export BOOKAY=`basename $1`
 
118
        export DBPATH=`dirname $1`
 
119
        
 
120
        if [ -n "$POPDIR" ] ; then
 
121
            # assume POPDIR is filled (done by bootstrap)
 
122
            cd "$DBPATH"
 
123
            perl -I"$POPDIR" -e '
 
124
use strict;
 
125
use locale;
 
126
use Classifier::Bayes;
 
127
use POPFile::Configuration;
 
128
use POPFile::MQ;
 
129
 
 
130
my $c = new POPFile::Configuration;
 
131
my $mq = new POPFile::MQ;
 
132
my $b = new Classifier::Bayes;
 
133
 
 
134
$c->configuration( $c );
 
135
$c->mq( $mq );
 
136
 
 
137
$mq->configuration( $c );
 
138
$mq->mq( $mq );
 
139
 
 
140
$b->configuration( $c );
 
141
$b->mq( $mq );
 
142
 
 
143
$b->initialize();
 
144
 
 
145
$c->load_configuration();
 
146
 
 
147
$b->start();
 
148
 
 
149
$b->create_bucket($ENV{"BOOKAY"});
 
150
$b->clear_bucket($ENV{"BOOKAY"});
 
151
 
 
152
open(TMPFILE, ">mailcross.tmp") || die;
 
153
while(<STDIN>) {
 
154
    if( /^From / ) {
 
155
        close(TMPFILE);
 
156
 
 
157
        $b->add_message_to_bucket($ENV{"BOOKAY"}, "mailcross.tmp");
 
158
 
 
159
        open(TMPFILE, ">mailcross.tmp") || die;
 
160
    }
 
161
    print TMPFILE $_;
 
162
}
 
163
close(TMPFILE);
 
164
$b->add_message_to_bucket($ENV{"BOOKAY"}, "mailcross.tmp");
 
165
 
 
166
$b->stop();
 
167
$mq->stop();
 
168
$c->stop();
 
169
'
 
170
            cd -
 
171
        fi
 
172
        ;;
 
173
    clean)
 
174
        shift
 
175
        find "$1" -name "table.db" -exec rm -f {} \;
 
176
        find "$1" -name "params" -exec rm -f {} \;
 
177
        ;;
 
178
    describe)
 
179
        VER="(unavailable?)"
 
180
        # we look for the popfile.pl script (not executable, can't use which)
 
181
        OLDIFS=$IFS
 
182
        IFS=:
 
183
        for d in `echo "$PATH:/usr/share/popfile:$HOME/popfile:."` ; do 
 
184
            if [ -f "$d/popfile.pl" ] ; then
 
185
            POPDIR=$d
 
186
            fi
 
187
        done
 
188
        IFS=$OLDIFS
 
189
 
 
190
        if [ -n "$POPDIR" -a -n "`which perl`" ] ; then
 
191
            VER=`cat $POPDIR/popfile.pl | grep CORE_version | sed -e 's/^.*(//' -e 's/).*$//' -e 's/, /./g'`
 
192
        fi
 
193
        echo "POPFile $VER with default options"
 
194
        ;;
 
195
    bootstrap)
 
196
        if [ -d "$2" ] ; then
 
197
            # we look for the popfile.pl script (not executable, can't use which)
 
198
            OLDIFS=$IFS
 
199
            IFS=:
 
200
            for d in `echo "$PATH:$HOME/popfile"` ; do 
 
201
                if [ -f "$d/popfile.pl" ] ; then
 
202
                    POPDIR=$d
 
203
                fi
 
204
            done
 
205
            IFS=$OLDIFS
 
206
 
 
207
            if [ -n "$POPDIR" -a -n "`which perl`" ] ; then
 
208
                echo "selecting $0"
 
209
                cat "$0" | sed -e "s|POPDIR=\"\"|POPDIR=\"$POPDIR\"|" > "$2"/`basename $0`
 
210
                chmod +x "$2"/`basename $0`
 
211
            else
 
212
                echo "POPFile appears to be missing"
 
213
            fi
 
214
        else
 
215
            echo "bad target directory $2"
 
216
        fi
 
217
        ;;
 
218
 
 
219
    toe)
 
220
        shift
 
221
        TRUECAT="$1"
 
222
        export HYACINTH=`basename $1`
 
223
        export DBPATH=`dirname $1`
 
224
        shift
 
225
        cat > "$TEMPDIR/mailtoe.tmp"
 
226
        shift
 
227
 
 
228
        if [ -n "$POPDIR" ] ; then
 
229
            cd "$DBPATH"
 
230
            # assume POPDIR is filled (done by bootstrap)
 
231
            # also, we assume that the buckets on the command line are 
 
232
            # the only ones in the current corpus
 
233
            perl -I"$POPDIR" -e '
 
234
use strict;
 
235
use locale;
 
236
use Classifier::Bayes;
 
237
use POPFile::Configuration;
 
238
use POPFile::MQ;
 
239
 
 
240
my $c = new POPFile::Configuration;
 
241
my $mq = new POPFile::MQ;
 
242
my $b = new Classifier::Bayes;
 
243
 
 
244
$c->configuration( $c );
 
245
$c->mq( $mq );
 
246
 
 
247
$mq->configuration( $c );
 
248
$mq->mq( $mq );
 
249
 
 
250
$b->configuration( $c );
 
251
$b->mq( $mq );
 
252
 
 
253
$b->initialize();
 
254
 
 
255
$c->load_configuration();
 
256
 
 
257
$b->start();
 
258
 
 
259
my $bookay = $b->classify($ENV{"TEMPDIR"}."/mailtoe.tmp");
 
260
if( $bookay ne $ENV{"HYACINTH"} ) {
 
261
    $b->add_message_to_bucket($ENV{"HYACINTH"}, $ENV{"TEMPDIR"}."/mailtoe.tmp");
 
262
}
 
263
 
 
264
print $bookay;
 
265
 
 
266
$b->stop();
 
267
$mq->stop();
 
268
$c->stop();
 
269
'
 
270
            cd -
 
271
        fi
 
272
        ;;
 
273
 
 
274
    foot)
 
275
        shift
 
276
        TRUECAT="$1"
 
277
        export HYACINTH=`basename $1`
 
278
        export DBPATH=`dirname $1`
 
279
        shift
 
280
        cat > "$TEMPDIR/mailtoe.tmp"
 
281
        shift
 
282
 
 
283
        if [ -n "$POPDIR" ] ; then
 
284
            cd "$DBPATH"
 
285
            # assume POPDIR is filled (done by bootstrap)
 
286
            # also, we assume that the buckets on the command line are 
 
287
            # the only ones in the current corpus
 
288
            perl -I"$POPDIR" -e '
 
289
use strict;
 
290
use locale;
 
291
use Classifier::Bayes;
 
292
use POPFile::Configuration;
 
293
use POPFile::MQ;
 
294
 
 
295
my $c = new POPFile::Configuration;
 
296
my $mq = new POPFile::MQ;
 
297
my $b = new Classifier::Bayes;
 
298
 
 
299
$c->configuration( $c );
 
300
$c->mq( $mq );
 
301
 
 
302
$mq->configuration( $c );
 
303
$mq->mq( $mq );
 
304
 
 
305
$b->configuration( $c );
 
306
$b->mq( $mq );
 
307
 
 
308
$b->initialize();
 
309
 
 
310
$c->load_configuration();
 
311
 
 
312
$b->start();
 
313
 
 
314
my $bookay = $b->classify($ENV{"TEMPDIR"}."/mailtoe.tmp");
 
315
$b->add_message_to_bucket($ENV{"HYACINTH"}, $ENV{"TEMPDIR"}."/mailtoe.tmp");
 
316
 
 
317
print $bookay;
 
318
 
 
319
$b->stop();
 
320
$mq->stop();
 
321
$c->stop();
 
322
'
 
323
            cd -
 
324
        fi
 
325
        ;;
 
326
 
 
327
esac