Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: tuning - corrected shootout entry

19 views
Skip to first unread message

WJ

unread,
Mar 2, 2015, 11:46:56 AM3/2/15
to
Nicolas Neuss wrote:

> (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"))

Gauche Scheme:

(use srfi-13) ; string-downcase string-tokenize

(define (wordcount :optional (port (current-input-port)))
(let1 table (make-tree-map)
(for-each
(^(word) (tree-map-update! table word (cut + 1 <>) 0))
(string-tokenize (string-downcase (port->string port)) #[a-z]))
(for-each
(^x (print (cdr x) " : " (car x)))
(sort (tree-map->alist table) > cdr))))

(call-with-input-string "Foo.b,a:e c(d)e d c b a[foo]FOO" wordcount)

3 : foo
2 : a
2 : b
2 : c
2 : d
2 : e

WJ

unread,
Jan 13, 2016, 9:59:51 PM1/13/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
MatzLisp (Ruby):

tbl = Hash.new(0)
"Foo.b,a:e c(d)e d c b a[foo]FOO".scan(/\w+/){|s| tbl[s.downcase] += 1}
tbl.sort_by{|k,v| [-v,k]}.each{|k,v| puts "#{v} : #{k}"}

3 : foo
2 : a
2 : b
2 : c
2 : d
2 : e

--
The Ortagard school in Rosengard, an area of Malmo with close to 100% Muslim
immigrants, is burning yet again. Several police patrols are called out. But
Prime Minister Persson has already been escorted by special security police
into his bulletproof Volvo....
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
0 new messages