2004-12-05 Nelson Ferreira <nelson.ferreira@ieee.org>
[gnus] / lisp / spam-stat.el
1 ;;; spam-stat.el --- detecting spam based on statistics
2
3 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Keywords: network
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SpamStat
8
9 ;; This file is part of GNU Emacs.
10
11 ;; This is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This is distributed in the hope that it will be useful, but WITHOUT
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19 ;; License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This implements spam analysis according to Paul Graham in "A Plan
29 ;; for Spam".  The basis for all this is a statistical distribution of
30 ;; words for your spam and non-spam mails.  We need this information
31 ;; in a hash-table so that the analysis can use the information when
32 ;; looking at your mails.  Therefore, before you begin, you need tons
33 ;; of mails (Graham uses 4000 non-spam and 4000 spam mails for his
34 ;; experiments).
35 ;;
36 ;; The main interface to using spam-stat, are the following functions:
37 ;;
38 ;; `spam-stat-buffer-is-spam' -- called in a buffer, that buffer is
39 ;; considered to be a new spam mail; use this for new mail that has
40 ;; not been processed before
41 ;;
42 ;; `spam-stat-buffer-is-non-spam' -- called in a buffer, that buffer
43 ;; is considered to be a new non-spam mail; use this for new mail that
44 ;; has not been processed before
45 ;;
46 ;; `spam-stat-buffer-change-to-spam' -- called in a buffer, that
47 ;; buffer is no longer considered to be normal mail but spam; use this
48 ;; to change the status of a mail that has already been processed as
49 ;; non-spam
50 ;;
51 ;; `spam-stat-buffer-change-to-non-spam' -- called in a buffer, that
52 ;; buffer is no longer considered to be spam but normal mail; use this
53 ;; to change the status of a mail that has already been processed as
54 ;; spam
55 ;;
56 ;; `spam-stat-save' -- save the hash table to the file; the filename
57 ;; used is stored in the variable `spam-stat-file'
58 ;;
59 ;; `spam-stat-load' -- load the hash table from a file; the filename
60 ;; used is stored in the variable `spam-stat-file'
61 ;;
62 ;; `spam-stat-score-word' -- return the spam score for a word
63 ;;
64 ;; `spam-stat-score-buffer' -- return the spam score for a buffer
65 ;;
66 ;; `spam-stat-split-fancy' -- for fancy mail splitting; add
67 ;; the rule (: spam-stat-split-fancy) to `nnmail-split-fancy'
68 ;;
69 ;; This requires the following in your ~/.gnus file:
70 ;;
71 ;; (require 'spam-stat)
72 ;; (spam-stat-load)
73
74 ;;; Testing:
75
76 ;; Typical test will involve calls to the following functions:
77 ;;
78 ;; Reset: (spam-stat-reset)
79 ;; Learn spam: (spam-stat-process-spam-directory "~/Mail/mail/spam")
80 ;; Learn non-spam: (spam-stat-process-non-spam-directory "~/Mail/mail/misc")
81 ;; Save table: (spam-stat-save)
82 ;; File size: (nth 7 (file-attributes spam-stat-file))
83 ;; Number of words: (hash-table-count spam-stat)
84 ;; Test spam: (spam-stat-test-directory "~/Mail/mail/spam")
85 ;; Test non-spam: (spam-stat-test-directory "~/Mail/mail/misc")
86 ;; Reduce table size: (spam-stat-reduce-size)
87 ;; Save table: (spam-stat-save)
88 ;; File size: (nth 7 (file-attributes spam-stat-file))
89 ;; Number of words: (hash-table-count spam-stat)
90 ;; Test spam: (spam-stat-test-directory "~/Mail/mail/spam")
91 ;; Test non-spam: (spam-stat-test-directory "~/Mail/mail/misc")
92
93 ;;; Dictionary Creation:
94
95 ;; Typically, you will filter away mailing lists etc. using specific
96 ;; rules in `nnmail-split-fancy'.  Somewhere among these rules, you
97 ;; will filter spam.  Here is how you would create your dictionary:
98
99 ;; Reset: (spam-stat-reset)
100 ;; Learn spam: (spam-stat-process-spam-directory "~/Mail/mail/spam")
101 ;; Learn non-spam: (spam-stat-process-non-spam-directory "~/Mail/mail/misc")
102 ;; Repeat for any other non-spam group you need...
103 ;; Reduce table size: (spam-stat-reduce-size)
104 ;; Save table: (spam-stat-save)
105
106 ;;; Todo:
107
108 ;; Speed it up.  Integrate with Gnus such that it uses spam and expiry
109 ;; marks to call the appropriate functions when leaving the summary
110 ;; buffer and saves the hash table when leaving Gnus.  More testing:
111 ;; More mails, disabling SpamAssassin, double checking algorithm, find
112 ;; improved algorithm.
113
114 ;;; Thanks:
115
116 ;; Ted Zlatanov <tzz@lifelogs.com>
117 ;; Jesper Harder <harder@myrealbox.com>
118 ;; Dan Schmidt <dfan@dfan.org>
119 ;; Lasse Rasinen <lrasinen@iki.fi>
120 ;; Milan Zamazal <pdm@zamazal.org>
121
122 \f
123
124 ;;; Code:
125 (require 'mail-parse)
126
127 (defgroup spam-stat nil
128   "Statistical spam detection for Emacs.
129 Use the functions to build a dictionary of words and their statistical
130 distribution in spam and non-spam mails.  Then use a function to determine
131 whether a buffer contains spam or not."
132   :version "21.4"
133   :group 'gnus)
134
135 (defcustom spam-stat-file "~/.spam-stat.el"
136   "File used to save and load the dictionary.
137 See `spam-stat-to-hash-table' for the format of the file."
138   :type 'file
139   :group 'spam-stat)
140
141 (defcustom spam-stat-install-hooks t
142   "Whether spam-stat should install its hooks in Gnus.
143 This is set to nil if you use spam-stat through spam.el."
144   :type 'boolean
145   :group 'spam-stat)
146
147 (defcustom spam-stat-unknown-word-score 0.2
148   "The score to use for unknown words.
149 Also used for words that don't appear often enough."
150   :type 'number
151   :group 'spam-stat)
152
153 (defcustom spam-stat-max-word-length 15
154   "Only words shorter than this will be considered."
155   :type 'integer
156   :group 'spam-stat)
157
158 (defcustom spam-stat-max-buffer-length 10240
159   "Only the beginning of buffers will be analyzed.
160 This variable says how many characters this will be."
161   :type 'integer
162   :group 'spam-stat)
163
164 (defcustom spam-stat-split-fancy-spam-group "mail.spam"
165   "Name of the group where spam should be stored.
166 If `spam-stat-split-fancy' is used in fancy splitting rules.  Has
167 no effect when spam-stat is invoked through spam.el."
168   :type 'string
169   :group 'spam-stat)
170
171 (defcustom spam-stat-split-fancy-spam-threshhold 0.9
172   "Spam score threshold in spam-stat-split-fancy."
173   :type 'number
174   :group 'spam-stat)
175
176 (defcustom spam-stat-washing-hook nil
177   "Hook applied to each message before analysis."
178   :type 'hook
179   :group 'spam-stat)
180
181 (defcustom spam-stat-process-directory-age 90
182   "Max. age of files to be processed in directory, in days.
183 When using `spam-stat-process-spam-directory' or
184 `spam-stat-process-non-spam-directory', only files that have
185 been touched in this many days will be considered.  Without
186 this filter, re-training spam-stat with several thousand messages
187 will start to take a very long time.")
188
189 (defvar spam-stat-last-saved-at nil
190   "Time stamp of last change of spam-stat-file on this run")
191
192 (defvar spam-stat-syntax-table
193   (let ((table (copy-syntax-table text-mode-syntax-table)))
194     (modify-syntax-entry ?- "w" table)
195     (modify-syntax-entry ?_ "w" table)
196     (modify-syntax-entry ?. "w" table)
197     (modify-syntax-entry ?! "w" table)
198     (modify-syntax-entry ?? "w" table)
199     (modify-syntax-entry ?+ "w" table)
200     table)
201   "Syntax table used when processing mails for statistical analysis.
202 The important part is which characters are word constituents.")
203
204 (defvar spam-stat-dirty nil
205   "Whether the spam-stat database needs saving.")
206
207 (defvar spam-stat-buffer nil
208   "Buffer to use for scoring while splitting.
209 This is set by hooking into Gnus.")
210
211 (defvar spam-stat-buffer-name " *spam stat buffer*"
212   "Name of the `spam-stat-buffer'.")
213
214 (defvar spam-stat-coding-system
215   (if (mm-coding-system-p 'emacs-mule) 'emacs-mule 'raw-text)
216   "Coding system used for `spam-stat-file'.")
217
218 ;; Hooking into Gnus
219
220 (defun spam-stat-store-current-buffer ()
221   "Store a copy of the current buffer in `spam-stat-buffer'."
222   (let ((buf (current-buffer)))
223     (with-current-buffer (get-buffer-create spam-stat-buffer-name)
224       (erase-buffer)
225       (insert-buffer-substring buf)
226       (setq spam-stat-buffer (current-buffer)))))
227
228 (defun spam-stat-store-gnus-article-buffer ()
229   "Store a copy of the current article in `spam-stat-buffer'.
230 This uses `gnus-article-buffer'."
231   (with-current-buffer gnus-original-article-buffer
232     (spam-stat-store-current-buffer)))
233
234 ;; Data -- not using defstruct in order to save space and time
235
236 (defvar spam-stat (make-hash-table :test 'equal)
237   "Hash table used to store the statistics.
238 Use `spam-stat-load' to load the file.
239 Every word is used as a key in this table.  The value is a vector.
240 Use `spam-stat-ngood', `spam-stat-nbad', `spam-stat-good',
241 `spam-stat-bad', and `spam-stat-score' to access this vector.")
242
243 (defvar spam-stat-ngood 0
244   "The number of good mails in the dictionary.")
245
246 (defvar spam-stat-nbad 0
247   "The number of bad mails in the dictionary.")
248
249 (defsubst spam-stat-good (entry)
250   "Return the number of times this word belongs to good mails."
251   (aref entry 0))
252
253 (defsubst spam-stat-bad (entry)
254   "Return the number of times this word belongs to bad mails."
255   (aref entry 1))
256
257 (defsubst spam-stat-score (entry)
258   "Set the score of this word."
259   (if entry
260       (aref entry 2)
261     spam-stat-unknown-word-score))
262
263 (defsubst spam-stat-set-good (entry value)
264   "Set the number of times this word belongs to good mails."
265   (aset entry 0 value))
266
267 (defsubst spam-stat-set-bad (entry value)
268   "Set the number of times this word belongs to bad mails."
269   (aset entry 1 value))
270
271 (defsubst spam-stat-set-score (entry value)
272   "Set the score of this word."
273   (aset entry 2 value))
274
275 (defsubst spam-stat-make-entry (good bad)
276   "Return a vector with the given properties."
277   (let ((entry (vector good bad nil)))
278     (spam-stat-set-score entry (spam-stat-compute-score entry))
279     entry))
280
281 ;; Computing
282
283 (defun spam-stat-compute-score (entry)
284   "Compute the score of this word.  1.0 means spam."
285    ;; promote all numbers to floats for the divisions
286    (let* ((g (* 2.0 (spam-stat-good entry)))
287           (b (float (spam-stat-bad entry))))
288      (cond ((< (+ g b) 5)
289             .2)
290            ((= 0 spam-stat-ngood)
291             .99)
292            ((= 0 spam-stat-nbad)
293             .01)
294            (t
295             (max .01
296                  (min .99 (/ (/ b spam-stat-nbad)
297                              (+ (/ g spam-stat-ngood)
298                                 (/ b spam-stat-nbad)))))))))
299
300 ;; Parsing
301
302 (defmacro with-spam-stat-max-buffer-size (&rest body)
303   "Narrow the buffer down to the first 4k characters, then evaluate BODY."
304   `(save-restriction
305      (when (> (- (point-max)
306                  (point-min))
307               spam-stat-max-buffer-length)
308        (narrow-to-region (point-min)
309                          (+ (point-min) spam-stat-max-buffer-length)))
310      ,@body))
311
312 (defun spam-stat-buffer-words ()
313   "Return a hash table of words and number of occurrences in the buffer."
314   (run-hooks 'spam-stat-washing-hook)
315   (with-spam-stat-max-buffer-size
316    (with-syntax-table spam-stat-syntax-table
317      (goto-char (point-min))
318      (let ((result (make-hash-table :test 'equal))
319            word count)
320        (while (re-search-forward "\\w+" nil t)
321          (setq word (match-string-no-properties 0)
322                count (1+ (gethash word result 0)))
323          (when (< (length word) spam-stat-max-word-length)
324            (puthash word count result)))
325        result))))
326
327 (defun spam-stat-buffer-is-spam ()
328   "Consider current buffer to be a new spam mail."
329   (setq spam-stat-nbad (1+ spam-stat-nbad))
330   (maphash
331    (lambda (word count)
332      (let ((entry (gethash word spam-stat)))
333        (if entry
334            (spam-stat-set-bad entry (+ count (spam-stat-bad entry)))
335          (setq entry (spam-stat-make-entry 0 count)))
336        (spam-stat-set-score entry (spam-stat-compute-score entry))
337        (puthash word entry spam-stat)))
338    (spam-stat-buffer-words))
339   (setq spam-stat-dirty t))
340
341 (defun spam-stat-buffer-is-non-spam ()
342   "Consider current buffer to be a new non-spam mail."
343   (setq spam-stat-ngood (1+ spam-stat-ngood))
344   (maphash
345    (lambda (word count)
346      (let ((entry (gethash word spam-stat)))
347        (if entry
348            (spam-stat-set-good entry (+ count (spam-stat-good entry)))
349          (setq entry (spam-stat-make-entry count 0)))
350        (spam-stat-set-score entry (spam-stat-compute-score entry))
351        (puthash word entry spam-stat)))
352    (spam-stat-buffer-words))
353   (setq spam-stat-dirty t))
354
355 (defun spam-stat-buffer-change-to-spam ()
356   "Consider current buffer no longer normal mail but spam."
357   (setq spam-stat-nbad (1+ spam-stat-nbad)
358         spam-stat-ngood (1- spam-stat-ngood))
359   (maphash
360    (lambda (word count)
361      (let ((entry (gethash word spam-stat)))
362        (if (not entry)
363            (gnus-message 8 "This buffer has unknown words in it")
364          (spam-stat-set-good entry (- (spam-stat-good entry) count))
365          (spam-stat-set-bad entry (+ (spam-stat-bad entry) count))
366          (spam-stat-set-score entry (spam-stat-compute-score entry))
367          (puthash word entry spam-stat))))
368    (spam-stat-buffer-words))
369   (setq spam-stat-dirty t))
370
371 (defun spam-stat-buffer-change-to-non-spam ()
372   "Consider current buffer no longer spam but normal mail."
373   (setq spam-stat-nbad (1- spam-stat-nbad)
374         spam-stat-ngood (1+ spam-stat-ngood))
375   (maphash
376    (lambda (word count)
377      (let ((entry (gethash word spam-stat)))
378        (if (not entry)
379            (gnus-message 8 "This buffer has unknown words in it")
380          (spam-stat-set-good entry (+ (spam-stat-good entry) count))
381          (spam-stat-set-bad entry (- (spam-stat-bad entry) count))
382          (spam-stat-set-score entry (spam-stat-compute-score entry))
383          (puthash word entry spam-stat))))
384    (spam-stat-buffer-words))
385   (setq spam-stat-dirty t))
386
387 ;; Saving and Loading
388
389 (defun spam-stat-save (&optional force)
390   "Save the `spam-stat' hash table as lisp file.
391 With a prefix argument save unconditionally."
392   (interactive "P")
393   (when (or force spam-stat-dirty)
394     (let ((coding-system-for-write spam-stat-coding-system))
395       (with-temp-file spam-stat-file
396         (let ((standard-output (current-buffer))
397               (font-lock-maximum-size 0))
398           (insert (format ";-*- coding: %s; -*-\n" spam-stat-coding-system))
399           (insert (format "(setq spam-stat-ngood %d spam-stat-nbad %d
400 spam-stat (spam-stat-to-hash-table '(" spam-stat-ngood spam-stat-nbad))
401           (maphash (lambda (word entry)
402                      (prin1 (list word
403                                   (spam-stat-good entry)
404                                   (spam-stat-bad entry))))
405                    spam-stat)
406           (insert ")))"))))
407     (message "Saved %s."  spam-stat-file)
408     (setq spam-stat-dirty nil
409           spam-stat-last-saved-at (nth 5 (file-attributes spam-stat-file)))))
410
411 (defun spam-stat-load ()
412   "Read the `spam-stat' hash table from disk."
413   ;; TODO: maybe we should warn the user if spam-stat-dirty is t?
414   (let ((coding-system-for-read spam-stat-coding-system))
415     (cond (spam-stat-dirty (message "Spam stat not loaded: spam-stat-dirty t"))
416           ((or (not (boundp 'spam-stat-last-saved-at))
417                (null spam-stat-last-saved-at)
418                (not (equal spam-stat-last-saved-at
419                            (nth 5 (file-attributes spam-stat-file)))))
420            (progn 
421              (load-file spam-stat-file)
422              (setq spam-stat-dirty nil
423                    spam-stat-last-saved-at 
424                    (nth 5 (file-attributes spam-stat-file)))))
425           (t (message "Spam stat file not loaded: no change in disk..")))))
426
427 (defun spam-stat-to-hash-table (entries)
428   "Turn list ENTRIES into a hash table and store as `spam-stat'.
429 Every element in ENTRIES has the form \(WORD GOOD BAD) where WORD is
430 the word string, NGOOD is the number of good mails it has appeared in,
431 NBAD is the number of bad mails it has appeared in, GOOD is the number
432 of times it appeared in good mails, and BAD is the number of times it
433 has appeared in bad mails."
434   (let ((table (make-hash-table :size (length entries)
435                                 :test 'equal)))
436     (mapc (lambda (l)
437             (puthash (car l)
438                      (spam-stat-make-entry (nth 1 l) (nth 2 l))
439                      table))
440           entries)
441     table))
442
443 (defun spam-stat-reset ()
444   "Reset `spam-stat' to an empty hash-table.
445 This deletes all the statistics."
446   (interactive)
447   (setq spam-stat (make-hash-table :test 'equal)
448         spam-stat-ngood 0
449         spam-stat-nbad 0)
450   (setq spam-stat-dirty t))
451
452 ;; Scoring buffers
453
454 (defvar spam-stat-score-data nil
455   "Raw data used in the last run of `spam-stat-score-buffer'.")
456
457 (defsubst spam-stat-score-word (word)
458   "Return score for WORD.
459 The default score for unknown words is stored in
460 `spam-stat-unknown-word-score'."
461   (spam-stat-score (gethash word spam-stat)))
462
463 (defun spam-stat-buffer-words-with-scores ()
464   "Process current buffer, return the 15 most conspicuous words.
465 These are the words whose spam-stat differs the most from 0.5.
466 The list returned contains elements of the form \(WORD SCORE DIFF),
467 where DIFF is the difference between SCORE and 0.5."
468   (let (result word score)
469     (maphash (lambda (word ignore)
470                (setq score (spam-stat-score-word word)
471                      result (cons (list word score (abs (- score 0.5)))
472                                   result)))
473              (spam-stat-buffer-words))
474     (setq result (sort result (lambda (a b) (< (nth 2 b) (nth 2 a)))))
475     (setcdr (nthcdr 14 result) nil)
476     result))
477
478 (defun spam-stat-score-buffer ()
479   "Return a score describing the spam-probability for this buffer."
480   (setq spam-stat-score-data (spam-stat-buffer-words-with-scores))
481   (let* ((probs (mapcar 'cadr spam-stat-score-data))
482          (prod (apply #'* probs)))
483     (/ prod (+ prod (apply #'* (mapcar #'(lambda (x) (- 1 x))
484                                        probs))))))
485
486 (defun spam-stat-split-fancy ()
487   "Return the name of the spam group if the current mail is spam.
488 Use this function on `nnmail-split-fancy'.  If you are interested in
489 the raw data used for the last run of `spam-stat-score-buffer',
490 check the variable `spam-stat-score-data'."
491   (condition-case var
492       (progn
493         (set-buffer spam-stat-buffer)
494         (goto-char (point-min))
495         (when (> (spam-stat-score-buffer) spam-stat-split-fancy-spam-threshhold)
496           (when (boundp 'nnmail-split-trace)
497             (mapc (lambda (entry)
498                     (push entry nnmail-split-trace))
499                   spam-stat-score-data))
500           spam-stat-split-fancy-spam-group))
501     (error (message "Error in spam-stat-split-fancy: %S" var)
502            nil)))
503
504 ;; Testing
505
506 (defun spam-stat-strip-xref ()
507   "Strip the the Xref header."
508   (save-restriction
509     (mail-narrow-to-head)
510     (when (re-search-forward "^Xref:.*\n" nil t)
511       (delete-region (match-beginning 0) (match-end 0)))))
512
513 (defun spam-stat-process-directory (dir func)
514   "Process all the regular files in directory DIR using function FUNC."
515   (let* ((files (directory-files dir t "^[^.]"))
516          (max (/ (length files) 100.0))
517          (count 0))
518     (with-temp-buffer
519       (dolist (f files)
520         (when (and (file-readable-p f)
521                    (file-regular-p f)
522                    (> (nth 7 (file-attributes f)) 0)
523                    (< (time-to-number-of-days (time-since (nth 5 (file-attributes f))))
524                       spam-stat-process-directory-age))
525           (setq count (1+ count))
526           (message "Reading %s: %.2f%%" dir (/ count max))
527           (insert-file-contents-literally f)
528           (spam-stat-strip-xref)
529           (funcall func)
530           (erase-buffer))))))
531
532 (defun spam-stat-process-spam-directory (dir)
533   "Process all the regular files in directory DIR as spam."
534   (interactive "D")
535   (spam-stat-process-directory dir 'spam-stat-buffer-is-spam))
536
537 (defun spam-stat-process-non-spam-directory (dir)
538   "Process all the regular files in directory DIR as non-spam."
539   (interactive "D")
540   (spam-stat-process-directory dir 'spam-stat-buffer-is-non-spam))
541
542 (defun spam-stat-count ()
543   "Return size of `spam-stat'."
544   (interactive)
545   (hash-table-count spam-stat))
546
547 (defun spam-stat-test-directory (dir &optional verbose)
548   "Test all the regular files in directory DIR for spam.
549 If the result is 1.0, then all files are considered spam.
550 If the result is 0.0, non of the files is considered spam.
551 You can use this to determine error rates.
552
553 If VERBOSE is non-nil display names of files detected as spam or
554 non-spam in a temporary buffer.  If it is the symbol `ham',
555 display non-spam files; otherwise display spam files."
556   (interactive "DDirectory: ")
557   (let* ((files (directory-files dir t "^[^.]"))
558          display-files
559          buffer-score
560          (total (length files))
561          (score 0.0); float
562          (max (/ total 100.0)); float
563          (count 0))
564     (with-temp-buffer
565       (dolist (f files)
566         (when (and (file-readable-p f)
567                    (file-regular-p f)
568                    (> (nth 7 (file-attributes f)) 0))
569           (setq count (1+ count))
570           (message "Reading %.2f%%, score %.2f"
571                    (/ count max) (/ score count))
572           (insert-file-contents-literally f)
573           (setq buffer-score (spam-stat-score-buffer))
574           (when (> buffer-score 0.9)
575             (setq score (1+ score)))
576           (when verbose
577             (if (> buffer-score 0.9)
578                 (unless (eq verbose 'ham) (push f display-files))
579               (when (eq verbose 'ham) (push f display-files))))
580           (erase-buffer))))
581     (when display-files
582       (with-output-to-temp-buffer "*spam-stat results*"
583         (dolist (file display-files)
584           (princ file)
585           (terpri))))
586     (message "Final score: %d / %d = %f" score total (/ score total))))
587
588 ;; Shrinking the dictionary
589
590 (defun spam-stat-reduce-size (&optional count)
591   "Reduce the size of `spam-stat'.
592 This removes all words that occur less than COUNT from the dictionary.
593 COUNT defaults to 5"
594   (interactive)
595   (setq count (or count 5))
596   (maphash (lambda (key entry)
597              (when (< (+ (spam-stat-good entry)
598                          (spam-stat-bad entry))
599                       count)
600                (remhash key spam-stat)))
601            spam-stat)
602   (setq spam-stat-dirty t))
603
604 (defun spam-stat-install-hooks-function ()
605   "Install the spam-stat function hooks."
606   (interactive)
607   (add-hook 'nnmail-prepare-incoming-message-hook
608             'spam-stat-store-current-buffer)
609   (add-hook 'gnus-select-article-hook
610             'spam-stat-store-gnus-article-buffer))
611
612 (when spam-stat-install-hooks
613   (spam-stat-install-hooks-function))
614
615 (defun spam-stat-unload-hook ()
616   "Uninstall the spam-stat function hooks."
617   (interactive)
618   (remove-hook 'nnmail-prepare-incoming-message-hook
619                'spam-stat-store-current-buffer)
620   (remove-hook 'gnus-select-article-hook
621                'spam-stat-store-gnus-article-buffer))
622
623 (add-hook 'spam-stat-unload-hook 'spam-stat-unload-hook)
624
625 (provide 'spam-stat)
626
627 ;;; arch-tag: ff1d2200-8ddb-42fb-bb7b-1b5e20448554
628 ;;; spam-stat.el ends here