markov-scm/markov.chicken.scm

44 lines
1.8 KiB
Scheme

(module markov
(make-table-from-file
guess-sentence
generate-next-word
join-verses
%random-seed
seed-from-entropy)
(import scheme (chicken base) srfi-69
(chicken format)
(chicken random)
srfi-4)
(define (seed-from-entropy)
(let ((bytes (blob->u8vector (random-bytes))))
(set! %random-seed (+ (u8vector-ref bytes 0)
(* 255 (u8vector-ref bytes 1))))))
(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"))