home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / monkeys.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  79 lines

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