2
votes

I am creating a word search but I want to rank them base on the highest existence of search keyword. How can I solve this problem?

I am trying to make a search if array 1 key exists inside array 2 long string and then order the array by total occurrence of array 1 in array 2.

Blow is my code

$str = "Hello World January Jude";
$arr1 = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$str = explode(" ", $str);
echo sort_base($arr1, $str);
function sort_base($arr, $str){
$count = "";
foreach ($arr as $valuer){
    foreach ($str as $value){
    //$list[] = strpos($valuer, $value, 0);
    $count .= strpos($valuer, $value, 0)."<hr/>"; 
}

}
$arr = trim($count," ");
 echo $arr;
}

Example input:

$array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; 
$str = "Hello World Cup"; 

Desire output:

Array in order:

  1. Hello World Cup
  2. Final Hello World
  3. Hello Cup
  4. Hello
  5. World
  6. Say Hello
2
Hello, can you explain in more detail how the output of that function is different from the output you're trying to get? Examples of expected and actual output would be helpful.Don't Panic
ok i will drop here For Example am searching for "Hello World Cup" which is $str as variable and i have arrays with example $array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; so i want to sort my array $array base on the one with the most existing search keywords from the string i split earlier which is "Hello World Cup" using $str = explode(" ",$array).High Breed Technology
so for example $array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; $str = "Hello World Cup"; so if i call function like this $arrays = sort_most_exists_asc($str, $array); it should Reorder my array so my array should be sorted to Hello World Cup Final Hello World Hello Cup Hello World Say HelloHigh Breed Technology
so for example $array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; $str = "Hello World Cup"; so if i call function like this $arrays = sort_most_exists_asc($str, $array); it should Reorder my array so my array should be sorted to 1. Hello World Cup 2. Final Hello World 3. Hello Cup 4. Hello 5. World 6. Say Hello Re Arranged base on total indexed array and return back as array for use.High Breed Technology

2 Answers

1
votes

You can use array-intersect and count to achieve number value of the similar words. Now you can use usort for sort by that.

Consider the following:

function sort_most_exists_asc($arr, $str) {
    usort($arr, function ($a, $b) use ($str) {
        $aa = count(array_intersect(explode(" ", $str), explode(" ", $a)));
        $bb = count(array_intersect(explode(" ", $str), explode(" ", $b)));
        return $bb - $aa;
    });
    return $arr;
}

$str = "Hello World January Jude";
$arr = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$arr = sort_most_exists_asc($arr, $str);

Live example: 3v4l

Notice this will work only for whole words. For words similarity use Levenshtein distance - and compare by that in the usort

0
votes

You can create a set of all the matching words between each phrase and the target list of words.

foreach ($arr1 as $phrase) {
    $matches[] = array_intersect(str_word_count($phrase, 1), $str);
}

That array of matches can be used with array_multisort to reorder the original array.

array_multisort($matches, SORT_DESC, $arr1);