1# bogo.R - check calculations performed by bogofilter
2#
3# Before running this script,
4# prepare file bogo.tbl as follows: run
5#   bogofilter -R <mailbox >bogo.tbl
6# where mailbox is a file with only one email message in it.
7
8# Then run R and type
9#   source("bogo.R")
10
11# first read the data
12bogo <- read.table("bogo.tbl")
13attach(bogo)
14
15# next read the special values from the last line
16l <- length(fw)
17bogoS <- fw[l]
18s <- invfwlog[l]
19x <- fwlog[l]
20md <- as.real(levels(U[l])[3])
21
22# now truncate bogo and reset l to match
23detach(bogo)
24bogo[abs(0.5 - bogo$fw) >= md,] -> bogo
25attach(bogo)
26l <- length(fw)
27
28# next recalculate the fw values from the counts in the table
29attach(bogo)
30pw2 <- pbad / (pbad + pgood)
31pw2[pbad + pgood == 0] <- 0
32fw2 <- (s * x + n * pw2) / (s + n)
33
34# display fw (calculated by bogofilter) and fw2 and compare
35print.noquote("R f(w):")
36print(round(fw2, digits=6))
37print.noquote("")
38print.noquote("Bogofilter f(w):")
39print(fw)
40print.noquote("")
41print.noquote("Difference (R - bogo):")
42print(round(fw2, digits=6) - fw)
43
44# calculate S using fw2
45P <- 1 - exp(sum(log(1-fw2))/l)
46Q <- 1 - exp(sum(log(fw2))/l)
47Srob <- ( 1 + (P-Q)/(P+Q) ) / 2
48S <- pchisq(sum(log(1-fw2)) * -2, 2 * l)
49H <- pchisq(sum(log(fw2)) * -2, 2 * l)
50Sfis <- (1 + S - H) / 2
51
52Drob <- abs(Srob - bogoS)
53Dfis <- abs(Sfis - bogoS)
54diff <- min(Drob,Dfis)
55RS <- if(Drob < Dfis) Srob else Sfis
56
57# display S as calculated by bogofilter and by R and compare
58print.noquote("")
59print.noquote(sprintf(
60  "R S: %8.2e, Bogofilter S: %8.2e, Difference (R - bogo): %10.4e",
61  RS, bogoS, diff))
62