...

View Full Version : Help with generating random words..



Mr.
09-29-2011, 03:05 AM
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:



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

ironboy
09-29-2011, 03:11 AM
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.

ironboy
09-29-2011, 03:13 AM
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!

ironboy
09-29-2011, 03:14 AM
Also what format do you have the word list in now.
Plain text file?
What are the words separated with? Line breaks? Something else?

ironboy
09-29-2011, 03:19 AM
Could you put the list somewhere (on a server) so I can download and have a look?

Mr.
09-29-2011, 03:20 AM
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.cgi/~checkout~/src/share/dict/web2?rev=1.12.22.1;content-type=text%2Fplain

ironboy
09-29-2011, 04:39 AM
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


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)
};
})();

ironboy
09-29-2011, 04:44 AM
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.

ironboy
09-29-2011, 04:54 AM
Ok, done with the prefix (changed the zip-file etc above):

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)
};
})();

Mr.
09-29-2011, 05:21 AM
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??

ironboy
09-29-2011, 05:26 AM
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.

Mr.
09-29-2011, 05:33 AM
Is there any way you can add just one more feature? a suffix option?

ironboy
09-29-2011, 05:41 AM
:thumbsup:Done.

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)
};
})();

Mr.
09-29-2011, 05:59 AM
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!!!!!

ironboy
09-29-2011, 06:09 AM
Don't throw your money around :D

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.

Mr.
09-29-2011, 06:19 AM
Here's an example. If I generate 500 words with these settings..

minLength
3
maxLength
6
WordsStartWith
b
Prefix words with
Brand

Sometimes it includes "BrandBucket" and other times it does not. I would think it would start from the top of the word list alphabetically and stop at word 500.

ironboy
09-29-2011, 08:51 AM
:thumbsup:Ok, fixed the word order (sadly not the world order) and added the possibility of a blank line after a certain number of words. I'm done. ;)

(Better empty your browser cache if you're revisiting the url:
http://www.clientsmile.se/words)


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,blAfter){
randomOrder && subList.randomSort();
subList = subList.slice(0,numberOfWords);
prefix = prefix || '';
suffix = suffix || '';
for(var i = 0; i<subList.length;i++){
subList[i] = prefix + (
capAfterPrefix && prefix
? subList[i].charAt(0).toUpperCase() + subList[i].substring(1)
: subList[i]
) + suffix + (i && i%blAfter == 0 ? '\n' : '');
};
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 = !maxLengthOfWords || isNaN(maxLengthOfWords) ? 100 : maxLengthOfWords/1;
var minLengthOfWords = f.minLengthOfWords.value;
minLengthOfWords = !minLengthOfWords || isNaN(minLengthOfWords)? 1 : minLengthOfWords/1;
var numberOfWords = f.numberOfWords.value;
numberOfWords = !numberOfWords || isNaN(numberOfWords) ? 1000000 : numberOfWords/1;
var blAfter = f.blankLineAfterNumberOfWords.value;
blAfter = !blAfter || isNaN(blAfter) ? 1000000 : blAfter/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,blAfter
).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)
};
})();

jassi.singh
09-29-2011, 10:14 AM
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));

return text;
}

Mr.
09-29-2011, 03:17 PM
:thumbsup:Ok, fixed the word order (sadly not the world order) and added the possibility of a blank line after a certain number of words. I'm done. ;)

(Better empty your browser cache if you're revisiting the url:
http://www.clientsmile.se/words)




You're my hero man!! Thank you so so much! So is $40 good for the work you gave me?

ironboy
09-29-2011, 03:19 PM
It's free of charge. :)

Mr.
09-30-2011, 05:03 AM
Thank you so much! Best experience I've had on this site yet!

P.S. Can you guide me in the right direction on how you learned such complex JavaScript?

ironboy
09-30-2011, 03:24 PM
In one word: Experience (from programming in general as well as JavaScript) - get your hands dirty. And search the web - read the blogs of good developers... Discuss things. Focus on solving real life problems.

I have a bachelor's degree in Computer Science - but mostly learned Smalltalk and Pascal back in those days... In 2003 I became a web developer at a bigger company and did a lot of production stuff and e-learning in JavaScript and ActionScript...

Nowadays I lead a team of developers - most of them are younger and brighter than I am. But if I can find the time I still enjoy programming as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum