doufu6130 2018-08-15 11:40
浏览 32

PHP通过省略单词将搜索字符串更改为较低的相关性

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--;
            }

?>`

  • 写回答

1条回答 默认 最新

  • dongqindu8110 2018-08-15 13:15
    关注

    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)));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答