Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 28 |
Nodes: | 6 (1 / 5) |
Uptime: | 46:02:06 |
Calls: | 422 |
Calls today: | 1 |
Files: | 1,024 |
Messages: | 90,336 |
(defun wordcount (&optional (stream *standard-input*)
&aux (*readtable* (copy-readtable)) (table (make-hash-table)))
;; tweak readtable
(loop for char across "\".;,#:()[]{}" do
(set-syntax-from-char char #\Space))
;; count
(loop for word = (read stream nil #\.) until (eq word #\.)
do (incf (gethash word table 0)))
;; output
(let ((*print-pretty* nil))
(loop for (word . count) in
(sort (loop for a being the hash-keys of table using (hash-value b)
collect (cons a b))
#'(lambda (a b)
(or (> (cdr a) (cdr b))
(string<= (car a) (car b)))))
do (format t "~D : ~A~%" count (string-downcase word)))))
;;; Testing:
(wordcount (make-string-input-stream "A b a hello.B, a Hello b"))