* code snippet: sort threads by mean article score
@ 2002-04-12 11:11 Raphaël Berbain
0 siblings, 0 replies; only message in thread
From: Raphaël Berbain @ 2002-04-12 11:11 UTC (permalink / raw)
Dear Gnusers,
Here is some code I have been using for a couple of weeks now. It
allows to sort threads based on the mean article score in the thread.
Just in case, to my mind "mean article score" == compute total thread
score, then divide by the number of articles in thread.
It is loosely base on gnus-thread-sort-by-total-score and friends,
with simplifications mainly where I didn't understand what some piece
of code did or did not.
To use it, place this code in Gnus' init file, and add
gnus-thread-sort-by-mean-score somewhere in
gnus-thread-sort-functions. It is quite convenient to use the %i
(Article score), %t (Number of articles under the current thread), and
%V (Total thread score) specifiers in gnus-summary-line-format to get
a visual feedback on this stuff. Actually, during development I used:
(defun gnus-thread-mean-score-n (thread)
"Compute mean article score of THREAD, or 0 for nil."
(if (null thread)
0.0
(gnus-thread-mean-score thread)))
(add-to-list 'gnus-summary-line-format-alist
'(?m (format "%f" (gnus-thread-mean-score-n \
(and (boundp 'thread) (car thread)))) ?s))
Standard disclaimer applies, i.e. it works for me - I barely
understand the code I have been modifying. Therefore, any
comments/suggestions/improvements welcome. In particular, I almost
exclusively use small positive scoring (with my setup, article scores
generally go from 0 to 10), and I have no real idea what this code
will do with a whole bunch of negative scores, or higly positive
scores. I mean, it should work, but I don't know if it will make
sense.
A couple of related questions:
- Does Elisp supports functions returning multiple values ?
- While writing (and edebugging) this code, it appeared to me that
thread sort functions are called twice for each comparison -
i.e. suppose at some point in the sort algorithm it needs to compare
thread1 and thread2. (gnus-thread-sort-by-... thread1 thread2) will
be called, then (gnus-thread-sort-by-... thread2 thread1). This
looks quite expensive, especially for a sort function which needs to
walk the whole sub-thread (such as total-score or mean-score). Is it
necessary to call the comparison function twice ?
code follows:
(defun gnus-thread-mean-score-function (&rest p)
"Combines values of the form (total-score . number-of-articles).
total-score are combined by applying gnus-thread-score-function,
number-of-articles use addition."
(let ((rv '(0 . 0))
c)
(while (setq c (car p))
(setq p (cdr p))
(setq rv (cons
(apply gnus-thread-score-function (list (car rv) (car c)))
(apply '+ (list (cdr rv) (cdr c))))))
rv))
(defun gnus-thread-mean-score-1 (thread)
"Compute a value of the form (total-score . number-of-articles) for THREAD.
total-score is computed using gnus-thread-score-function."
(let* ((root (car thread))
(branch (mail-header-number root)))
(apply 'gnus-thread-mean-score-function
(append
(mapcar 'gnus-thread-mean-score-1 (cdr thread))
(when (> branch 0)
(list (cons (or (cdr (assq branch gnus-newsgroup-scored))
gnus-summary-default-score
0)
1)))))))
(defun gnus-thread-mean-score (thread)
"Compute mean article score of THREAD."
(let ((rv (gnus-thread-mean-score-1 thread)))
(/ (float (car rv)) (cdr rv))))
(defun gnus-thread-sort-by-mean-score (h1 h2)
"Sort threads depending on the mean article score in the thread."
(> (gnus-thread-mean-score h1) (gnus-thread-mean-score h2)))
--
Raphaël
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2002-04-12 11:11 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-12 11:11 code snippet: sort threads by mean article score Raphaël Berbain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).