|
|
|
@ -2,42 +2,18 @@
|
|
|
|
|
exec guile -e '(@ (count-words) main)' -s "$0" "$@" |
|
|
|
|
!# |
|
|
|
|
|
|
|
|
|
(define-module (count-words) |
|
|
|
|
(define-module (word-counter) |
|
|
|
|
#:use-module (bible-tools) |
|
|
|
|
#:use-module (count-words) |
|
|
|
|
#:use-module (srfi srfi-1) |
|
|
|
|
#:use-module (srfi srfi-26) |
|
|
|
|
#:export (main)) |
|
|
|
|
|
|
|
|
|
(define (split/stuff str) |
|
|
|
|
(let ((words (string-split str #\space)) |
|
|
|
|
(fs (map (lambda (char) |
|
|
|
|
(lambda (str) (string-split str char))) |
|
|
|
|
'(#\, #\: #\. #\! #\? #\; #\< #\>)))) |
|
|
|
|
(filter (lambda (str) (not (string=? "" str))) |
|
|
|
|
(fold (lambda (f val) (mapcan f val)) words fs)))) |
|
|
|
|
|
|
|
|
|
(define (count verse result) |
|
|
|
|
(let ((words (split/stuff verse))) |
|
|
|
|
(fold (lambda (word res) |
|
|
|
|
(let ((val (assoc word res))) |
|
|
|
|
(if val |
|
|
|
|
(alist-cons word (+ (cdr val) 1) res) |
|
|
|
|
(alist-cons word 1 res)))) |
|
|
|
|
result words))) |
|
|
|
|
|
|
|
|
|
(define (count-words verses) |
|
|
|
|
(delete-duplicates (sort (fold count '() verses) |
|
|
|
|
(lambda (a b) (> (cdr a) (cdr b)))) |
|
|
|
|
(lambda (a b) (equal? (car a) (car b))))) |
|
|
|
|
|
|
|
|
|
(define (show-meta txt) |
|
|
|
|
(let ((count (count-words (map text txt)))) |
|
|
|
|
(for-each (lambda (word) |
|
|
|
|
(display |
|
|
|
|
(string-append (car word) |
|
|
|
|
":\t" |
|
|
|
|
(number->string (cdr word)) |
|
|
|
|
"\n"))) |
|
|
|
|
count))) |
|
|
|
|
(define (tabs w) (if (< (string-length (car w)) 7) "\t\t" "\t")) |
|
|
|
|
(define (to-str w) (string-append (car w) ":" (tabs w) |
|
|
|
|
(number->string (cdr w)) "\n")) |
|
|
|
|
(for-each (compose display to-str) (count-words (map text txt)))) |
|
|
|
|
|
|
|
|
|
(define (help) |
|
|
|
|
(display "Usage: count-words [book] [-c chapter] [-h] [-v]\n") |
|
|
|
@ -48,18 +24,15 @@ exec guile -e '(@ (count-words) main)' -s "$0" "$@"
|
|
|
|
|
(define (version) |
|
|
|
|
(display "count-words v1.0.0\n")) |
|
|
|
|
|
|
|
|
|
(define (show-book book) |
|
|
|
|
(lambda (bible) |
|
|
|
|
(show-meta (get-book book bible)))) |
|
|
|
|
|
|
|
|
|
(define (show-chapter book chapter) |
|
|
|
|
(lambda (bible) |
|
|
|
|
(show-meta (get-chapter chapter (get-book book bible))))) |
|
|
|
|
|
|
|
|
|
(define (main args) |
|
|
|
|
(define flag? (cut member <> args)) |
|
|
|
|
(define get (compose cadr flag?)) |
|
|
|
|
(define-syntax conf |
|
|
|
|
(syntax-rules (else) |
|
|
|
|
((_ (f e)... (else g)) (cond ((flag? f) e)... (else g))))) |
|
|
|
|
(with-bible "jantzen" |
|
|
|
|
(cond ((< (length args) 2) (help)) |
|
|
|
|
((member "-h" args) (help)) |
|
|
|
|
((member "-v" args) (version)) |
|
|
|
|
((member "-c" args) (show-chapter (cadr args) (string->number (cadr (member "-c" args))))) |
|
|
|
|
(else (show-book (cadr args)))))) |
|
|
|
|
(conf ("-h" (help)) |
|
|
|
|
("-v" (version)) |
|
|
|
|
("-c" |
|
|
|
|
(call-with-chapter (cadr args) (get "-c") show-meta)) |
|
|
|
|
(else (call-with-book (cadr args) show-meta))))) |
|
|
|
|