spacepaste

  1.  
  2. #!/usr/bin/env bash
  3. # -*- mode: scheme -*-
  4. exec guile $0
  5. # !#
  6. (import (ice-9 rdelim)
  7. (ice-9 hash-table))
  8. (define (count-or-unicode>? a b)
  9. (let ((Na (car a))(Nb (car b)))
  10. (or (> Na Nb)
  11. (and (= Na Nb)
  12. (string<? (cdr a) (cdr b))))))
  13. (define (count-words port)
  14. (define delimiters " \t\n")
  15. (define delimiters-set (string->char-set delimiters))
  16. (define buf (make-string 10000))
  17. (define (read-word port)
  18. (let ((res (%read-delimited! delimiters buf #t port)))
  19. (or (and (eof-object? (car res))
  20. (car res))
  21. (substring buf 0 (cdr res)))))
  22. (define count (make-hash-table (inexact->exact 1e5)))
  23. (define (add-word next)
  24. (hash-set! count next
  25. (1+ (hash-ref count next 0))))
  26. (let loop ((next (read-word port)))
  27. (unless (eof-object? next)
  28. (unless (string-every delimiters-set next)
  29. (add-word next))
  30. (loop (read-word port))))
  31. (sort! ;; destructive sort to save memory
  32. (hash-map->list (lambda (key val) (cons val key)) count)
  33. count-or-unicode>?))
  34. (for-each
  35. (lambda (x) (format #t "~a\t~a\n" (cdr x) (car x)))
  36. (count-words (current-input-port)))
  37.