A "Random Generator" Construction Kit
November 16, 2010 9:37 AM   Subscribe

I'd like to build one of those simple Random Generators that pulls elements from a database and plugs them into a Mad Libs-like structure. Something along the lines of They Fight Crime or any of the online random story generators out there. I work with HTML and CSS every day, and I have some "copy 'n' paste 'n' tweak" experience with JQuery, but I don't really know where to begin building this thing. Any help?

I host my site with BlueHost, which gives me support for MySQL, CGI-BIN, PHP 5, Perl 5, Python, and Ruby/Ruby on Rails. Ideally, there's a sample script & database out there, which I could expand on to create this. Unfortunately, most of what I've found is tutorials for random password generators. Thanks!
posted by Ian A.T. to Computers & Internet (10 answers total) 5 users marked this as a favorite
 
Maybe you could work on a script that creates sentences using a formula or grammar, something like this and variations thereof:

(sentence) := (article) (noun) (verb) (adverb)

(I'm not a linguistics expert so feel free to correct me on this terminology.)

Your database would keep instances of (article), (noun), etc.

How you structure this table is up to you. As a first pass, I would keep a complete table of words, each with a type key field that identifies the word as an article, noun, verb, etc.

Your SQL query would take records of this table filtered by each type key (i.e., filtered by article, by noun, etc.) and order by RAND().

This is like shuffling a deck of cards, where the cards are all articles, all nouns, etc.

Your script would SELECT the first record of each word-type's table, gluing them together to build an ad-lib sentence.

Rinse and repeat with different grammars to build a paragraph, and so on.
posted by Blazecock Pileon at 10:15 AM on November 16, 2010


Yeah, I should have clarified in my question: The structure will be the same every single time, just like Mad Libs. Something like:
The [adjective][person noun] went to the [location noun].
Only the variables will change. I'm really clueless about this sort of stuff though.

Thank you for the response...I hope you didn't mind getting paged to the thread.
posted by Ian A.T. at 10:23 AM on November 16, 2010


Yeah, that's much easier, then. Just keep two tables: a word table and a type table.
Word         | TypeKey
-----------------------
Yellow       | 1
Dog          | 2
Veterinarian | 3
etc.
TypeKey | Type
--------------------
1       | adjective
2       | personNoun
3       | locationNoun
etc.
You could even break nouns down further by NounType codes if you're feeling fancy. It just makes your query a bit more complex.

But ultimately you SELECT on words of various types, ORDER BY RAND() and pull off the first record for each type.
posted by Blazecock Pileon at 10:30 AM on November 16, 2010


This sounds like a great project to learn MySQL and PHP. I recommend the tutorials at tizag.com, they were a huge help when I was getting started with simple web apps. You'll need a "words" table with "adjective" "person" and "location" columns, then you just generate a random number between 0 and the size of each column and select the row at that number. Repeat for each column and string your phrase together.

Or you could do this whole thing in Javascript and just stick your words in different arrays. That would be simpler but not as much fun.
posted by ChrisHartley at 10:37 AM on November 16, 2010


Honestly, a database seems a little like overkill here. You're not going to have users inputting and saving their own words, are you? Why not just have a python list or perl array for each part of the sentence?

I would write a cgi script in python or perl that generates the random sentence and use ssi to include that script on your standard html page.
posted by wayland at 10:50 AM on November 16, 2010


I'm fond of the Dada Engine for this kind of thing. You may have seen its most well-known application, the Postmodernism Generator, mentioned here before. (Full disclosure: I host, but did not create, the pomo generator.)

Dada Engine comes with a bunch of sample scripts that make it pretty easy to figure out how to create your own, and if you have any problems getting it to run as a CGI, feel free to drop me some memail.
posted by hades at 11:56 AM on November 16, 2010


I've created several similar generators by altering the basic PHP script used at Seventh Sanctum. I've got PHP experience, but he explains it well enough that you can do what you need to do without knowing PHP (i.e., plug in sentence structures and words).
posted by telophase at 12:14 PM on November 16, 2010


It sounds a little bit like what I did here: http://lab.artlung.com/random-cells/, just a toy. It's just [adjective] [noun] [verb] [collective noun]. You can mouse over or click or tap to get new ones.

How close to what you want is that?
posted by artlung at 12:23 PM on November 16, 2010


You may also want to look at Markov chain text generation for this, depending on the complexity you are seeking.
posted by artlung at 12:24 PM on November 16, 2010


Thanks, everyone! This is exactly what I needed. I really appreciate it.
posted by Ian A.T. at 2:05 PM on November 16, 2010


« Older Training for a new career in IT   |   What cool things can I do with a spare, jailbroken... Newer »
This thread is closed to new comments.