home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / exoten / icon / monkeys.icn < prev    next >
Encoding:
Text File  |  1990-03-08  |  1.9 KB  |  70 lines

  1. ############################################################################
  2. #
  3. #    Name:    monkeys.icn
  4. #
  5. #    Title:    Generate random text
  6. #
  7. #    Author:    Stephen B. Wampler, modified by Ralph E. Griswold
  8. #
  9. #    Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #
  13. #  The old monkeys at the typewriters anecdote ...
  14. #  
  15. #     This program uses ngram analysis to randomly generate text in
  16. #  the same 'style' as the input text.  The arguments are:
  17. #  
  18. #     -s     show the input text
  19. #     -n n   use n as the ngram size (default:3)
  20. #     -l n   output at about n lines (default:10)
  21. #     -r n   set random number seed to n
  22. #  
  23. ############################################################################
  24. #
  25. #  Links: options
  26. #
  27. ############################################################################
  28.  
  29. link options
  30.  
  31. procedure main(args)
  32.    local switches, n, linecount, ngrams, preline
  33.    local line, ngram, nextchar, firstngram, Show
  34.  
  35.    switches := options(args,"sn+l+r+")
  36.    if \switches["s"] then Show := writes else Show := 1
  37.    n := \switches["n"] | 3
  38.    linecount := \switches["l"] | 10
  39.  
  40.    ngrams := table()
  41.  
  42.    Show("Orginal Text is: \n\n")
  43.  
  44.    preline := ""
  45.    every line := preline || !&input do {
  46.       Show(line)
  47.       line ? {
  48.             while ngram := move(n) & nextchar := move(1) do {
  49.                /firstngram := ngram
  50.                /ngrams[ngram] := ""
  51.                ngrams[ngram] ||:= nextchar
  52.                move(-n)
  53.                }
  54.             preline := tab(0) || "\n"
  55.             }
  56.       }
  57.  
  58.    Show("\n\nGenerating Sentences\n\n")
  59.  
  60.    ngram := writes(firstngram)
  61.    while linecount > 0 do {
  62.       if /ngrams[ngram] then
  63.          exit()                 # if hit EOF ngram early
  64.       ngram := ngram[2:0] || writes(nextchar := ?ngrams[ngram])
  65.       if (nextchar == "\n") then
  66.          linecount -:= 1
  67.       }
  68.  
  69. end
  70.