[comp.lang.icon] And now something useless

tenaglia@mis.mcw.edu (Chris Tenaglia - 257-8765) (02/28/90)

Icon is a great language for recreational programming as well. I recently
read a Scientific American where someone described a program that takes a
known text and scrambles it in a most bizaare fashion. The output is not
unlike a Max Headroom monolog. Also they interfaced it with a rhyming
engine to generate bizaare poetry. Well I thought it would be fun to try
doing it in icon. Below is the scrambler. I guess I was lazy not to do the
rhyming engine. It chooses subsequent words based on the likelyhood of them
occuring after the current word. This icon program accomplishes that. I
find it rather amusing what it does to my own documentation. Perhaps someone
has a more clever method, or perhaps someone would want to post a rhyming
engine?

##################### 80 lines follow ############################
#                                                                #
# Poet.Icn               02/28/90          BY TENAGLIA           #
#                                                                #
# THIS PROGRAM TAKES A DOCUMENT AND RE-OUTPUTS IT IN A CLEVERLY  #
# SCRAMBLED FASHION. IT USES THE NEXT TWO MOST LIKELY WORDS TO   #
# TO FOLLOW. USAGE : ICONX POET INPUT_FILE [OUTPUT_FILE]         #
# IF NO OUTPUT FILE IS SPECIFIED, THE GIBBERISH IS SENT TO TTY   #
# THE CONCEPT WAS FOUND IN A RECENT SCIENTIFIC AMERICAN AND ICON #
# SEEMED TO OFFER THE BEST IMPLEMENTATION.                       #
#                                                                #
##################################################################
global vocab,index
procedure main(param)
  source := param[1]        | input("_Source:")
  target := param[2]        | "tt:"
  (in  := open(source))     | stop("Can't open ",source)
  (out := open(target,"w")) | stop("Can't open ",target)
  vocab:= []
  index:= table([])
  write("Loading vocabulary")
  while line := read(in) do
    {
    vocab |||:= parse(line,' ')
    writes(".")
    }
  close(in)

  write("\nindexing...\n")
  every i := 1 to *vocab-2 do index[vocab[i]] |||:= [i]
  index[vocab[-2]] |||:= [-2]    # wrap end to front in order to
  index[vocab[-1]] |||:= [-1]    # prevent stuck loop if last word chosen

  n := -1 ; &random := map(&clock,":","0") ; line := ""
  write("\n")
  every 1 to *vocab/2 do
    {
    (n > 1) | (n := ?(*vocab-2))
    word    := vocab[n]
    follows := vocab[(?(index[word]))+1]
    n       := (?(index[follows])) + 1
    if (*line + *word + *follows + 2) > 80 then
      {
      write(out,line)
      line := ""
      }
    line ||:= word || " " || follows || " "
    }
  write(out,line,".")
  close(out)
  end

##################################################################
#                                                                #
# THIS PROCEDURE PULLS ALL THE ELEMENTS (TOKENS) OUT OF A LINE   #
# BUFFER AND RETURNS THEM IN A LIST. A VARIABLE NAMED 'CHARS'    #
# CAN BE STATICALLY DEFINED HERE OR GLOBAL. IT IS A CSET THAT    #
# CONTAINS THE VALID CHARACTERS THAT CAN COMPOSE THE ELEMENTS    #
# ONE WISHES TO EXTRACT.                                         #
#                                                                #
##################################################################
procedure parse(line,delims)
  static chars
  chars  := &cset -- delims
  tokens := []
  line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))
  return tokens
  end

##################################################################
#                                                                #
# THIS PROCEDURE IS TERRIBLY HANDY IN PROMPTING AND GETTING      #
# AN INPUT STRING                                                #
#                                                                #
##################################################################
procedure input(prompt)
  writes(prompt)
  return read()
  end

Yours truly,

Chris Tenaglia (System Manager)
Medical College of Wisconsin
8701 W. Watertown Plank Rd.
Milwaukee, WI 53226
(414)257-8765
tenaglia@mis.mcw.edu