markov-scm/markov.chicken.scm

40 lines
1.6 KiB
Scheme

(module markov
(make-table-from-file
guess-sentence
generate-next-word
join-verses
seed-from-entropy)
(import scheme (chicken base) srfi-69
(chicken format)
(chicken random))
(define (seed-from-entropy)
(set-pseudo-random-seed! (random-bytes)))
(define (join-verses* lst str book chapter verse)
(if (null? lst)
str
(let* ((cur (car lst))
(first-character (if (= (string-length cur) 0)
#f
(string-ref cur 0)))
(rest (cdr lst)))
(cond
((not (char? first-character)) (join-verses* rest str
book chapter verse))
((equal? cur "startofbook")
(join-verses* rest (sprintf "~A~%~%~%" str) (+ book 1) 1 1))
((equal? cur "startofchapter")
(join-verses* rest (sprintf "~A~%~%" str) book (+ 1 chapter) 1))
((equal? cur "startofverse")
(join-verses* rest (sprintf "~A~%~A:~A:~A"
str book chapter verse)
book chapter (+ 1 verse)))
((or (char-alphabetic? first-character)
(char-numeric? first-character))
(join-verses* rest (string-append str " " cur)
book chapter verse))
(else (join-verses* rest (string-append str cur)
book chapter verse))))))
(define (join-verses lst)
(string-append (join-verses* lst "THE BOOK OF MARKOV" 1 0 1)
"\n"))
(include "markov.impl.scm"))