Page 2 of 2

Posted: Tue Aug 09, 2005 10:16 am
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...

Posted: Tue Aug 09, 2005 10:41 am
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.

Posted: Tue Aug 09, 2005 11:16 am
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.

Posted: Tue Aug 09, 2005 3:23 pm
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.

Posted: Tue Aug 09, 2005 3:36 pm
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

Posted: Tue Aug 09, 2005 4:11 pm
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.

Posted: Tue Aug 09, 2005 4:20 pm
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;
}

Posted: Tue Aug 09, 2005 4:25 pm
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.

random words in sentence[solved]

Posted: Wed Aug 10, 2005 6:03 am
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