【发布时间】: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年的工资
- 德国的
我只查找连续的单词。
【问题讨论】:
-
您正在寻找的称为电源组。已经在 SO 上询问并解决了几次。 :) Here's a question which was resolved with a working function 和 here's another。
-
这种方法似乎相当低效且不必要地复杂。您可能应该在看类似 Lucene 或 Sphinx 的东西。
标签: php string-parsing