Clarify comment.
[gnus] / lisp / spam-stat.el
1 ;;; spam-stat.el --- detecting spam based on statistics
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 ;;   2010  Free Software Foundation, Inc.
5
6 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; Keywords: network
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SpamStat
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This implements spam analysis according to Paul Graham in "A Plan
28 ;; for Spam".  The basis for all this is a statistical distribution of
29 ;; words for your spam and non-spam mails.  We need this information
30 ;; in a hash-table so that the analysis can use the information when
31 ;; looking at your mails.  Therefore, before you begin, you need tons
32 ;; of mails (Graham uses 4000 non-spam and 4000 spam mails for his
33 ;; experiments).
34 ;;
35 ;; The main interface to using spam-stat, are the following functions:
36 ;;
37 ;; `spam-stat-buffer-is-spam' -- called in a buffer, that buffer is
38 ;; considered to be a new spam mail; use this for new mail that has
39 ;; not been processed before
40 ;;
41 ;; `spam-stat-buffer-is-non-spam' -- called in a buffer, that buffer
42 ;; is considered to be a new non-spam mail; use this for new mail that
43 ;; has not been processed before
44 ;;
45 ;; `spam-stat-buffer-change-to-spam' -- called in a buffer, that
46 ;; buffer is no longer considered to be normal mail but spam; use this
47 ;; to change the status of a mail that has already been processed as
48 ;; non-spam
49 ;;
50 ;; `spam-stat-buffer-change-to-non-spam' -- called in a buffer, that
51 ;; buffer is no longer considered to be spam but normal mail; use this
52 ;; to change the status of a mail that has already been processed as
53 ;; spam
54 ;;
55 ;; `spam-stat-save' -- save the hash table to the file; the filename
56 ;; used is stored in the variable `spam-stat-file'
57 ;;
58 ;; `spam-stat-load' -- load the hash table from a file; the filename
59 ;; used is stored in the variable `spam-stat-file'
60 ;;
61 ;; `spam-stat-score-word' -- return the spam score for a word
62 ;;
63 ;; `spam-stat-score-buffer' -- return the spam score for a buffer
64 ;;
65 ;; `spam-stat-split-fancy' -- for fancy mail splitting; add
66 ;; the rule (: spam-stat-split-fancy) to `nnmail-split-fancy'
67 ;;
68 ;; This requires the following in your ~/.gnus file:
69 ;;
70 ;; (require 'spam-stat)
71 ;; (spam-stat-load)
72
73 ;;; Testing:
74
75 ;; Typical test will involve calls to the following functions:
76 ;;
77 ;; Reset: (spam-stat-reset)
78 ;; Learn spam: (spam-stat-process-spam-directory "~/Mail/mail/spam")
79 ;; Learn non-spam: (spam-stat-process-non-spam-directory "~/Mail/mail/misc")
80 ;; Save table: (spam-stat-save)
81 ;; File size: (nth 7 (file-attributes spam-stat-file))
82 ;; Number of words: (hash-table-count spam-stat)
83 ;; Test spam: (spam-stat-test-directory "~/Mail/mail/spam")
84 ;; Test non-spam: (spam-stat-test-directory "~/Mail/mail/misc")
85 ;; Reduce table size: (spam-stat-reduce-size)
86 ;; Save table: (spam-stat-save)
87 ;; File size: (nth 7 (file-attributes spam-stat-file))
88 ;; Number of words: (hash-table-count spam-stat)
89 ;; Test spam: (spam-stat-test-directory "~/Mail/mail/spam")
90 ;; Test non-spam: (spam-stat-test-directory "~/Mail/mail/misc")
91
92 ;;; Dictionary Creation:
93
94 ;; Typically, you will filter away mailing lists etc. using specific
95 ;; rules in `nnmail-split-fancy'.  Somewhere among these rules, you
96 ;; will filter spam.  Here is how you would create your dictionary:
97
98 ;; Reset: (spam-stat-reset)
99 ;; Learn spam: (spam-stat-process-spam-directory "~/Mail/mail/spam")
100 ;; Learn non-spam: (spam-stat-process-non-spam-directory "~/Mail/mail/misc")
101 ;; Repeat for any other non-spam group you need...
102 ;; Reduce table size: (spam-stat-reduce-size)
103 ;; Save table: (spam-stat-save)
104
105 ;;; Todo:
106
107 ;; Speed it up.  Integrate with Gnus such that it uses spam and expiry
108 ;; marks to call the appropriate functions when leaving the summary
109 ;; buffer and saves the hash table when leaving Gnus.  More testing:
110 ;; More mails, disabling SpamAssassin, double checking algorithm, find
111 ;; improved algorithm.
112
113 ;;; Thanks:
114
115 ;; Ted Zlatanov <tzz@lifelogs.com>
116 ;; Jesper Harder <harder@myrealbox.com>
117 ;; Dan Schmidt <dfan@dfan.org>
118 ;; Lasse Rasinen <lrasinen@iki.fi>
119 ;; Milan Zamazal <pdm@zamazal.org>
120
121 \f
122
123 ;;; Code:
124 (require 'mail-parse)
125
126 (defvar gnus-original-article-buffer)
127
128 (defgroup spam-stat nil
129   "Statistical spam detection for Emacs.
130 Use the functions to build a dictionary of words and their statistical
131 distribution in spam and non-spam mails.  Then use a function to determine
132 whether a buffer contains spam or not."
133   :version "22.1"
134   :group 'gnus)
135
136 (defcustom spam-stat-file "~/.spam-stat.el"
137   "File used to save and load the dictionary.
138 See `spam-stat-to-hash-table' for the format of the file."
139   :type 'file
140   :group 'spam-stat)
141
142 (defcustom spam-stat-install-hooks t
143   "Whether spam-stat should install its hooks in Gnus.
144 This is set to nil if you use spam-stat through spam.el."
145   :type 'boolean
146   :group 'spam-stat)
147
148 (defcustom spam-stat-unknown-word-score 0.2
149   "The score to use for unknown words.
150 Also used for words that don't appear often enough."
151   :type 'number
152   :group 'spam-stat)
153
154 (defcustom spam-stat-max-word-length 15
155   "Only words shorter than this will be considered."
156   :type 'integer
157   :group 'spam-stat)
158
159 (defcustom spam-stat-max-buffer-length 10240
160   "Only the beginning of buffers will be analyzed.
161 This variable says how many characters this will be."
162   :type 'integer
163   :group 'spam-stat)
164
165 (defcustom spam-stat-split-fancy-spam-group "mail.spam"
166   "Name of the group where spam should be stored.
167 If `spam-stat-split-fancy' is used in fancy splitting rules.  Has
168 no effect when spam-stat is invoked through spam.el."
169   :type 'string
170   :group 'spam-stat)
171
172 (defcustom spam-stat-split-fancy-spam-threshold 0.9
173   "Spam score threshold in spam-stat-split-fancy."
174   :type 'number
175   :group 'spam-stat)
176
177 (defcustom spam-stat-washing-hook nil
178   "Hook applied to each message before analysis."
179   :type 'hook
180   :group 'spam-stat)
181
182 (defcustom spam-stat-score-buffer-user-functions nil
183   "List of additional scoring functions.
184 Called  one by one on the buffer.
185
186 If all of these functions return non-nil answers, these numerical
187 answers are added to the computed spam stat score on the buffer.  If
188 you defun such functions, make sure they don't return the buffer in a
189 narrowed state or such: use, for example, `save-excursion'.  Each of
190 your functions is also passed the initial spam-stat score which might
191 aid in your scoring.
192
193 Also be careful when defining such functions.  If they take a long
194 time, they will slow down your mail splitting.  Thus, if the buffer is
195 large, don't forget to use smaller regions, by wrapping your work in,
196 say, `with-spam-stat-max-buffer-size'."
197   :type '(repeat sexp)
198   :group 'spam-stat)
199
200 (defcustom spam-stat-process-directory-age 90
201   "Max. age of files to be processed in directory, in days.
202 When using `spam-stat-process-spam-directory' or
203 `spam-stat-process-non-spam-directory', only files that have
204 been touched in this many days will be considered.  Without
205 this filter, re-training spam-stat with several thousand messages
206 will start to take a very long time."
207   :type 'number
208   :group 'spam-stat)
209
210 (defvar spam-stat-last-saved-at nil
211   "Time stamp of last change of spam-stat-file on this run")
212
213 (defvar spam-stat-syntax-table
214   (let ((table (copy-syntax-table text-mode-syntax-table)))
215     (modify-syntax-entry ?- "w" table)
216     (modify-syntax-entry ?_ "w" table)
217     (modify-syntax-entry ?. "w" table)
218     (modify-syntax-entry ?! "w" table)
219     (modify-syntax-entry ?? "w" table)
220     (modify-syntax-entry ?+ "w" table)
221     table)
222   "Syntax table used when processing mails for statistical analysis.
223 The important part is which characters are word constituents.")
224
225 (defvar spam-stat-dirty nil
226   "Whether the spam-stat database needs saving.")
227
228 (defvar spam-stat-buffer nil
229   "Buffer to use for scoring while splitting.
230 This is set by hooking into Gnus.")
231
232 (defvar spam-stat-buffer-name " *spam stat buffer*"
233   "Name of the `spam-stat-buffer'.")
234
235 (defvar spam-stat-coding-system
236   (if (mm-coding-system-p 'emacs-mule) 'emacs-mule 'raw-text)
237   "Coding system used for `spam-stat-file'.")
238
239 ;; Hooking into Gnus
240
241 (defun spam-stat-store-current-buffer ()
242   "Store a copy of the current buffer in `spam-stat-buffer'."
243   (let ((buf (current-buffer)))
244     (with-current-buffer (get-buffer-create spam-stat-buffer-name)
245       (erase-buffer)
246       (insert-buffer-substring buf)
247       (setq spam-stat-buffer (current-buffer)))))
248
249 (defun spam-stat-store-gnus-article-buffer ()
250   "Store a copy of the current article in `spam-stat-buffer'.
251 This uses `gnus-article-buffer'."
252   (with-current-buffer gnus-original-article-buffer
253     (spam-stat-store-current-buffer)))
254
255 ;; Data -- not using defstruct in order to save space and time
256
257 (defvar spam-stat (make-hash-table :test 'equal)
258   "Hash table used to store the statistics.
259 Use `spam-stat-load' to load the file.
260 Every word is used as a key in this table.  The value is a vector.
261 Use `spam-stat-ngood', `spam-stat-nbad', `spam-stat-good',
262 `spam-stat-bad', and `spam-stat-score' to access this vector.")
263
264 (defvar spam-stat-ngood 0
265   "The number of good mails in the dictionary.")
266
267 (defvar spam-stat-nbad 0
268   "The number of bad mails in the dictionary.")
269
270 (defvar spam-stat-error-holder nil
271   "A holder for condition-case errors while scoring buffers.")
272
273 (defsubst spam-stat-good (entry)
274   "Return the number of times this word belongs to good mails."
275   (aref entry 0))
276
277 (defsubst spam-stat-bad (entry)
278   "Return the number of times this word belongs to bad mails."
279   (aref entry 1))
280
281 (defsubst spam-stat-score (entry)
282   "Set the score of this word."
283   (if entry
284       (aref entry 2)
285     spam-stat-unknown-word-score))
286
287 (defsubst spam-stat-set-good (entry value)
288   "Set the number of times this word belongs to good mails."
289   (aset entry 0 value))
290
291 (defsubst spam-stat-set-bad (entry value)
292   "Set the number of times this word belongs to bad mails."
293   (aset entry 1 value))
294
295 (defsubst spam-stat-set-score (entry value)
296   "Set the score of this word."
297   (aset entry 2 value))
298
299 (defsubst spam-stat-make-entry (good bad)
300   "Return a vector with the given properties."
301   (let ((entry (vector good bad nil)))
302     (spam-stat-set-score entry (spam-stat-compute-score entry))
303     entry))
304
305 ;; Computing
306
307 (defun spam-stat-compute-score (entry)
308   "Compute the score of this word.  1.0 means spam."
309    ;; promote all numbers to floats for the divisions
310    (let* ((g (* 2.0 (spam-stat-good entry)))
311           (b (float (spam-stat-bad entry))))
312      (cond ((< (+ g b) 5)
313             .2)
314            ((= 0 spam-stat-ngood)
315             .99)
316            ((= 0 spam-stat-nbad)
317             .01)
318            (t
319             (max .01
320                  (min .99 (/ (/ b spam-stat-nbad)
321                              (+ (/ g spam-stat-ngood)
322                                 (/ b spam-stat-nbad)))))))))
323
324 ;; Parsing
325
326 (defmacro with-spam-stat-max-buffer-size (&rest body)
327   "Narrow the buffer down to the first 4k characters, then evaluate BODY."
328   `(save-restriction
329      (when (> (- (point-max)
330                  (point-min))
331               spam-stat-max-buffer-length)
332        (narrow-to-region (point-min)
333                          (+ (point-min) spam-stat-max-buffer-length)))
334      ,@body))
335
336 (defun spam-stat-buffer-words ()
337   "Return a hash table of words and number of occurrences in the buffer."
338   (run-hooks 'spam-stat-washing-hook)
339   (with-spam-stat-max-buffer-size
340    (with-syntax-table spam-stat-syntax-table
341      (goto-char (point-min))
342      (let ((result (make-hash-table :test 'equal))
343            word count)