rankmsg.ml 4.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
(***********************************************************************)
(*                                                                     *)
(*                 SpamOracle -- a Bayesian spam filter                *)
(*                                                                     *)
(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 2002 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  This file is distributed under the terms of the   *)
(*  GNU Public License version 2, http://www.gnu.org/licenses/gpl.txt  *)
(*                                                                     *)
(***********************************************************************)

Ralf Treinen's avatar
Ralf Treinen committed
13
(* $Id$ *)
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

(* Message ranking *)

open Mail
open Database

let word_count_in w res =
  let count = ref 0 in
  for i = 0 to Array.length res - 1 do
    if w = fst res.(i) then incr count
  done;
  !count

let add_word w p res =
  let i = ref 0 in
  while !i < Array.length res
     && abs_float (p -. 0.5) <= abs_float(snd res.(!i) -. 0.5)
  do
    incr i
  done;
  if !i < Array.length res then begin
    for j = Array.length res - 1 downto !i + 1 do
      res.(j) <- res.(j - 1)
    done;
    res.(!i) <- (w, p)
  end

let normalize (p : float) low high =
  if p > high then high else if p < low then low else p

Ralf Treinen's avatar
Ralf Treinen committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
let cap (p : float) =
  if p > 1.0 then 1.0 else p

let word_proba g b num_g num_b =
  let g = 2 * g in  (* Graham's magic factor to bias in favor of ham *)
  let pgood = cap (float g /. float num_g)
  and pbad = cap (float b /. float num_b) in
  let p = pbad /. (pgood +. pbad) in
  if !Config.robinson_s = 0.0 then
    normalize p !Config.low_freq_limit !Config.high_freq_limit
  else begin
    (* Robinson's adjustement *)
    let n = float (g + b) in
    let p =
      (!Config.robinson_s *. !Config.robinson_x +. n *. p)
      /. (!Config.robinson_s +. n) in
    (* Result normalization *)
    normalize p !Config.low_freq_limit !Config.high_freq_limit
  end

64 65 66 67
let process_word (db, res) w =
  try
    let (g, b) = Hashtbl.find db.s_freq w in
    if word_count_in w res < !Config.max_repetitions then begin
Ralf Treinen's avatar
Ralf Treinen committed
68
      let p = word_proba g b db.s_num_good db.s_num_spam in
69 70 71 72 73
      add_word w p res
    end
  with Not_found ->
    ()

Ralf Treinen's avatar
Ralf Treinen committed
74 75 76
let process_words ((db, res) as ctx) txt =
  Wordsplit.iter (process_word ctx) (in_short db) txt;
  if !Config.summarize_referenced then Refhosts.add txt
77 78 79 80

let process_msg ctx m =
  iter_message (process_words ctx) m

Ralf Treinen's avatar
Ralf Treinen committed
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
(* This is Graham's original approach *)

let spaminess_score_graham res =
  let p = ref 1.0 and pexp = ref 0
  and cp = ref 1.0 and cpexp = ref 0 in
  for i = 0 to Array.length res - 1 do
    let (_, x) = res.(i) in
    p := !p *. x;
    if !p <= 1e-100 then begin 
      let (m, e) = frexp !p in p := m; pexp := !pexp + e
    end;
    cp := !cp *. (1.0 -. x);
    if !cp <= 1e-100 then begin
      let (m, e) = frexp !cp in cp := m; cpexp := !cpexp + e
    end
  done;
  if !cpexp < !pexp then cp := ldexp !cp (!cpexp - !pexp)
  else if !cpexp > !pexp then p := ldexp !p (!pexp - !cpexp);
  !p /. (!p +. !cp)

(* This is Robinson's chi-square stuff *)

let chi2_inverse m n =   (* chi2 inverse of 2m with 2n degrees *)
  let t = ref (exp (-. m)) in
  let s = ref !t in
  for i = 1 to n do
    t := !t *. m /. float i;
    s := !s +. !t
  done;
  if !s >= 1.0 then 1.0 else !s

let log2 = log 2.0

let chi2_hypothesis ps =
  (* Compute -2 * ln (product ps).  Be careful with underflows. *)
  let p = ref 1.0 and pexp = ref 0 in
  for i = 0 to Array.length ps - 1 do
    p := !p *. ps.(i);
    if !p <= 1e-100 then begin
      let (x, e) = frexp !p in p := x; pexp := !pexp + e
    end
  done;
  chi2_inverse (-. (log !p +. log2 *. float !pexp)) (Array.length ps)

let spaminess_score_robinson res =
  let probs = Array.map snd res in
  let cprobs = Array.map (fun x -> 1.0 -. x) probs in
  0.5 *. (1.0 +. chi2_hypothesis probs -. chi2_hypothesis cprobs)
129 130 131 132 133 134 135

type rank =
  { spam_prob: float;
    num_meaningful: int;
    explanation: string }

let rank_message db msg =
Ralf Treinen's avatar
Ralf Treinen committed
136
  Refhosts.reset();
137 138
  let res = Array.make !Config.num_words_retained ("", 0.5) in
  process_msg (db, res) msg;
Ralf Treinen's avatar
Ralf Treinen committed
139 140 141 142
  let p =
    if !Config.use_chi_square
    then spaminess_score_robinson res
    else spaminess_score_graham res in
143 144 145 146 147 148 149 150 151 152 153 154
  let meaningful = ref 0 in
  while !meaningful < Array.length res && fst res.(!meaningful) <> ""
  do incr meaningful done;
  let summary = Buffer.create 200 in
  for i = 0 to !meaningful - 1 do
    let (w, p) = res.(i) in
    Printf.bprintf summary "%s:%02d " w (truncate (p *. 100.0))
  done;
  { spam_prob = p;
    num_meaningful = !meaningful;
    explanation = Buffer.contents summary }