random words in sentence
Moderator: General Moderators
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
outputs
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.
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"));Code: Select all
Array
(
[0] => 0
[1] => 3
[2] => 4
)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.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
the words do have to be next to eachother this is true.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?
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
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.
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;
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
random words in sentence[solved]
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.
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
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]);
}
}maybe not the best way but it was the only way i could figure out