PDA

View Full Version : Can you point me to a script that does this?



BLiZZaRD
10-11-2005, 12:49 PM
Okay, I may be asking a lot here....

I have 5 sentences, of 5 words each. I am to make one sentence of 5 words using only one word from each original sentence.

I have searched the web for hours trying to find a program that would allow me to input the 25 words and rearrange them for me.

If possible I would love someone to point the way to such a program, or maybe even write one????

This is for a game, nothing major, but with (5!)3125 choices (375000 approximate options) you can see why I would rather NOT do this with pen and paper....

Thanks to anyone who can help!

Twey
10-11-2005, 04:14 PM
Something like:
<script type="text/javascript">
var sentences = new Array();
sentences[0] = "Hello my name is John"; // apologies for lack of punctuation :p
sentences[1] = "Won't it be hot tomorrow";
sentences[2] = "Who turned the light on";
sentences[3] = "I think it might be";
sentences[4] = "I'm running out of ideas";

var tot = "";

for(var i=0;i<sentences.length;i++) {
var words = new Array();
words = sentences[i].split(" ");
var word = words[Math.floor(Math.random() * words.length)];
if(i != 0) word = " " + (word == "I" ? word : word.toLowerCase());
else {
var firstChar = word.charAt(0).toUpperCase();
word = firstChar + word.substr(1);
}
tot += word;
}
tot += ".";
// The final sentence is stored at "tot."
</script>
? I hope you're not expecting actual grammar or punctuation or anything :p

jscheuer1
10-11-2005, 04:25 PM
I worked it out this way, put this in the body of your page:


<script type="text/javascript">
//Randomizing Unit Courtesy of Mike Winter as seen at:
//http://www.dynamicdrive.com/forums/showthread.php?p=8442
function random(n) {
return Math.floor((Math.random() % 1) * n);
}

Array.prototype.shuffle = function() {var i = this.length;
while(i--) {this.swap(i, random(i + 1));}
};
Array.prototype.swap = function(x, y) {
var t = this[x]; this[x] = this[y]; this[y] = t;
};
//End Randomizing Unit
var endSen=''
sens=new Array();
//set sentences below
sens[0]="five words in a row"
sens[1]="fish do not use cars"
sens[2]="birds are on the fly"
sens[3]="animals have many friends too"
sens[4]="who asked you the question"
for (var i_tem = 0; i_tem < sens.length; i_tem++){
sens[i_tem]=sens[i_tem].split(' ')
endSen+=sens[i_tem][random(5)]+' '
}
endSen=endSen.split(' ')
endSen.shuffle();
endSen=endSen.join(' ')
if(endSen.charAt(0)==' ')
endSen=endSen.substr(1)
endSen=endSen.charAt(0).toUpperCase()+endSen.substr(1)
document.write(endSen+'.')

</script>

BLiZZaRD
10-12-2005, 05:13 PM
You guys are AWESOME!!!!!!!

Thanks a lot! And no punctuation and grammar are not important. Working on them now!

This place is so awesome!

Thanks again! :D

Twey
10-12-2005, 05:57 PM
And no punctuation and grammar are not important. Working on them now!
That might be quite difficult. Punctuation support is conceivable, but to get actual grammar you'd have to have a list of every single possible word and its grammatical type.

BLiZZaRD
10-12-2005, 06:07 PM
Well apparently the "answer" is just 5 of those 25 words, in a random (and semi-sensible) order, so I am guessing we do not change the words at all. so "called" would stay "called" and not be changed to "calls" or "calling"

So as far as I am concerned this script works perfect!

Thanks again!