Forum Moderators: coopster

Message Too Old, No Replies

Best way to code for random sentence

         

webdevfv

11:16 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Hi this might be a simple query for you guys.

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

Anyango

11:38 am on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



are there too many words ? are they added regularly ? i mean can we have them in an array ? or database is must ?

brotherhood of LAN

12:05 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Interesting question :)

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.

Habtom

12:22 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Provided $sent_1, $sent_2, $sent_3 . . . are arrays representing the seven parts of the sentence, and $records is the number of records in the database, you can have something like the following:

echo $sent_1[rand(0,$records)]." ".$sent_2[rand(0,$records)]." "......;

henry0

12:27 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does the final result come the very same broken sentence?
Or does it come from any row from each table?
if the first suggestion is true then you need not only an incremented id per table but to insert a sentence id (the same in each table) so the random order could be made out of "where ID= whatever id is the sentence"
Give us more details

andrewsmd

2:32 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds to me like an array of words would be much easier. If you have lists of the words then you could save them in text files and it would be much easier to work with. Here is an easy example. Say you have
section1.txt and in it you have
A
This
The
My
His
etc. (you don't need the etc it's just to show you could have more)
in section2.txt you have
superb
big
red
hot
etc.
in section3.txt you could have
cow
bat
door
room
etc.
Now in your PHP you could store these in arrays like so
$file1 = "C:\\your folder\\section1.txt" (the \\ is correct you have to escape the \ in php")
$file2 = "C:\\your folder\\section2.txt"
$file3 = "C:\\your folder\\section3.txt"

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

Anyango

2:57 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member





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