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 / scramble.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  94 lines

  1. ############################################################################
  2. #
  3. #    File:     scramble.icn
  4. #
  5. #    Subject:  Program to scramble a document
  6. #
  7. #    Author:   Chris Tenaglia
  8. #
  9. #    Date:     June 14, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. # This program takes a document and re-outputs it in a cleverly
  18. # scrambled fashion. It uses the next two most likely words to
  19. # to follow.
  20. #
  21. # The concept was found in a recent Scientific American and Icon
  22. # seemed to offer the best implementation.
  23. #
  24. ############################################################################
  25. #
  26. #  Links:  random
  27. #
  28. ############################################################################
  29.  
  30. link random
  31.  
  32. global vocab,index
  33.  
  34. procedure main()
  35.   local line, i, n, word, follows
  36.  
  37.   vocab:= []
  38.   index:= table([])
  39.   while line := read() do
  40.     {
  41.     vocab |||:= parse(line,' ')
  42.     }
  43.  
  44.   every i := 1 to *vocab-2 do index[vocab[i]] |||:= [i]
  45.   index[vocab[-2]] |||:= [-2]    # wrap end to front in order to
  46.   index[vocab[-1]] |||:= [-1]    # prevent stuck loop if last word chosen
  47.  
  48.   n := -1 ;
  49.   randomize()
  50.   line := ""
  51.   every 1 to *vocab/2 do
  52.     {
  53.     (n > 1) | (n := ?(*vocab-2))
  54.     word    := vocab[n]
  55.     follows := vocab[(?(index[word]))+1]
  56.     n       := (?(index[follows])) + 1
  57.     if (*line + *word + *follows + 2) > 80 then
  58.       {
  59.       write(line)
  60.       line := ""
  61.       }
  62.     line ||:= word || " " || follows || " "
  63.     }
  64.   write(line,".")
  65.   end
  66.  
  67. #
  68. # This procedure pulls all the elements (tokens) out of a line
  69. # buffer and returns them in a list. A variable named chars
  70. # can be statically defined here or global. It is a cset that
  71. # contains the valid characters that can compose the elements
  72. # one wishes to extract.
  73. #
  74.  
  75. procedure parse(line,delims)
  76.   local tokens
  77.   static chars
  78.  
  79.   chars  := &cset -- delims
  80.   tokens := []
  81.   line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))
  82.   return tokens
  83.   end
  84.  
  85. #
  86. # This procedure is terribly handy in prompting and getting
  87. # an input string
  88. #
  89.  
  90. procedure input(prompt)
  91.   writes(prompt)
  92.   return read()
  93.   end
  94.