Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Question Help with generating random words..

    I'm trying to find a much simpler way than this current script I am using to generate and print a random word. I don't want to have to name each word but instead just include a very large list of about 300,000 words that javascript uses to pull a random word out of. Can you please help me out?? The list of words I have is a straight list with no commas or anything.

    I am currently outputting to a text box, but I am looking to generate words according to users input. For example, a user wants 500 words starting with the letter "b" and is less than 5 characters long appended to the character "de."

    Any help would be awesome! Even a right direction. I'm also willing to pay some for such a script if need be. Thanks!

    Here's my code:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    
    var NumberOfWords = 28
    
    var words = new BuildArray(NumberOfWords)
    
    words[1] = "czarevitch"
    words[2] = "brightwork"
    words[3] = "verkrampte"
    words[4] = "protectrix"
    words[5] = "nudibranch"
    words[6] = "grandchild"
    words[7] = "newfangled"
    words[8] = "flugelhorn"
    words[9] = "mythologer"
    words[10] = "pluperfect"
    words[11] = "jellygraph"
    words[12] = "quickthorn"
    words[13] = "rottweiler"
    words[14] = "technician"
    words[15] = "cowpuncher"
    words[16] = "middlebrow"
    words[17] = "jackhammer"
    words[18] = "triphthong"
    words[19] = "wunderkind"
    words[20] = "dazzlement"
    words[21] = "jabberwock"
    words[22] = "witchcraft"
    words[23] = "pawnbroker"
    words[24] = "thumbprint"
    words[25] = "motorcycle"
    words[26] = "cryptogram"
    words[27] = "torchlight"
    words[28] = "bankruptcy"
    
    function BuildArray(size){
        this.length = size
        for (var i = 1; i <= size; i++){
            this[i] = null}
        return this
    }
    
    function PickRandomWord(frm) {
        var rnd = Math.ceil(Math.random() * NumberOfWords)
    
        // Display the word inside the text box
        frm.WordBox.value = words[rnd]
    }
    //-->
    </SCRIPT>

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    I'ld love to help you - 300,000 words will take a couple of megs of data - so you might want to to consider writing it server side instead - on the other hand many page loads (including images) today are a few megs so it's doable in javascript if you want to.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    I understood that the user should be able to control output:
    1) Set a max length for the words he wants.
    2) Set how many words he want.

    The last one I didn't understand 'appended to the character "de"' - please explain!

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Also what format do you have the word list in now.
    Plain text file?
    What are the words separated with? Line breaks? Something else?

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Could you put the list somewhere (on a server) so I can download and have a look?

  • #6
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts
    the user should have the option to add any word to the random generated words... so the user enters "Fork" and specifies that he wants it at the beginning of the word and gets...

    ForkBank
    ForkBurn
    ForkBase
    ForkBlue... etc

    The list is a long list separated by line breaks in just a text file. One word below the other. User should also be able to specify how long the word should be and what letter it starts with.

    This is the list online...

    http://www.freebsd.org/cgi/cvsweb.cg...e=text%2Fplain

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    The "forking" part needs some server-side scripting.
    I don't have time for that right now (and I don't know what language you want it in PHP ASP .net anyway.)

    You will have to make do with this for now
    http://www.clientsmile.se/words

    zipped: http://www.clientsmile.se/words.zip

    Code:
    getWords = (function(){
      
      var theList;
      
      // sort in random order
      Array.prototype.randomSort = function(){
        this.sort(function(){
          return (Math.round(Math.random())-0.5);
        })
      };
      
      // read a file using SJAX
      var getFile = function (url){
        var req = window.XMLHttpRequest && new XMLHttpRequest(); 
        !req && (req = new ActiveXObject("Microsoft.XMLHTTP"));
        req && req.open("GET", url, false) 
        req && req.send(null);
        !req && (req = {responseText:false});
        var r = req.responseText;
        return r
      };
      
      // read our wordlist to the array theList
      var readListToArray = function(){
        theList = getFile('wordlist.txt').split('\n');
      };
      
      // find words matching a regular expressions and a max length
      var findWords = function(regExp,maxLength,minLength){
        var filtered = [];
        for(var i = 0; i < theList.length; i++){
          theList[i].length >= minLength 
          && theList[i].length <= maxLength 
          && !theList[i].replace(regExp,'') 
          && filtered.push(theList[i]);
        };
        return filtered
      };
      
      // make a random selection from a list of ok words
      var randomSelection = function(subList,numberOfWords,randomOrder){
        subList.randomSort();
        subList = subList.slice(0,numberOfWords)
        !randomOrder && subList.sort()
        return subList
      };
      
      // react on user input
      var getWords = function(f){
        var rExp = new RegExp(f.startWith.value + '.*' + f.endWith.value,'gi');
        var maxLengthOfWords = f.maxLengthOfWords.value;
        maxLengthOfWords = isNaN(maxLengthOfWords) ? 100 : maxLengthOfWords/1;
        var minLengthOfWords = f.minLengthOfWords.value;
        minLengthOfWords = isNaN(minLengthOfWords) ? 1 : minLengthOfWords/1;
        var numberOfWords = f.numberOfWords.value;
        numberOfWords = isNaN(numberOfWords) ? 50 : numberOfWords/1;
        var randomOrder = f.randomOrder.checked;
        var okWords = findWords(rExp,maxLengthOfWords,minLengthOfWords);
        f.yourWords.value = randomSelection(okWords,numberOfWords,randomOrder).join('\n');
      };
      
      // init
      readListToArray();
      
      // return a public function
      // (timeout for debugging - this way we never send the form even if bug occurs)
      return function(f){
        setTimeout(function(){getWords(f)},1)
      };
    })();

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Hm, I missunderstood the "forking" part - thought you meant the user should be able to create an alternative word list... add words permanently to the word list. The thing you mean is doable... - prefixing words.

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Ok, done with the prefix (changed the zip-file etc above):
    Code:
    getWords = (function(){
      
      var theList;
      
      // sort in random order
      Array.prototype.randomSort = function(){
        this.sort(function(){
          return (Math.round(Math.random())-0.5);
        })
      };
      
      // read a file using SJAX
      var getFile = function (url){
        var req = window.XMLHttpRequest && new XMLHttpRequest(); 
        !req && (req = new ActiveXObject("Microsoft.XMLHTTP"));
        req && req.open("GET", url, false) 
        req && req.send(null);
        !req && (req = {responseText:false});
        var r = req.responseText;
        return r
      };
      
      // read our wordlist to the array theList
      var readListToArray = function(){
        theList = getFile('wordlist.txt').split('\n');
      };
      
      // find words matching a regular expressions and a max length
      var findWords = function(regExp,maxLength,minLength){
        var filtered = [];
        for(var i = 0; i < theList.length; i++){
          theList[i].length >= minLength 
          && theList[i].length <= maxLength 
          && !theList[i].replace(regExp,'') 
          && filtered.push(theList[i]);
        };
        return filtered
      };
      
      // make a random selection from a list of ok words
      var randomSelection = function(subList,numberOfWords,randomOrder,prefix,capAfterPrefix){
        subList.randomSort();
        subList = subList.slice(0,numberOfWords)
        !randomOrder && subList.sort()
        if(prefix){
          for(var i = 0; i<subList.length;i++){
            subList[i] = prefix + (
              capAfterPrefix
                ? subList[i].charAt(0).toUpperCase() + subList[i].substring(1) 
                : subList[i]
              );
          }
        };
        return subList
      };
      
      // react on user input
      var getWords = function(f){
        var rExp = new RegExp(f.startWith.value + '.*' + f.endWith.value,'gi');
        var maxLengthOfWords = f.maxLengthOfWords.value;
        maxLengthOfWords = isNaN(maxLengthOfWords) ? 100 : maxLengthOfWords/1;
        var minLengthOfWords = f.minLengthOfWords.value;
        minLengthOfWords = isNaN(minLengthOfWords) ? 1 : minLengthOfWords/1;
        var numberOfWords = f.numberOfWords.value;
        numberOfWords = isNaN(numberOfWords) ? 50 : numberOfWords/1;
        var prefix = f.prefix.value;
        var randomOrder = f.randomOrder.checked;
        var capitalizeAfterPrefix = f.capitalizeAfterPrefix.checked;
        var okWords = findWords(rExp,maxLengthOfWords,minLengthOfWords);
        f.yourWords.value = randomSelection(
          okWords,numberOfWords,randomOrder,prefix,capitalizeAfterPrefix
        ).join('\n');  
      };
      
      // init
      readListToArray();
      
      // return a public function
      // (timeout for debugging - this way we never send the form even if bug occurs)
      return function(f){
        setTimeout(function(){getWords(f)},1)
      };
    })();

  • Users who have thanked ironboy for this post:

    Mr. (09-29-2011)

  • #10
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Smile Wow!!

    Wow!! You are awesome!!! Can you give me your email just in case i have any future projects i can pay you to work on for me??

  • #11
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Thanks!
    I enjoy all things having to do with words and semantics in programming.

    I already make enough money (not by hanging around here - but I'm home with a cold so I'm enjoying myself with some basic programming), but I'll send you my email anyway - who knows what might happen.

  • #12
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Is there any way you can add just one more feature? a suffix option?

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Done.
    Code:
    getWords = (function(){
      
      var theList;
      
      // sort in random order
      Array.prototype.randomSort = function(){
        this.sort(function(){
          return (Math.round(Math.random())-0.5);
        })
      };
      
      // read a file using SJAX
      var getFile = function (url){
        var req = window.XMLHttpRequest && new XMLHttpRequest(); 
        !req && (req = new ActiveXObject("Microsoft.XMLHTTP"));
        req && req.open("GET", url, false) 
        req && req.send(null);
        !req && (req = {responseText:false});
        var r = req.responseText;
        return r
      };
      
      // read our wordlist to the array theList
      var readListToArray = function(){
        theList = getFile('wordlist.txt').split('\n');
      };
      
      // find words matching a regular expressions and a max length
      var findWords = function(regExp,maxLength,minLength){
        var filtered = [];
        for(var i = 0; i < theList.length; i++){
          theList[i].length >= minLength 
          && theList[i].length <= maxLength 
          && !theList[i].replace(regExp,'') 
          && filtered.push(theList[i]);
        };
        return filtered
      };
      
      // make a random selection from a list of ok words
      var randomSelection = function(subList,numberOfWords,randomOrder,prefix,suffix,capAfterPrefix){
        subList.randomSort();
        subList = subList.slice(0,numberOfWords)
        !randomOrder && subList.sort()
        prefix = prefix || '';
        suffix = suffix || '';
        for(var i = 0; i<subList.length;i++){
          subList[i] = prefix + (
            capAfterPrefix
              ? subList[i].charAt(0).toUpperCase() + subList[i].substring(1) 
              : subList[i]
            ) + suffix;
        };
        return subList
      };
      
      // react on user input
      var getWords = function(f){
        var rExp = new RegExp(f.startWith.value + '.*' + f.endWith.value,'gi');
        var maxLengthOfWords = f.maxLengthOfWords.value;
        maxLengthOfWords = isNaN(maxLengthOfWords) ? 100 : maxLengthOfWords/1;
        var minLengthOfWords = f.minLengthOfWords.value;
        minLengthOfWords = isNaN(minLengthOfWords) ? 1 : minLengthOfWords/1;
        var numberOfWords = f.numberOfWords.value;
        numberOfWords = isNaN(numberOfWords) ? 50 : numberOfWords/1;
        var prefix = f.prefix.value;
        var suffix = f.suffix.value;
        var randomOrder = f.randomOrder.checked;
        var capitalizeAfterPrefix = f.capitalizeAfterPrefix.checked;
        var okWords = findWords(rExp,maxLengthOfWords,minLengthOfWords);
        f.yourWords.value = randomSelection(
          okWords,numberOfWords,randomOrder,prefix,suffix,capitalizeAfterPrefix
        ).join('\n');  
      };
      
      // init
      readListToArray();
      
      // return a public function
      // (timeout for debugging - this way we never send the form even if bug occurs)
      return function(f){
        setTimeout(function(){getWords(f)},1)
      };
    })();

  • #14
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts
    haha wow thank you so much!! I'll throw you $40 if you add one more thing for me!

    For some reason it is skipping a lot of words when I generate them not being random. It's not generating them alphabetically one after the other.

    Can you make an option to display all the words with my specifications, and with a gap between every 500 words??

    So if I want "Bird" as a prefix to all words starting with "B" that is 4 characters long then I should receive every word in the list that meets that requirement and after every 500 words a gap.

    Thank you soooooo much!!!!!

  • #15
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Don't throw your money around

    We might have a problem - 'cause it shouldn't be skipping words (although I've noticed that it sorts capitalized words before any others...)

    Give me an example of a word it skips...

    Get back to you later - have to make som breakfast and take the kids to school now.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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