-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvegetarise.c
1739 lines (1528 loc) · 41.9 KB
/
vegetarise.c
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
/* vegetarise.c - A spam filter based on Paul Graham's idea
* Copyright (C) 2002 Werner Koch
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: vegetarise.c,v 1.6 2004/09/11 14:51:51 werner Exp $
*/
/* How to use:
*
* # Put this at the top of your ~/.procmailrc:
* #
* VEGETARISE_SOCKET=$HOME/.vegetarise-socket
* # After basic filtering, e.g. throwing away all chinese stuff and the
* # worm of the day, add a rule like:
* :0
* * ? vegetarise -s $HOME/Mail/words
* spamfolder2/
*
*
* To intialize vegetarise you need to have collections of spam and
* vegetarian mails. For example, if you have sorted them into two
* mbox files:
*
* vegetarise -l veg.mbox spam.mbox >words
*
* To add new stuff to an esisting word list, use this:
*
* vegetarise -l veg.mbox spam.mbox oldwords >words
*
* If you don't have mbox files but two files each with a list of mails
* (MH or Maildir format), you can use this:
*
* vegetarise -L veg-files.list spam-files.list oldwords >words
*
* It can either be run standalone (usually slow) or in auto server
* mode (using option -s).
**/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#ifdef HAVE_PTH /* In theory we could use sockets without Pth but it
does not make much sense to we require it. */
#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pth.h>
#endif /*HAVE_PTH*/
#define PGMNAME "vegetarise"
#define MAX_WORDLENGTH 50 /* max. length of a word */
#define MAX_WORDS 15 /* max. number of words to look at. */
/* A list of token characters. There is explicit code for 8bit
characters. */
#define TOKENCHARS "abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"0123456789-_'$"
#ifdef __GNUC__
#define inline __inline__
#else
#define inline
#endif
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
#define ATTR_PRINTF(a,b) __attribute__ ((format (printf,a,b)))
#define ATTR_NR_PRINTF(a,b) __attribute__ ((noreturn,format (printf,a,b)))
#else
#define ATTR_PRINTF(a,b)
#define ATTR_NR_PRINTF(a,b)
#endif
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
#define DIMof(type,member) DIM(((type *)0)->member)
#ifdef HAVE_PTH
# define my_read(a,b,c) pth_read ((a),(b),(c))
# define my_write(a,b,c) pth_write ((a),(b),(c))
# define YIELD do { if (running_as_server) pth_yield (NULL); } while (0)
#else
# define my_read(a,b,c) read ((a),(b),(c))
# define my_write(a,b,c) write ((a),(b),(c))
# define YIELD do { ; } while (0)
#endif
#define xtoi_1(a) ((a) <= '9'? ((a)- '0'): \
(a) <= 'F'? ((a)-'A'+10):((a)-'a'+10))
#define xtoi_2(a,b) ((xtoi_1(a) * 16) + xtoi_1(b))
struct pushback_s {
int buflen;
char buf[100];
int nl_seen;
int state;
int base64_nl;
int base64_val;
int qp1;
};
typedef struct pushback_s PUSHBACK;
struct hash_entry_s {
struct hash_entry_s *next;
unsigned int veg_count;
unsigned int spam_count;
unsigned int hit_ref; /* reference to the hit table. */
char prob; /* range is 1 to 99 or 0 for not calculated */
char word [1];
};
typedef struct hash_entry_s *HASH_ENTRY;
struct hit_array_s {
size_t size; /* Allocated size. */
unsigned int *hits;
};
typedef struct hit_array_s *HIT_ARRAY;
/* Option flags. */
static int verbose;
static int name_only;
/* Flag to indicate that we are running as a server. */
static int running_as_server;
/* Keep track of memory used for debugging. */
static size_t total_memory_used;
/* The global table with the words and its size. */
static int hash_table_size;
static HASH_ENTRY *word_table;
/* When storing a new word, we assign a hit reference id to it, so
that we can index a hit table. The variable keeps tracks of the
used reference numbers. */
static unsigned int next_hit_ref;
/* Number of good and bad messages the word table is made up. */
static unsigned int srvr_veg_count, srvr_spam_count;
/* Base64 conversion tables. */
static unsigned char bintoasc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static unsigned char asctobin[256]; /* runtime initialized */
/* Prototypes. */
static void die (const char *format, ...) ATTR_NR_PRINTF(1,2);
static void error (const char *format, ...) ATTR_PRINTF(1,2);
static void info (const char *format, ...) ATTR_PRINTF(1,2);
/*
Helper
*/
static void
die (const char *fmt, ...)
{
va_list arg_ptr;
va_start (arg_ptr, fmt);
fputs (PGMNAME": fatal error: ", stderr);
vfprintf (stderr, fmt, arg_ptr);
va_end (arg_ptr);
exit (1);
}
static void
error (const char *fmt, ...)
{
va_list arg_ptr;
va_start (arg_ptr, fmt);
fputs (PGMNAME": error: ", stderr);
vfprintf (stderr, fmt, arg_ptr);
va_end (arg_ptr);
}
static void
info (const char *fmt, ...)
{
va_list arg_ptr;
va_start (arg_ptr, fmt);
fputs (PGMNAME": ", stderr);
vfprintf (stderr, fmt, arg_ptr);
va_end (arg_ptr);
}
static void *
xmalloc (size_t n)
{
void *p = malloc (n);
if (!p)
die ("out of core\n");
total_memory_used += n;
return p;
}
static void *
xcalloc (size_t n, size_t k)
{
void *p = calloc (n, k);
if (!p)
die ("out of core\n");
total_memory_used += n * k;
return p;
}
static inline unsigned int
hash_string (const char *s)
{
unsigned int h = 0;
unsigned int g;
while (*s)
{
h = (h << 4) + *s++;
if ((g = (h & (unsigned int) 0xf0000000)))
h = (h ^ (g >> 24)) ^ g;
}
return (h % hash_table_size);
}
static void
pushback (PUSHBACK *pb, int c)
{
if (pb->buflen < sizeof pb->buf)
pb->buf[pb->buflen++] = c;
else
error ("comment parsing problem\n");
}
static inline int
basic_next_char (FILE *fp, PUSHBACK *pb)
{
int c;
if (pb->buflen)
{
c = *pb->buf;
if (--pb->buflen)
memmove (pb->buf, pb->buf+1, pb->buflen);
}
else
{
c = getc (fp);
if (c == EOF)
return c;
}
/* check for HTML comment - we only use the limited syntax:
"<!--" ... "-->" */
while (c == '<')
{
if ((c=getc (fp)) == EOF)
return c;
pushback (pb, c);
if ( c != '!' )
return '<';
if ((c=getc (fp)) == EOF)
{
pb->buflen = 0;;
return EOF; /* This misses the last chars but who cares. */
}
pushback (pb, c);
if ( c != '-' )
return '<';
if ((c=getc (fp)) == EOF)
{
pb->buflen = 0;
return EOF; /* This misses the last chars but who cares. */
}
pushback (pb, c);
if ( c != '-' )
return '<';
pb->buflen = 0;
/* found html comment - skip to end */
do
{
while ( (c = getc (fp)) != '-')
{
if (c == EOF)
return EOF;
}
if ( (c=getc (fp)) == EOF)
return EOF;
}
while ( c != '-' );
while ( (c = getc (fp)) != '>')
{
if (c == EOF)
return EOF;
}
c = getc (fp);
}
return c;
}
static inline int
next_char (FILE *fp, PUSHBACK *pb)
{
int c, c2;
next:
if ((c=basic_next_char (fp, pb)) == EOF)
return c;
switch (pb->state)
{
case 0: break;
case 1:
if (pb->nl_seen && (c == '\r' || c == '\n'))
{
pb->state = 2;
goto next;
}
break;
case 2:
if (!strchr (bintoasc, c))
break;
pb->nl_seen = 0;
pb->base64_nl = 0;
pb->state = 3;
/* fall through */
case 3: /* 1. base64 byte */
case 4: /* 2. base64 byte */
case 5: /* 3. base64 byte */
case 6: /* 4. base64 byte */
if (c == '\n')
{
pb->base64_nl = 1;
goto next;
}
if (pb->base64_nl && c == '-')
{
pb->state = 0; /* end of mime part */
c = ' '; /* make sure that last token gets processed. */
break;
}
pb->base64_nl = 0;
if (c == ' ' || c == '\r' || c == '\t')
goto next; /* skip all other kind of white space */
if (c == '=' )
goto next; /* we ignore the stop character and rely on
the MIME boundary */
if ((c = asctobin[c]) == 255 )
goto next; /* invalid base64 character */
switch (pb->state)
{
case 3:
pb->base64_val = c << 2;
pb->state = 4;
goto next;
case 4:
pb->base64_val |= (c>>4)&3;
c2 = pb->base64_val;
pb->base64_val = (c<<4)&0xf0;
c = c2;
pb->state = 5;
break; /* deliver C */
case 5:
pb->base64_val |= (c>>2)&15;
c2 = pb->base64_val;
pb->base64_val = (c<<6)&0xc0;
c = c2;
pb->state = 6;
break; /* deliver C */
case 6:
pb->base64_val |= c&0x3f;
c = pb->base64_val;
pb->state = 3;
break; /* deliver C */
}
break;
case 101: /* quoted-printable */
if (pb->nl_seen && (c == '\r' || c == '\n'))
{
pb->state = 102;
goto next;
}
break;
case 102:
if (pb->nl_seen && (c == '-'))
{
pb->state = 105;
goto next;
}
else if ( c == '=' )
{
pb->state = 103;
goto next;
}
break;
case 103:
if ( isxdigit (c) )
{
pb->qp1 = c;
pb->state = 104;
goto next;
}
pb->state = 102;
break;
case 104:
if ( isxdigit (c) )
c = xtoi_2 (pb->qp1, c);
pb->state = 102;
break;
case 105:
if (c == '-')
{
pb->state = 106;
goto next;
}
pb->state = 102;
break; /* we drop one, but that's okay for this application */
case 106:
if ( isspace (c) )
{
pb->state = 0; /* assume end of mime part */
c = ' '; /* make sure that last token gets processed. */
}
else
pb->state = 102;
break;
}
return c;
}
static void
enlarge_hit_array (HIT_ARRAY ha)
{
unsigned int *arr;
size_t i, n;
n = ha->size + 100;
arr = xmalloc (n * sizeof *arr);
for (i=0; i < ha->size; i++)
arr[i] = ha->hits[i];
for (; i < n; i++)
arr[i] = 0;
free (ha->hits);
ha->hits = arr;
ha->size = n;
}
static HIT_ARRAY
new_hit_array (void)
{
HIT_ARRAY ha = xmalloc (sizeof *ha);
/* Create the array with space for extra 1000 words */
ha->size = next_hit_ref + 1000;
ha->hits = xcalloc (ha->size, sizeof *ha->hits);
return ha;
}
static void
release_hit_array (HIT_ARRAY ha)
{
if (ha)
{
free (ha->hits);
free (ha);
}
}
/*
real processing stuff
*/
static HASH_ENTRY
store_word (const char *word, int *is_new)
{
unsigned int hash = hash_string (word);
HASH_ENTRY entry;
if (is_new)
*is_new = 0;
for (entry = word_table[hash];
entry && strcmp (entry->word, word); entry = entry->next)
;
if (!entry)
{
size_t n = sizeof *entry + strlen (word);
entry = xmalloc (n);
strcpy (entry->word, word);
entry->veg_count = 0;
entry->spam_count = 0;
entry->hit_ref = next_hit_ref++;
entry->prob = 0;
entry->next = word_table[hash];
word_table[hash] = entry;
if (is_new)
*is_new = 1;
}
return entry;
}
static void
check_one_word ( const char *word, int left_anchored, int is_spam,
HIT_ARRAY ha)
{
size_t wordlen = strlen (word);
const char *p;
int n0, n1, n2, n3, n4, n5;
/* fprintf (stderr, "token `%s'\n", word); */
for (p=word; isdigit (*p); p++)
;
if (!*p || wordlen < 3)
return; /* only digits or less than 3 chars */
if (wordlen == 16 && word[6] == '-' && word[13] == '-')
return; /* very likely a message-id formatted like 16cpeB-0004HM-00 */
if (wordlen > 25 )
return; /* words longer than that are rare */
for (n0=n1=n2=n3=n4=n5=0, p=word; *p; p++)
{
if ( *p & 0x80)
n0++;
else if ( isupper (*p) )
n1++;
else if ( islower (*p) )
n2++;
else if ( isdigit (*p) )
n3++;
else if ( *p == '-' )
n4++;
else if ( *p == '.' )
n5++;
}
if ( n4 == wordlen)
return; /* Only dashes. */
/* try to figure meaningless identifiers */
if (n0)
; /* 8bit chars in name are okay */
else if ( n3 && n3 + n5 == wordlen && n5 == 3 )
; /* allow IP addresses */
else if ( !n5 && n1 > 3 && (n2 > 3 || n3 > 3) )
return; /* No dots, mixed uppercase with digits or lowercase. */
else if ( !n5 && n2 > 3 && (n1 > 3 || n3 > 3) )
return; /* No dots, mixed lowercase with digits or uppercase. */
else if ( wordlen > 8 && (3*n3) > (n1+n2))
return; /* long word with 3 times more digits than letters. */
if (ha)
{ /* we are in checking mode */
int is_new;
HASH_ENTRY e = store_word (word, &is_new);
if (ha->size <= e->hit_ref)
{
assert (e->hit_ref < next_hit_ref);
enlarge_hit_array (ha);
}
ha->hits[e->hit_ref]++;
}
else if (is_spam)
store_word (word, NULL)->spam_count++;
else
store_word (word, NULL)->veg_count++;
}
/* Parse a message and return the number of messages in case it is an
mbox message as indicated by IS_MBOX passed as true. */
static unsigned int
parse_message (const char *fname, FILE *fp, int is_spam, int is_mbox,
HIT_ARRAY ha)
{
int c;
char aword[MAX_WORDLENGTH+1];
int idx = 0;
int in_token = 0;
int left_anchored = 0;
int maybe_base64 = 0;
PUSHBACK pbbuf;
unsigned int msgcount = 0;
int count = 0;
memset (&pbbuf, 0, sizeof pbbuf);
while ( (c=next_char (fp, &pbbuf)) != EOF)
{
if ( ++count > 20000 )
{
count = 0;
YIELD;
}
again:
if (in_token)
{
if ((c & 0x80) || strchr (TOKENCHARS, c))
{
if (idx < MAX_WORDLENGTH)
aword[idx++] = c;
/* truncate a word and ignore truncated characters */
}
else
{ /* got a delimiter */
in_token = 0;
aword[idx] = 0;
if (maybe_base64)
{
if ( !strcasecmp (aword, "base64") )
pbbuf.state = 1;
else if ( !strcasecmp (aword, "quoted-printable") )
pbbuf.state = 101;
else
pbbuf.state = 0;
maybe_base64 = 0;
}
else if (is_mbox && left_anchored
&& !pbbuf.state && !strcmp (aword, "From"))
{
if (c != ' ')
{
pbbuf.nl_seen = (c == '\n');
goto again;
}
msgcount++;
}
else if (left_anchored
&& (!strcasecmp (aword, "Received")
|| !strcasecmp (aword, "Date")))
{
if (c != ':')
{
pbbuf.nl_seen = (c == '\n');
goto again;
}
do
{
while ( (c = next_char (fp, &pbbuf)) != '\n')
{
if (c == EOF)
goto leave;
}
}
while ( c == ' ' || c == '\t');
pbbuf.nl_seen = 1;
goto again;
}
else if (left_anchored
&& !strcasecmp (aword, "Content-Transfer-Encoding"))
{
if (c != ':')
{
pbbuf.nl_seen = (c == '\n');
goto again;
}
maybe_base64 = 1;
}
else if (c == '.' && idx && !(aword[idx-1] & 0x80)
&& isalnum (aword[idx-1]) )
{
/* Assume an IP address or a hostname if a dot is
followed by a letter or digit. */
c = next_char (fp, &pbbuf);
if ( !(c & 0x80) && isalnum (c) && idx < MAX_WORDLENGTH)
{
aword[idx++] = '.';
in_token = 1;
}
else
check_one_word (aword, left_anchored, is_spam, ha);
pbbuf.nl_seen = (c == '\n');
goto again;
}
#if 0
else if (c == '=')
{
/* Assume an QP encoded character if followed by an
hexdigit */
c = next_char (fp, &pbbuf);
if ( !(c & 0x80) && (isxdigit (c)) && idx < MAX_WORDLENGTH)
{
aword[idx++] = '=';
in_token = 1;
}
else
check_one_word (aword, left_anchored, is_spam, ha);
pbbuf.nl_seen = (c == '\n');
goto again;
}
#endif
else
check_one_word (aword, left_anchored, is_spam, ha);
}
}
else if ( (c & 0x80) || strchr (TOKENCHARS, c))
{
in_token = 1;
idx = 0;
aword[idx++] = c;
left_anchored = pbbuf.nl_seen;
}
pbbuf.nl_seen = (c == '\n');
}
leave:
if (ferror (fp))
die ("error reading `%s': %s\n", fname, strerror (errno));
msgcount++;
return msgcount;
}
static unsigned int
calc_prob (unsigned int g, unsigned int b,
unsigned int ngood, unsigned int nbad)
{
double prob_g, prob_b, prob;
/* (max .01 */
/* (min .99 (float (/ (min 1 (/ b nbad)) */
/* (+ (min 1 (/ g ngood)) */
/* (min 1 (/ b nbad))))))))) */
prob_g = (double)g / ngood;
if (prob_g > 1)
prob_g = 1;
prob_b = (double)b / ngood;
if (prob_b > 1)
prob_b = 1;
prob = prob_b / (prob_g + prob_b);
if (prob < .01)
prob = .01;
else if (prob > .99)
prob = .99;
return (unsigned int) (prob * 100);
}
static void
calc_probability (unsigned int ngood, unsigned int nbad)
{
int n;
HASH_ENTRY entry;
unsigned int g, b;
if (!ngood)
die ("no vegetarian mails available - stop\n");
if (!nbad)
die ("no spam mails available - stop\n");
for (n=0; n < hash_table_size; n++)
{
for (entry = word_table[n]; entry; entry = entry->next)
{
g = entry->veg_count * 2;
b = entry->spam_count;
if (g + b >= 5)
entry->prob = calc_prob (g, b, ngood, nbad);
}
}
}
static unsigned int
check_spam (unsigned int ngood, unsigned int nbad, HIT_ARRAY ha)
{
unsigned int n;
HASH_ENTRY entry;
unsigned int dist, min_dist;
struct {
HASH_ENTRY e;
unsigned int d;
double prob;
} st[MAX_WORDS];
int nst = 0;
int i;
double prod, inv_prod, taste;
for (i=0; i < MAX_WORDS; i++)
{
st[i].e = NULL;
st[i].d = 0;
}
min_dist = 100;
for (n=0; n < hash_table_size; n++)
{
for (entry = word_table[n]; entry; entry = entry->next)
{
if (entry->hit_ref && ha->hits[entry->hit_ref])
{
if (!entry->prob)
dist = 10; /* 50 - 40 */
else
dist = entry->prob < 50? (50 - entry->prob):(entry->prob - 50);
if (nst < MAX_WORDS)
{
st[nst].e = entry;
st[nst].d = dist;
st[nst].prob = entry->prob? (double)entry->prob/100 : 0.4;
if (dist < min_dist)
min_dist = dist;
nst++;
}
else if (dist > min_dist)
{
unsigned int tmp = 100;
int tmpidx = -1;
for (i=0; i < MAX_WORDS; i++)
{
if (st[i].d < tmp)
{
tmp = st[i].d;
tmpidx = i;
}
}
assert (tmpidx != -1);
st[tmpidx].e = entry;
st[tmpidx].d = dist;
st[tmpidx].prob = entry->prob? (double)entry->prob/100 : 0.4;
min_dist = 100;
for (i=0; i < MAX_WORDS; i++)
{
if (st[i].d < min_dist)
min_dist = dist;
}
}
}
}
}
/* ST has now the NST most intersting words */
if (!nst)
{
info ("not enough words - assuming goodness\n");
return 100;
}
if (verbose > 1)
{
for (i=0; i < nst; i++)
info ("prob %3.2f dist %3d for `%s'\n",
st[i].prob, st[i].d, st[i].e->word);
}
prod = 1;
for (i=0; i < nst; i++)
prod *= st[i].prob;
inv_prod = 1;
for (i=0; i < nst; i++)
inv_prod *= 1.0 - st[i].prob;
taste = prod / (prod + inv_prod);
if (verbose > 1)
info ("taste -> %u\n\n", (unsigned int)(taste * 100));
return (unsigned int)(taste * 100);
}
static void
reset_hits (HIT_ARRAY ha)
{
int i;
for (i=0; i < ha->size; i++)
ha->hits[i] = 0;
}
static void
write_table (unsigned int ngood, unsigned int nbad)
{
int n;
HASH_ENTRY entry;
printf ("#\t0\t0\t0\t%u\t%u\n", ngood, nbad);
for (n=0; n < hash_table_size; n++)
{
for (entry = word_table[n]; entry; entry = entry->next)
if (entry->prob)
printf ("%s\t%d\t%u\t%u\n", entry->word, entry->prob,
entry->veg_count, entry->spam_count);
}
}
static void
read_table (const char *fname,
unsigned int *ngood, unsigned int *nbad, unsigned int *nwords)
{
FILE *fp;
char line[MAX_WORDLENGTH + 100];
unsigned int lineno = 0;
char *p;
*nwords = 0;
fp = fopen (fname, "r");
if (!fp)
die ("can't open wordlist `%s': %s\n", fname, strerror (errno));
while ( fgets (line, sizeof line, fp) )
{
lineno++;
if (!*line) /* last line w/o LF? */
die ("incomplete line %u in `%s'\n", lineno, fname);
if (line[strlen (line)-1] != '\n')
die ("line %u in `%s' too long\n", lineno, fname);
line[strlen (line)-1] = 0;
if (!*line)
goto invalid_line;
p = strchr (line, '\t');
if (!p || p == line || (p-line) > MAX_WORDLENGTH )
goto invalid_line;
*p++ = 0;
if (lineno == 1)
{
if (sscanf (p, "%*u %*u %*u %u %u", ngood, nbad) < 2)
goto invalid_line;
}
else
{
HASH_ENTRY e;
unsigned int prob, g, b;
int is_new;
if (sscanf (p, "%u %u %u", &prob, &g, &b) < 3)
goto invalid_line;
if (prob > 99)
goto invalid_line;
e = store_word (line, &is_new);
if (!is_new)
die ("duplicate entry at line %u in `%s'\n", lineno, fname);
e->prob = prob? prob : 1;
e->veg_count = g;
e->spam_count = b;
++*nwords;
}
}
if (ferror (fp))
die ("error reading wordlist `%s' at line %u: %s\n",
fname, lineno, strerror (errno));
fclose (fp);
return;
invalid_line:
die ("invalid line %u in `%s'\n", lineno, fname);
}