【问题标题】:Parsing a string into parts, only consecutive words, not a power set将字符串解析为部分,只有连续的单词,而不是幂集
【发布时间】:2013-06-17 18:46:53
【问题描述】:

我正在尝试编写一个搜索查询来从数据库中查找文章。我想获取用户输入的搜索字符串并查找一组特定的可能搜索词。如果用户输入搜索字符串“2011 年德国平均工资列表”,我想生成一个要搜索的术语列表。我想我会寻找整个字符串和连续单词的部分字符串。那就是我想搜索“平均工资列表”和“2011 年德国”,而不是“2011 年德国列表”。

到目前为止,我有这段代码可以生成我的搜索词:

  $searchString = "listing of average salaries in germany for 2011";
  $searchTokens = explode(" ", $searchString);
  $searchTerms = array($searchString);

  $tokenCount = count($searchTokens);
  for($max=$tokenCount - 1; $max>0; $max--) {
      $termA = "";
      $termB = "";
      for ($i=0; $i < $max; $i++) {
          $termA .= $searchTokens[$i] . " ";
          $termB .= $searchTokens[($tokenCount-$max) + $i] . " ";
      }
      array_push($searchTerms, $termA);
      array_push($searchTerms, $termB);
  }

  print_r($searchTerms);

它给了我这个术语列表:

  • 2011年德国平均工资列表
  • 德国的平均工资列表
  • 2011年德国平均工资
  • 德国平均工资列表
  • 2011年德国平均工资
  • 平均工资列表
  • 2011年德国工资
  • 平均工资列表
  • 2011 年在德国
  • 平均列表
  • 2011 年德国
  • 上市
  • 2011 年
  • 上市
  • 2011

我不知道如何得到是缺少的条款:

  • 在德国的平均工资
  • 德国的平均工资
  • 德国平均工资
  • 平均工资在
  • 德国的平均工资
  • 在德国的工资
  • 等等...

更新

我不是在寻找“电源组”,所以像 this 或 this 这样的答案无效。例如,我不希望这些出现在我的术语列表中:

  • 平均德国
  • 列出2011年的工资
  • 德国的

我只查找连续的单词。

【问题讨论】:

标签: php string-parsing


【解决方案1】:

您想找到分解后字符串的所有顺序子集,只需从offset=0 开始,然后将数组从length=1 拆分到count-offset:

$search_string = 'listing of average salaries in germany for 2011';
$search_array = explode(' ',$search_string);
$count = count($search_array);

$s = array();
$min_length = 1;

for ($offset=0;$offset<$count;$offset++) {
    for ($length=$min_length;$length<=$count-$offset;$length++) {
        $match = array_slice($search_array,$offset,$length);
        $search_matches []= join(' ',$match);
    }
}

print_r($search_array);
print_r($search_matches);

【讨论】:

    【解决方案2】:

    首先,我只想让您知道,如果您要针对 SQL 数据库运行所有这些以进行搜索,效率极低,建议您使用 LIKE 选项。 http://www.techonthenet.com/sql/like.php

    现在,要获得所有可能的组合,只需将单词分解成一个数组(就像您对 explode 所做的那样),然后按照@ulvund 给出的关于这个问题的建议:PHP: How to get all possible combinations of 1D array?

    也就是说

    <?php
    
    $array = explode(" ", "listing of average salaries in germany for 2011");
    
    function depth_picker($arr, $temp_string, &$collect) {
        if ($temp_string != "") 
            $collect []= $temp_string;
    
        for ($i=0; $i<sizeof($arr);$i++) {
            $arrcopy = $arr;
            $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
            if (sizeof($arrcopy) > 0) {
                depth_picker($arrcopy, $temp_string ." " . $elem[0], $collect);
            } else {
                $collect []= $temp_string. " " . $elem[0];
            }   
        }   
    }
    
    $collect = array();
    depth_picker($array, "", $collect);
    print_r($collect);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 2017-10-18
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多