markov-scm/toplevel.scm

67 lines
2.5 KiB
Scheme

(load "markov.chicken.so")
(import markov
(chicken format)
srfi-18)
(display "Generating table (this may take a while)\n" (current-error-port))
(define table (make-table-from-file "kjv.txt"))
(display "Done generating.\n")
(define (generate-until-word* current collected limits)
(if (member current limits)
(reverse collected)
(generate-until-word* (generate-next-word table current)
(cons current collected)
limits)))
(define (generate-until-word limits)
(generate-until-word* (generate-next-word table (car limits))
'()
limits))
(define (make-a-bible-verse)
(generate-until-word '("startofverse" "startofchapter" "startofbook")))
(define (make-a-book-of-the-bible)
(generate-until-word '("startofbook")))
(define (display-a-book-of-the-bible)
(display (join-verses (make-a-book-of-the-bible))))
(define (generate-continuously* previous-word time book chapter verse)
;; For some reason, default CHICKEN only allows sleeping in increments
;; of 1 second, but THREAD-SLEEP! in SRFI-18 allows for millisecond
;; precision.
(flush-output)
(thread-sleep! time)
(cond
((equal? previous-word "startofbook")
(printf "THE BOOK OF ~A~%~%~%" (generate-next-word table "of"))
(printf "~A:1:1" (+ book 1))
(generate-continuously* (generate-next-word table "startofverse")
time (+ 1 book) 1 1))
((equal? previous-word "startofchapter")
(printf "~%~%~A:~A:1" book (+ 1 chapter))
(generate-continuously* (generate-next-word table "startofverse")
time book (+ 1 chapter) 1))
((equal? previous-word "startofverse")
(printf "~%~A:~A:~A" book chapter (+ 1 verse))
(generate-continuously* (generate-next-word table previous-word)
time book chapter (+ 1 verse)))
(else (let ((first-char (string-ref previous-word 0)))
(if (or (char-alphabetic? first-char)
(char-numeric? first-char))
(printf " ~A" previous-word)
(printf "~A" previous-word))
(generate-continuously* (generate-next-word table previous-word)
time book chapter verse)))))
(define (generate-continuously time)
(generate-continuously* "startofbook" time 0 1 1))
(display "Seeding from truly random source...\n")
(seed-from-entropy)
(display "Try (make-a-bible-verse), (display-a-book-of-the-bible), and (generate-continuously 0.1).\n")