random-phrase-generator/random-prase-generator.scm
2023-02-20 15:31:46 +03:00

53 lines
1.6 KiB
Scheme
Executable File

#!/usr/bin/env -S guile -e "main" -s
!#
;; -*- geiser-scheme-implementation: guile -*-
(import (srfi srfi-1)
(ice-9 rdelim)
(ice-9 format))
(define* (read-lines #:optional (port (current-input-port)))
(unfold eof-object?
values
(lambda (_) (read-line))
(read-line)))
(define (random-lines file count)
(let* ((lines (with-input-from-file file read-lines))
(length (length lines)))
(map
(lambda (_) (list-ref lines (random length)))
(iota count))))
(define (random-phrase file words-count)
(let* ((words (random-lines file words-count)))
(string-concatenate
(map
(lambda (w) (string-append w " "))
(append
`(,(string-capitalize (first words)))
(map (lambda (word)
(case (random 10)
((0) (string-append word ","))
((1) (string-append word " -"))
((1) (string-append word ":"))
(else word)))
(reverse (cdr (reverse (cdr words)))))
`(,(string-append (last words)
(case (random 5)
((0) "!")
((1) "?")
((2) "...")
(else ".")))))))))
(define (main args)
(if (null? (cdr args))
(format #t "Usage: ~a <words-list-file.txt>\n" (car args))
(let ((WORDS-FILE (cadr args)))
(set! *random-state* (random-state-from-platform))
(display
(string-upcase
(random-phrase WORDS-FILE (+ 3 (random 4)))))
(newline))))