(load "textgen.ss")

(define capitalize
  (lambda (val)
    val))

(define parts-of-speech
   (list 'adjective 'article 'noun 'tverb 'iverb 'proper-noun))

(define sentence
  (lambda ()
    (let ((choice (random 100)))
      (cond
        ; 30% of the time, we use a simple subject/verb sentence.
        ((< choice 30)
         (string-append (capitalize (noun-phrase))
                        space
                        (generate-part-of-speech 'iverb)
                        period))
        ; 65% of the time, we use a subject/verb/object sentence.
        ((< choice 95)
         (string-append (capitalize (noun-phrase))
                        space
                        (generate-part-of-speech 'tverb)
                        space
                        (noun-phrase)
                        period))
        ; The remaining 5% of the time, we use an exclamation!
        (else (string-append (capitalize (generate-part-of-speech 'exclamation))
                             exclamation-point))))))

(define structure->string
  (lambda (structure)
    (if (null? structure)
        ""
        (string-append (if (string? (car structure))
                           (car structure)
                           (generate-part-of-speech (car structure)))
                       (structure->string (cdr structure))))))

(define random-structure
  (lambda (fname)
    (let ((port (open-input-file fname))
          (rnd (random 1000)))
      (letrec ((kernel (lambda (num)
                         (let ((entry (read port)))
                           (if (< num (cadr entry))
                               (begin (close-input-port port) 
                                      (structure->string (car entry)))
                               (kernel (- num (cadr entry))))))))
        (kernel rnd)))))

