(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"))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 55:32:15 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,684 |