this is the first time for me asking a question here so please don't crucify me if I didn't do everything 100% correct.
I am trying to create a full text search with php and mysql that returns results that decrease by relevance. The user will type in words to look for in an article, so I don't know how many words he will use. Let' say the user is looking for differnt occupations in an article like
$searchstring = 'painter bricklayer baker pope'
Now first I want to search for texts where ALL these words appear. In the next step I want to widen the search by leaving one word away.
Second search string should then be only
'painter bricklayer baker'
third:
painter bricklayer pope
forth:
painter baker pope
fifth:
bricklayer painter pope
And then the same should happen for all these new variants again, like for the second result:
painter bricklayer
painter baker
bricklayer baker
and then for this variants again and again until there is only one word left.
Here is what I've got so far but it leads to some dead end because I am not able to store all sub-results in separate arrays and do the same to them again and again. But I guess my approach ist totally wrong anyway and there might be a propper solution for this already that I am just not able to find on my own. So, can anyone push me in the right direction please?
`
';
$stringarray = explode( ' ', $searchstring );
$collectionArray = array();
$newString = '';
$run = count($stringarray);
while ($run > 1) {
$length = count($stringarray);
$counter = $length-1;
echo '<br><br>';
echo '$counter: ' . $counter . '<br>';
while ($counter > 0) {
for ($i = $length-1; $i >=0; $i--)
{
echo 'Counter: ' . $counter . ' | Index ' . $i .': ';
if ($i == ($counter)) {
echo '-----<br>';
continue;
}
echo $stringarray[$i] . '<br>';
$newString = $newString . ' ' . $stringarray[$i];
//echo $counter . ' danach';
}
//print_r($collectionArray);
$collectionArray[] = $newString;
$counter--;
$newString = '';
echo '<br><br>';
}
array_pop($stringarray);
print_r($stringarray);
echo '<br>';
print_r($collectionArray);
$run--;
}
?>`
One solution is to build this as an recursive function (a function that calls itself). You start with each word, call the function itself to add each other word. If you provide the result array as an reference you can avoid going down paths already done:
$words = explode(' ', 'painter bricklayer baker pope');
function compileSearchStrings(array &$searchStrings, array $available, array $used = []) {
// foreach way to go
foreach($available as $word) {
$words = $used;
$words[] = $word;
sort($words);
$searchString = implode(' ', $words);
// did we walk down that path already?
if (!in_array($searchString, $searchStrings, TRUE)) {
// store path
$searchStrings[] = $searchString;
// still a way to go?
if (count($available) > 0) {
compileSearchStrings($searchStrings, array_diff($available, array($word)), $words);
}
}
}
}
$seachStrings = [];
compileSearchStrings($seachStrings, $words);
var_dump($seachStrings);
If is possible to implement it without the reference, but that will generate duplicates that you need to remove:
function compileSearchStringsNoRef(array $available, array $used = []) {
$result = [];
foreach($available as $word) {
$words = $used;
$words[] = $word;
sort($words);
$result[] = implode(' ', $words);
if (count($available) > 0) {
array_push(
$result,
...compileSearchStringsNoRef(array_diff($available, array($word)), $words)
);
}
}
// remove duplicates and return
return array_unique($result);
}
var_dump(compileSearchStringsNoRef($words));
To get more specific queries first, you can sort the array by length:
function sortByLength($array) {
usort(
$array,
function($a, $b) {
$aLength = strlen($a);
$bLength = strlen($b);
if ($aLength === $bLength) {
return strnatcasecmp($a, $b);
}
return $bLength - $aLength;
}
);
return $array;
}
var_dump(sortByLength(compileSearchStringsNoRef($words)));