How to use Boyer Moore Algorithm to search for items using Laravel

194 views Asked by At

I have tried to make boyer moore algorithm with laravel based on google reference. But I'm stuck when comparing the text with the pattern. The text that I use, retrieves data from the database using CONCAT, and the pattern is taken from user input.

The first code is to create a badCharHeuristic function

function badCharHeuristic($str, $size, &$badchar)
{
    for ($i=0; $i < 256; $i++)
       $badchar[$i] = -1;
        
    for ($i=0; $i < $size; $i++)  
       $badchar[ord($str[$i])] = $i;
}

The second code is to create a function for search string

function SearchString($str, $pat)
{
    $m = strlen($pat);
    $n = strlen($str);
    $i = 0;

    $this->badCharHeuristic($pat, $m, $badchar);

    $s = 0;
    while ($s <= ($n - $m))
    {
        $j = $m - 1;

        while ($j >= 0 && $pat[$j] == $str[$s + $j])
            $j--;

        if ($j < 0)
        {
            $arr[$i++] = $s;
            $s += ($s + $m < $n) ? $m - $badchar[ord($str[$s + $m])] : 1;
        }
        else
            $s += max(1, $j - $badchar[ord($str[$s + $j])]);
    }

    for ($j = 0; $j < $i; $j++)
    {
        $result[$j] = $arr[$j];
    }

    return $result;
}

Then to test it, I created a new function and called the SearchString function

public function searchItem(Request $request)
{
    $data = DB::table('tbl_orders_detail')
       ->select(DB::raw("CONCAT(item_name,' Kondisi ',item_condition) as item"))->get();

    $value = $this->SearchString($data, $request->item);
        dd($value);
}

And I get error ord() expects parameter 1 to be string, object given. $data holds values of type array.

how so that pattern can compare the text contained in the array? please help and explain. Thank you

0

There are 0 answers