PDA

View Full Version : Auto-generate text



bsmcneil
09-03-2007, 10:26 PM
This may be a stupid question - I want to create something that will display 7 words at any given time - and the words are auto-generated (from a list of 100). Can someone point me to or tell me how to do this (or if it's possible)?

Trinithis
09-04-2007, 12:33 AM
var words = [
"hello",
"building",
//and so forth
"newspaper",
"gun"
]

function doSomething(word) {
//code to display random word
alert(word);
}

for(var i=0; i<7; ++i) doSomething(words[Math.floor(Math.random()*words.length)]);

Twey
09-04-2007, 12:45 AM
<span id="randomwords">&nbsp;</span>
Array.prototype.choose = function(n) {
return this.slice().sort(function() { return Math.random() - 0.5; }).slice(0, n);
};

document.getElementById("randomwords").firstChild.nodeValue = "apple banana cranberry dragonfruit elderberry fig grape hackberry iceplant jujube kumquat lychee marang nance okra papaya".split(" ").choose(7);

jscheuer1
09-04-2007, 02:36 AM
Not exactly 'future proof', but it works, and some of the phrases are kind of funny:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>
<script type="text/javascript">
var words=[];
words[0]='Movie';
words[1]='Member';
words[2]='about';
words[3]='Your';
words[4]='Yahooligans';
words[5]='Address';
words[6]='again';
words[7]='Allen';
words[8]='Would';
words[9]='Wink';
words[10]='Antidisestablishmentarianism';
words[11]='Were';
words[12]='Usability';
words[13]='ark';
words[14]='Tripod';
words[15]='become';
words[16]='Transfinite';
words[17]='Behaviors';
words[18]='Being';
words[19]='Belt';
words[20]='Tomfoolery';
words[21]='biggest';
words[22]='this';
words[23]='boring';
words[24]='Things';
words[25]='Thank';
words[26]='canal';
words[27]='Tested';
words[28]='Chimichanga';
words[29]='come';
words[30]='Competitive';
words[31]='Tell';
words[32]='could';
words[33]='Critic';
words[34]='Swank';
words[35]='Dart';
words[36]='Supportability';
words[37]='Debauchery';
words[38]='Definitions';
words[39]='Different';
words[40]='Dig';
words[41]='SuperCow';
words[42]='Disillusion';
words[43]='States';
words[44]='Sphygmomanometer';
words[45]='Software';
words[46]='stigmata';
words[47]='Shenanigans';
words[48]='Engine';
words[49]='Eschew';
words[50]='ever';
words[51]='sent';
words[52]='existence';
words[53]='Exude';
words[54]='Schnitzel';
words[55]='Fandango';
words[56]='figure';
words[57]='Flautist';
words[58]='Follicle';
words[59]='Frankfurter';
words[60]='friend';
words[61]='remember';
words[62]='Recapitulate';
words[63]='Really';
words[64]='quavery';
words[65]='Project';
words[66]='Game';
words[67]='possibly';
words[68]='Glass';
words[69]='goal';
words[70]='Poinsettia';
words[71]='Gymnophilia';
words[72]='please';
words[73]='plan';
words[74]='Places';
words[75]='Phylogeny';
words[76]='Phylactery';
words[77]='here';
words[78]='Phrases';
words[79]='Hipster';
words[80]='Phlebotomies';
words[81]='Huff';
words[82]='Phantasmagorical';
words[83]='People';
words[84]='Pecks';
words[85]='interoperability';
words[86]='justice';
words[87]='Just';
words[88]='Juxtaposition';
words[89]='Kathmandu';
words[90]='King';
words[91]='know';
words[92]='knowledge';
words[93]='known';
words[94]='wagon';
words[95]='Panama';
words[96]='least';
words[97]='Palindrome';
words[98]='Lilliputian';
words[99]='link';
var q=['would', 'were', 'could', 'ever', 'figure', 'remember', 'know'],
p='.';
words.sort(function(){return Math.random()-0.5});
for (var i = 0; i < q.length; i++)
if(words[0].toLowerCase()==q[i])
p='?';
for (var i = 0, a=''; i < 7; i++)
a+=words[i]+(i<6?' ':p);
document.write(a.toLowerCase().replace(/^./,a.charAt(0).toUpperCase()));
</script>
</body>
</html>

Trinithis
09-04-2007, 03:29 AM
What's the purpose of randomly sorting the array, when you could simply access an element at random? Is it so you do not pick the same word twice?

jscheuer1
09-04-2007, 03:53 AM
What's the purpose of randomly sorting the array, when you could simply access an element at random? Is it so you do not pick the same word twice?

Exactly.

Twey
09-04-2007, 03:29 PM
Crikey John. Did you just sit down and write that word list, or did it come from somewhere?

jscheuer1
09-04-2007, 06:57 PM
Crikey John. Did you just sit down and write that word list, or did it come from somewhere?

I Googled 'list of words' or something very close to that and found several interesting lists. I made the words array in my previous post from a list known as Dave's list of words that are fun to say (http://www.uark.edu/~dbruce/list.html). There were tons, so I just selected them all, massaged it into an array in my text editor via regex's, and had that array generate 100 entries for me.

But, I then got into the concept a bit more and found lists that just verbs, nouns, adverbs, or adjectives. I made up my own short list of articles and pronouns and came up with (a work in progress):

http://home.comcast.net/~jscheuer1/side/lib_some.htm

Trinithis
09-04-2007, 09:26 PM
I glanced at your library, and I noticed


'crazy,flipped-out',

You forgot a quote, and besides, you already have 'crazy' in your list.

Also, some of your adverbs seem odd, and are not on Dictionary.com, such as (but not limited to) 'incomprehendingly' and 'incomprehensibly'. The latter could be remedied to 'incomprehensively'.

Consider adding 'well' (ahem...'good'), 'very', and 'quite' to your adverb list, and perhaps 'some' to your articles.

I'm not sure if it matters, but the only articles in the English language are: 'a', 'an', and 'the', but your article array includes some possessive pronouns in it.

Twey
09-04-2007, 10:15 PM
Also, some of your adverbs seem odd, and are not on Dictionary.com, such as (but not limited to) 'incomprehendingly' and 'incomprehensibly'. The latter could be remedied to 'incomprehensively'.'Incomprehensibly' is certainly valid (it's in CIDE), and I believe 'incomprendingly' should be 'uncomprehendingly' but is otherwise a valid mutation of 'uncomprehending' where the gerund is used as an adjective.

jscheuer1
09-04-2007, 10:28 PM
OK, I just got all those words from the web - can't trust everything you get on the web. I took care of crazy,flipped-out. Just got rid of it. I have so many words, I can afford to just get rid of some that are either questionable or just plain wrong. As I said, it is a work in progress. When I'm in the mood, I will go through the list with the spell checker.

The bit about the articles is true. I wasn't meaning to say that the pronouns are articles. I used that as the array name, as I originally conceived of it as just articles, but expanded it later, got a more fitting description?

I'm updating this thing periodically anyway. I just put in a more robust routine to deal with 'a', which only agrees with singular nouns and verbs.

I'm still highly amused by the output, however imperfect.