Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Auto-generate text

  1. #1
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Auto-generate text

    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)?

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    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)]);
    Trinithis

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <span id="randomwords">&nbsp;</span>
    Code:
    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);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not exactly 'future proof', but it works, and some of the phrases are kind of funny:

    Code:
    <!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>
    Last edited by jscheuer1; 09-04-2007 at 02:57 AM. Reason: update words and code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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?
    Trinithis

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Trinithis View Post
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Crikey John. Did you just sit down and write that word list, or did it come from somewhere?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    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. 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
    Last edited by jscheuer1; 09-06-2007 at 04:15 PM. Reason: update word source for accuracy
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I glanced at your library, and I noticed

    Code:
    '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.
    Last edited by Trinithis; 09-04-2007 at 11:56 PM.
    Trinithis

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •