Forum Moderators: coopster
I want to make up random sentences - I have split the sentence into seven parts and I want each sentence to take a word(s) out of each section to make a complete sentence.
What's the best way to do it?
Initially I've set up 7 tables each with an ID and WORD fields - is this the right way to go?
Simple example
--------------
Section1 - Section2 - Section3 - etc
A - stunning - gift
This - superb - present
A few points I can think of:
- 'Complexity' is a bit subjective, what is your goal. To be human readable or just to be unique enough for search engine robots?
- Are your words already in some sort of 'order'? i.e. determiners, nouns, adjectives, verbs
If the answer to the latter is true, you could just make a matrix of all combinations in 1 query.
There isn't a "best" way IMO, just various methods and outcomes, all of which don't substitute 'real' content.
//this stores the contents of each file into an array
//separated by the returns
$array1 = file($file1);
$array2 = file($file2);
$array3 = file($file3);
//and array to store the sentences
$sentenceArray = array();
//set $i < to as many times as you want right now
//we are going to run it 5 times
for($i = 0; $i < 5; $i++){
//these random numbers would need to correspond to the
//number of items in your file. i.e. if you have 5 words
//in file1 then rand1 needs to be rand(0,4) because arrays
//start with 0 and there are 5 items
$rand1 = rand(0, 4);
$rand2 = rand(0, 3);
$rand3 = rand(0, 3);
//this is setting the sentences to an array
array_push($sentenceArray, "{$array1[$rand1]} {$array2[$rand2]} {$array3[$rand3]}");
//this is setting the sentences to a string that
//has carriage returns you could write to a new file
$sentenceFile .= "{$array1[$rand1]} {$array2[$rand2]} {$array3[$rand3]}\n";
//this is setting the sentences to an html string you
//could write to a webpage
$sentenceHtml .= "{$array1[$rand1]} {$array2[$rand2]} {$array3[$rand3]}<br>";
}//for
I didn't debug it but it's close. The text files would be nicer because you don't have to connect to a database every time and inserting more words would be a matter of copy and paste.
//these random numbers would need to correspond to the
//number of items in your file. i.e. if you have 5 words
//in file1 then rand1 needs to be rand(0,4) because arrays
//start with 0 and there are 5 items
$rand1 = rand(0, 4);
$rand2 = rand(0, 3);
$rand3 = rand(0, 3);
can be
$rand1 = rand(0, count($array1)-1);
$rand2 = rand(0, count($array2)-1);
$rand3 = rand(0, count($array3)-1);
so that you don't have to worry about re-entering those numbers each time you edit your strings files.
[edited by: Anyango at 2:58 am (utc) on Nov. 21, 2008]