[WebSiphon Home] [Purity Home]
SiphonScript Tutorial: Shakespearean Insult Generator

"Thou pottle-deep spongy fen-sucked harpy !"


Shakespearean Insult Generator
Topics:

  • Reading Files
  • Parsing Text
  • Using Lists
  • Repeat Statements
  • This insult generator script uses plain text files to serve up random Shakespearean insults to self-afflicting souls on the web using WebSiphon.

    The adjectives.txt and nouns.txt data files are in plain text format. Each line of the file contains an adjective or a noun, followed by a carriage return. The first thing to do is to read in the text files and split() them on the carriage return.

    	adj = readFile(template_path & "adjectives.txt");
    	adj = split(adj, "\r");
    
    	  n = readFile(template_path & "nouns.txt");
    	  n = split(n, "\r");
    

    This gives us two lists, adj and n, containing all of our adjectives and nouns. To build our insult we need up to three adjectives and one noun. We'll start with the adjectives.

    Choosing an adjective is simple, we use the random() function to randomly grab an adjective and then we add it to our chosen list.

    	choice = random(adj);
    	chosenList = chosenList & choice; 
    

    As we want to have more than one adjective in our insult, we want to use a repeat ... times statement to make a up to three choices. Make sure to specify the end of your repeat loop so that only the desired code is repeated.

    	repeat random(3) times
    		choice = random(adj);
    		chosenList = chosenList & choice;
    	end repeat; 
    

    To insure that our insult is interesting and not repetitive, we will add some checking to make sure that each adjective selected is unique. This is a two part process. First we we can use the in comparison operator to determine if the adjective has already been used. Then we use a repeat while loop to keep choosing while the adjective is in the list. Our final adjective selection process looks like this:

    	repeat random(3) times
    		repeat while choice in chosenList
    			choice = random(adj);
    		end repeat;
    		chosenList = chosenList & choice;
    	end repeat; 
    

    Now we can build the insult.

    	insult = "Thou " & chosenList & " " & random(n) & "!";
    
    We use the fact that when a list is coerced into a string then items are separated by a space to simply our lives. We could also step through chosenList with a repeat with statement, but this is far simplier. We choose a random noun and our insult is packed into a variable that we can use anywhere we choose.

    ~ To the curious: This tutorial page was authored on 9/27/97 by hippo, which was then based on a demonstration script written on 10/03/96 by creed. While listening to John Brown Body's "All Time", nnunn revived and tidied up this page due to popular demand on 11/03/04. Enjoy!

    [Top]


    websiphon-support@purity.com Copyright © 1996-2023 Purity Software. All rights reserved.