random words in sentence

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

But then the word list would have to be availible in every order, wouldn't it?

And acheiving that seems to be the big problem...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

No the recursion would handle searching through the ordering search space, and would prune the orderings without generating the full list as appropriate.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Here's my recursion'less approach:

Use array_keys with the optional search paramater, use a loop to do this once per keyword youre looking for (loop through an array of keywords)

So something like

Code: Select all

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
outputs

Code: Select all

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
You could then look at $array to see if the array values are sequential

If there was more then one call to array_keys youd obviously have to merge the return array with what you've already found so far.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Grim... wrote:But the words have to be next to each other in the sentence, right?


Shiznatix, I have to ask because I'm intrigued...
Why?
the words do have to be next to eachother this is true.

why? because i get a huge list of sentences and i have to put them through a function to make links on certain words and then other things.

what would be easiest here people would be how would i get a list of every different possibility of the string of words. that should be the easiest way because there would be a total of 2 or 3 maximum 4 different posibilities.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

don't do permutations.. it get's pretty nasty..

simply using the index list like jshpro2 posted in combination with some array combiners should yield the results far faster. The tricky part is properly breaking the sentences apart. ... and even then, it's not all that hard.

[edit]oops sorry jshpro2
Last edited by feyd on Tue Aug 09, 2005 4:14 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ok well hummmm, how do i go about doing this cause i have no idea at all. maybe jshpro2 had the right idea but i have no idea.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Not tested, but this should be close.

Code: Select all

function sequentialOrderlessMatch($subject,$words,$matchStarted=FALSE) {
  foreach($words as $aWord) {
    $pos = strpos($subject,$aWord);
    if ($pos!==FALSE && (!$matchStarted || $pos==0)) {
       $newSubject = substr($subject,strlen($aWord)+1);
       $newWords = array_diff($words,array($aWord))l
       $found = sequentialOrderlessMatch($newSubject,$newWords,TRUE);
    }
    if (!$found) continue;
  }
  return $found;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically.. here's my idea.. strtolower() both needle and haystack arrays using array_map(). Iterate over the needles each pass storing the results of a rewrite over the output from an array_flip() of array_keys(). The rewrite should replace all the new values with the current needle. Merge this final array into an aggregate array so all needles end up being in there.

Iterate over this set finding contiguous sections and checking the word found to see if it's unique among the list in the sequence thus far. If you find all of them, you're done.. it's a match.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

random words in sentence[solved]

Post by shiznatix »

incase you where wondering here is what I did, i first manually made a list of all the possible ways the words could appear then I did this code.

Code: Select all

$oldlines = implode(' ', $arrlines);

            for ($i=0; $i<count($keys); $i++)
            {
                if (false !== strpos($oldlines, $keys[$i]))
                {
                    $somt = explode(' ', $keys[$i]);
                    $con = count($somt);

                    $temp = preg_replace('/'.$keys[$i].'/', '¤', $oldlines, 1);
                    $temp = str_replace('  ', ' ', $temp);
                    $temp = str_replace('   ', ' ', $temp);

                    $newtemp = explode(' ', $temp);

                    for($b=0; $b<count($newtemp); $b++)
                    {
                        if ($newtemp[$b] == '¤')
                        {
                            $count = $b;
                            break;
                        }
                    }

                    $newlinesarr[$count] = '<a href="'.$filename.'">'.$keys[$i].'</a>';

                    for($c=$count; $c<$count+$con; $c++)
                        unset($arrlines[$c]);
                }
            }
after some other replacements I merged the $newlinesarr with the $arrlines and bam. i had to seperate the $newlinesarr from its original because i had to make sure there was no double linking

maybe not the best way but it was the only way i could figure out
Post Reply