我需要将搜索字词列表转换成最有效的组合搜索字词集.任何单词或引用的短语都可以由OR分隔.许多术语可以在括号内组合.也可以使用ANDs.
例如,foo bar和boo吧共享栏,所以代替两个不同的搜索项,可以组合为(foo OR boo)AND bar.
这是算法需要做的.给定这个数据集:
foo bar boo bar goo bar hoo doo foo manchu moo bar too bar foo fighters "blue kazoo" bar baz qux quux
我想得到以下回覆:
(foo OR boo OR goo OR moo OR too OR "blue kazoo") AND bar foo AND (manchu OR fighters) hoo doo baz OR qux OR quux
这不行:
(foo bar) OR (boo bar) OR (goo bar) OR (foo manchu)
我得到以下代码:
原文链接:https://www.f2er.com/php/131785.htmlfunction keyMultiSort(&$array,$key,$reverse = false,$priority_last = false,$save_key = true,Callable $func = null) { if ($func === null) { $func = function ($first,$second) use ($key,$reverse,$priority_last) { if (!isset($first[$key])) { return ($reverse === false) ? -1 : 1; } if (!isset($second[$key])) { return ($reverse === false) ? 1 : -1; } if ($first[$key] > $second[$key]) { return ($reverse === false) ? 1 : -1; } if ($first[$key] < $second[$key]) { return ($reverse === false) ? -1 : 1; } if ($first[$key] === $second[$key]) { return ($priority_last === false) ? 1 : -1; } return 0; }; } if ($save_key) { uasort($array,$func); } else { usort($array,$func); } } $array = [ ['foo','bar'],['boo',['goo',['hoo','doo'],['foo','manchu'],['moo',['too','fighters'],['blue kazoo',]; $pairs = []; $str = ''; foreach($array as $item) { if(!isset($pairs[$item[0]]['count'])) { $pairs[$item[0]]['count'] = 1; } else { $pairs[$item[0]]['count']++; } $pairs[$item[0]]['elements'][] = $item[1]; if(!isset($pairs[$item[1]]['count'])) { $pairs[$item[1]]['count'] = 1; } else { $pairs[$item[1]]['count']++; } $pairs[$item[1]]['elements'][] = $item[0]; keyMultiSort($pairs,'count',true); } $remove = []; foreach($pairs as $elm=>$item) { $remove[] = $elm; $elements = array_diff($item['elements'],$remove); if(empty($elements)) { if (in_array($elm,$remove)) { continue; } $str .= $elm.PHP_EOL; } else { $str .= $elm.' AND ('.implode(' OR ',$elements).')'.PHP_EOL; } $remove = array_merge($remove,$elements); } var_dump($str);
结果:
string(99) "bar AND (foo OR boo OR goo OR moo OR too OR blue kazoo) foo AND (manchu OR fighters) hoo AND (doo) "
它可以优化,取决于目标…