/usr/bin/scbayes is in scmail 1.3-4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #! /usr/bin/gosh
;;;
;;; scbayes - simple front-end of scmail/bayesian-filter
;;;
;;; Copyright(C) 2003 by Shiro Kawai (shiro@acm.org)
;;;
;;; Permission to use, copy, modify, distribute this software and
;;; accompanying documentation for any purpose is hereby granted,
;;; provided that existing copyright notices are retained in all
;;; copies and that this notice is included verbatim in all
;;; distributions.
;;; This software is provided as is, without express or implied
;;; warranty. In no circumstances the author(s) shall be liable
;;; for any damages arising out of the use of this software.
;;;
(use gauche.parseopt)
(use gauche.parameter)
(use dbm)
(use dbm.gdbm) ;; should be customizable
(use util.digest)
(use srfi-13)
(use srfi-19)
(use rfc.md5)
(use file.util)
(use scmail.bayesian-filter)
(use scmail)
(use scmail.mail)
(use scmail.mailbox)
(use scmail.config-core)
(use scmail.config)
(use scmail.progress)
(use scmail.util)
;; NB: this script is just for an experiment of bayesian filtering, and
;; not intended to be a robust tool for day-to-day use, although it's quite
;; possible that this will eventually evolve to such a tool.
;; Usage:
;; scbayes --learn-nonspam folder
;; scbayes --learn-spam folder
;; scbayes --check-mail path
;; scbayes --check-folder folder
(define command (lambda () (usage)))
(define *table-file* #f)
(define (table-file)
(or *table-file* (scmail-config-get-path 'token-table)))
(define (check-folder-path mailbox folder)
(unless (or (and (absolute-path? folder) (file-is-directory? folder))
(file-is-directory?
(scmail-mailbox-folder->path mailbox folder)))
(scmail-eformat "no such folder: ~a" folder)))
(define (usage)
(print #`"Usage: ,(sys-basename *program-name*) [options] command")
(print " Mandatory commands (exclusive):")
(print " To build a table: ")
(print " * A directory in absolute path can be specified instead of a folder.")
(print " --learn-nonspam folder... Learn mails in the folder(s) as non-spams")
(print " --learn-spam folder... Learn mails in the folder(s) as spams")
(print " --unlearn-nonspam folder... Unlearn mails in the folder(s) as non-spams")
(print " --unlearn-spam folder... Unlearn mails in the folder(s) as spams")
(print " To test:")
(print " --check-mail path Show spamness of the mail")
(print " --check-spam folder Check spamness of mails in the folder")
(print " --check-nonspam folder Check spamness of mails in the folder")
(print " --table-stat Prints # of entries in the table")
(print " --dump-table Dump entries in the table")
(print " --dump-digest Dump entries in the digest DB")
(print " Options")
(print " --force Don't skip mails already digest")
(print " --slow Learn slowly with periodic sleep")
(print " --flush-interval num Specify interval of flushing [unlimited]")
(print " --table file Specify alternative DB file")
(print " --digest file Specify alternative digest DB file")
(print " -c, --config file Specify alternative config file")
(print " -d, --scmail-dir dir Specify scmail's directory")
(print " -v, --verbose Work noisily (diagnostic output)")
(print " -q, --quiet Suppress all normal output")
(print " -h, --help Display this help and exit")
(exit 0))
(define (temporary-table-file)
(string-append (table-file) ".tmp"))
(define (lock-file)
(string-append (table-file) ",lock"))
(define (prepare-temporary-files)
(define (copy progress src dest)
(call-with-output-file dest
(lambda (oport)
(call-with-input-file src
(lambda (iport)
(let loop ((block (read-block 8192 iport))
(i 1))
(unless (eof-object? block)
(begin
(progress-inc! progress (string-length block))
(display block oport)
(if (and (slow?) (= (modulo i 128) 0)) (sys-sleep 1))
(loop (read-block 8192 iport) (+ i 1))))))))))
(when (and (not (file-exists? (table-file)))
(file-exists? (digest-file)))
(scmail-wformat "~a is found while ~a is not found."
(digest-file) (table-file))
(scmail-eformat "(Please remove ~a if you don't need it.)"
(digest-file)))
(when (file-exists? (table-file))
(let1 progress (make <progress>
:title "prepare"
:total (+ (file-size (table-file))
(if (file-exists? (digest-file))
(file-size (digest-file))
0)))
(copy progress (table-file) (temporary-table-file))
(if (file-exists? (digest-file))
(copy progress (digest-file) (temporary-digest-file)))
(progress-finish! progress))))
(define (swap-files)
(with-signal-handlers
(((list SIGINT SIGHUP SIGTERM) => #f))
(lambda ()
(sys-rename (temporary-table-file) (table-file))
(sys-rename (temporary-digest-file) (digest-file)))))
(define (create-file-exclusively name)
(let ((p (open-output-file name :if-exists #f :if-does-not-exist :create)))
(if p
(begin
(close-output-port p)
#t)
#f)))
(define (lock)
(unless (eq? (create-file-exclusively (lock-file)) #t)
(scmail-wformat "~a is now being updated" (table-file))
(scmail-wformat "or perhaps ~a is staled." (lock-file))
(scmail-eformat "(Please remove the lock file if it is staled.)"))
)
(define force-learn? (make-parameter #f))
(define slow? (make-parameter #f))
(define *digest-file* #f)
(define (digest-file)
(or *digest-file* (scmail-config-get-path 'digest)))
(define digest-db (make-parameter #f))
(define digest-cache (make-parameter (make-hash-table 'string=?)))
(define (temporary-digest-file)
(string-append (digest-file) ".tmp"))
(define (add-to-digest-db! key value)
(dbm-put! (digest-db) key value))
(define (delete-from-digest-db! key value)
(dbm-delete! (digest-db) key))
(define (learned? digest)
(if (or (hash-table-exists? (digest-cache) digest)
(and (digest-db) (dbm-exists? (digest-db) digest)))
#t
#f))
(define (not-learned? digest)
(not (learned? digest)))
(define (mail-digest mail)
(let1 md5 (make <md5>)
(for-each (lambda (name) (digest-update! md5
(scmail-mail-query mail name)))
'(date from message-id subject))
(digest-hexify (digest-final! md5))))
(define (add-to-digest-cache! digest)
(hash-table-put! (digest-cache) digest (number->string (sys-time))))
(define-constant *dbm-class* <gdbm>) ;; should be customizable
(define (open-digest-db file)
(with-error-handler
(lambda (e)
(scmail-eformat "~a" (ref e 'message)))
(lambda ()
(digest-db
(dbm-open *dbm-class* :path file :rw-mode :write)))))
(define (collect-target-files mailbox folder)
(if (and (absolute-path? folder)
(file-is-directory? folder))
(directory-list folder
:children? #t
:add-path? #t
:filter (lambda (x)
(file-is-regular?
(build-path folder
x))))
(scmail-mailbox-mail-list mailbox folder)))
(define (collect-target-files-from-folders mailbox folders)
(apply append
(map (lambda (folder)
(collect-target-files mailbox folder))
folders)))
(define flush-interval (make-parameter 0)) ;; unlimited
(define (learn-common table-type folders
task-name skip? process-words update-digest-db!)
(define (flush-token-table-cache progress)
(token-table-cache-flush
(lambda (i)
(progress-inc! progress)
(if (and (slow?) (= (modulo i 100) 0)) (sys-sleep 1))
)))
(define (flush-digest-cache progress)
(let1 counter 1
(hash-table-for-each
(digest-cache)
(lambda (key value)
(update-digest-db! key value)
(progress-inc! progress)
(if (and (slow?) (= (modulo counter 100) 0)) (sys-sleep 1))
(inc! counter)))
(digest-cache (make-hash-table 'string=?))))
(define (flush-both-cache)
(let1 total (+ (length (hash-table-keys (digest-cache)))
(token-table-cache-length))
(if (> total 0)
(let1 progress (make <progress>
:title "flush"
:total total
:bar-mark #\.)
(flush-token-table-cache progress)
(flush-digest-cache progress)
(progress-finish! progress)))))
(define (learn-files files)
(let ((learned-file-count 0)
(progress (make <progress>
:title task-name
:total (length files))))
(for-each (lambda (file)
(let* ((mail (make <mail> :file file))
(digest (mail-digest mail)))
(if (or (force-learn?) (not (skip? digest)))
(with-error-handler
(lambda (e)
(scmail-wformat "~a: ~a" file (ref e 'message)))
(lambda ()
(process-words mail table-type)
(add-to-digest-cache! digest)
(inc! learned-file-count)
(if (slow?) (sys-sleep 1))
(when (and (> (flush-interval) 0)
(= (modulo learned-file-count
(flush-interval)) 0))
(newline)
(flush-both-cache))
(scmail-dformat "~a: ~a: ~a"
learned-file-count task-name file)
))
(scmail-dformat "skip: ~a" file))
(progress-inc! progress)))
files)
(progress-finish! progress)
learned-file-count))
;; FIXME: copied from progress.scm
(define (time-difference->real time0 time1)
(let1 time (time-difference time0 time1)
(+ (time-second time)
(/ (time-nanosecond time) 1000000000))))
(define (.xx number)
(/ (round (* number 100)) 100))
(define (report start-time learned-file-count)
(let1 elapsed (time-difference->real (current-time) start-time)
(format #t "summary: ~:d ~a mails ~a ~aed in ~:d sec. (~a mails/sec.)\n"
learned-file-count
(if (eq? table-type token-table-index-of-spam)
"spam"
"nonspam")
(if (<= learned-file-count 1) "is" "are")
task-name
(.xx elapsed)
(if (> elapsed 0)
(.xx (/ learned-file-count elapsed))
"NaN")
)))
(define (cleanup)
(if (file-exists? (temporary-table-file))
(sys-unlink (temporary-table-file)))
(if (file-exists? (temporary-digest-file))
(sys-unlink (temporary-digest-file)))
(sys-unlink (lock-file)))
(let1 mailbox (make-scmail-mailbox (ref (scmail-config) 'mailbox-type)
(ref (scmail-config) 'mailbox))
(scmail-config-make-directory)
(for-each (lambda (folder) (check-folder-path mailbox folder)) folders)
(lock)
(let ((learned-file-count 0)
(start-time (current-time)))
(dynamic-wind
(lambda () #t)
(lambda ()
(prepare-temporary-files)
(open-digest-db (temporary-digest-file))
(with-token-table
(temporary-table-file) :write
(lambda ()
(let ((target-files
(collect-target-files-from-folders mailbox folders)))
(set! learned-file-count (learn-files target-files))
(flush-both-cache)
)))
(swap-files)
(report start-time learned-file-count)
)
(lambda ()
(cleanup)
)))))
(define (learn table-type folders)
(learn-common table-type folders
"learn" learned? token-table-collect-words
add-to-digest-db!))
(define (unlearn table-type folders)
(learn-common table-type folders
"unlearn" not-learned? token-table-discard-words
delete-from-digest-db!))
(define (check-spamness-of-mail file)
(unless (file-is-readable? file)
(scmail-eformat "can't read ~a" file))
(with-token-table
(table-file) :read
(lambda ()
(receive (prob lang words)
(spamness-of-mail
(make <mail> :file file))
(print file)
(print #`" ,prob")
(for-each (lambda (w)
(print #`" ,(car w) : ,(cdr w)"))
words)
prob)))
0)
;; (test-spamness-of-files nonspam-files #f)
;; (test-spamness-of-files spam-files #t)
(define (test-spamness-of-files files expect-spam?)
(let ((threshold 0.9)
(count 0)
(bad '())
(mailbox-type (ref (scmail-config) 'mailbox-type)))
(for-each (lambda (file)
(let1 mail (make-scmail-mail mailbox-type :file file)
(inc! count)
(receive (prob lang words) (spamness-of-mail mail)
(when (or (and expect-spam?
(< prob threshold))
(and (not expect-spam?)
(>= prob threshold)))
(push! bad (list file prob words))
))))
files)
(print #`"Out of ,count messages")
(print #`" ,(length bad) messages are identified incorrectly:")
(for-each (lambda (entry)
(print
#`" ,(sys-basename (car entry)) (score=,(cadr entry)):")
(for-each (lambda (w)
(print #`" ,(car w) : ,(cdr w)"))
(caddr entry)))
bad)
(length bad)
))
(define (check-spamness-in-folder folder spam?)
(let ((number-of-incorrect-answers 0)
(mailbox (make-scmail-mailbox (ref (scmail-config) 'mailbox-type)
(ref (scmail-config) 'mailbox))))
(check-folder-path mailbox folder)
(with-token-table
(table-file) :read
(lambda ()
(set! number-of-incorrect-answers
(test-spamness-of-files (collect-target-files mailbox folder)
spam?))))
number-of-incorrect-answers))
(define (table-stat)
(with-token-table
(table-file) :read
(lambda ()
(let ((mcount (token-table-message-count))
(tcount (token-table-token-count))
(totals (make-list (token-table-number-of-values) 0)))
(format #t "lang nonspam spam\n")
(dolist (lang (token-table-languages))
(let1 v (list (ref tcount (token-table-index-of-nonspam lang))
(ref mcount (token-table-index-of-nonspam lang))
(ref tcount (token-table-index-of-spam lang))
(ref mcount (token-table-index-of-spam lang)))
(apply format #t "~4a: ~7dw/~5dm ~7dw/~5dm\n" lang v)
(set! totals (map + totals v))))
(apply format #t "total:~7dw/~5dm ~7dw/~5dm\n" totals))))
0)
(define (dump-table)
(with-token-table
(table-file) :read
(lambda ()
(token-table-for-each
(lambda (key value)
(unless (string-prefix? (token-table-special-key-prefix) key)
(format #t "~a\t~a\n" key value))))))
0)
(define (dump-digest)
(open-digest-db (digest-file))
(dbm-for-each (digest-db)
(lambda (key value)
(format #t "~a\t~a\n" key value)))
0)
;;(define (update-db old-db)
;; (format #t #`"converting ,old-db to ,(table-file)...")
;; (flush)
;; (convert-database old-db (table-file))
;; (print "done.")
;; 0)
(define (main args)
(define folders '())
(define (get-folders)
(if (null? folders)
(scmail-eformat "one or more folders should be specified"))
folders)
(define (learn-internal table)
(learn table (get-folders))
0)
(define (unlearn-internal table)
(unlearn table (get-folders))
0)
(scmail-set-program-name! (car args))
(scmail-check-gauche-version)
(let* ((config-file (scmail-config-default-file))
(verbose-mode? #f)
(quiet-mode? #f)
(rest
(parse-options
(cdr args)
(("learn-nonspam" ()
(set! command (lambda ()
(learn-internal token-table-index-of-nonspam))))
("learn-spam" ()
(set! command (lambda ()
(learn-internal token-table-index-of-spam))))
("unlearn-nonspam" ()
(set! command (lambda ()
(unlearn-internal token-table-index-of-nonspam))))
("unlearn-spam" ()
(set! command (lambda ()
(unlearn-internal token-table-index-of-spam))))
("check-mail=s" (file)
(set! command (lambda () (check-spamness-of-mail file))))
("d|scmail-dir=s" (dir)
(scmail-config-set-directory! dir))
("check-nonspam=s" (folder)
(set! command (lambda () (check-spamness-in-folder folder #f))))
("check-spam=s" (folder)
(set! command (lambda () (check-spamness-in-folder folder #t))))
("dump-table" ()
(set! command (lambda () (dump-table))))
("dump-digest" ()
(set! command (lambda () (dump-digest))))
("table-stat" ()
(set! command (lambda () (table-stat))))
;;("update-db=s" (old-db)
;; (set! command (lambda () (update-db old-db))))
("c|config=s" (file)
(set! config-file file))
("table=s" (table)
(set! *table-file* (expand-path table)))
("digest=s" (file)
(set! *digest-file* (expand-path file)))
("force" () (force-learn? #t))
("slow" () (slow? #t))
("flush-interval=i" (i) (flush-interval i))
("v|verbose" () (set! verbose-mode? #t))
("q|quiet" () (set! quiet-mode? #t))
("h|help" () (usage))
(else _ (usage))))))
(if verbose-mode? (scmail-config-set-verbose-mode!))
(set! folders rest)
(with-output-to-port (if quiet-mode?
(open-output-file "/dev/null")
(standard-output-port))
(lambda ()
(scmail-config-read config-file)
(command)))
))
;; Local variables:
;; mode: scheme
;; end:
|