-
- (import (ice-9 rdelim)
- (ice-9 hash-table))
-
- (define (count-or-unicode>? a b)
- (let ((Na (car a))(Nb (car b)))
- (if (> Na Nb)
- #t
- (if (= Na Nb)
- (string<? (cdr a) (cdr b))
- #f))))
-
- (define (skip-whitespace port)
- (let ([ch (peek-char port)])
- (when (and (not (eof-object? ch)) (char-whitespace? ch))
- (read-char port)
- (skip-whitespace port))))
-
- (define (count-words port)
- (define (read-word port)
- (skip-whitespace port)
- (let ((line (read-delimited "\t" port)))
- (if (eof-object? line)
- #f
- (map (λ (x) (string-split x #\space))
- (string-split line #\newline)))))
- (define count (make-hash-table 100000))
- (define (add-word next)
- (unless (string-null? next)
- (hash-set! count next
- (1+ (hash-ref count next 0)))))
- (let loop ((next (read-word port)))
- (when next
- (for-each (lambda (wl)
- (for-each (lambda (w) (add-word w)) wl))
- next)
- (loop (read-word port))))
- (sort! ;; destructive sort to save memory
- (hash-map->list (lambda (key val) (cons val key)) count)
- count-or-unicode>?))
-
- (for-each
- (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x)))
- (count-words (current-input-port)))
-