可能使用编辑距离来匹配搜索词准确性
我有一个 mySQL 表,人们在其中添加他们的姓名和兴趣。我想使用某种单词匹配来遍历并找到 100% 匹配或接近匹配。我听说过编辑距离,但不知道如何让它在我的桌子上循环。
$input = $_POST["interest"];
$result = mysql_query("SELECT interest_desc FROM interests");
做了一些谷歌搜索并到达了这一点
function closest($seed, $haystack){
$shortest = -1;
foreach ($haystack as $word){
$lev = levenshtein($seed, $word);
if ($lev == 0) {
$closest = $word; $shortest = 0; break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word; $shortest = $lev;
}
}
return $closest;
}
$array = mysql_fetch_row($result);
$closestmatch = closest($input,$array);
echo $closetmatch;
i have a mySQL table where people add their names and their interests. I want to use some sort of word match that goes through and finds either a 100% match or a close match. Ive heard of the levenshtein distance but have no clue how to make it cycle through my table.
$input = $_POST["interest"];
$result = mysql_query("SELECT interest_desc FROM interests");
Done some googling and got to this point
function closest($seed, $haystack){
$shortest = -1;
foreach ($haystack as $word){
$lev = levenshtein($seed, $word);
if ($lev == 0) {
$closest = $word; $shortest = 0; break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word; $shortest = $lev;
}
}
return $closest;
}
$array = mysql_fetch_row($result);
$closestmatch = closest($input,$array);
echo $closetmatch;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 SOUNDEX 是您的一个选择。
Lo Sauer 的文章 可以帮助您
http://www.lsauer.com/2013/05/mysql -模糊搜索全文查询.html
I think SOUNDEX is an alternative for you.
The Lo Sauer's article can help you with that
http://www.lsauer.com/2013/05/mysql-fuzzy-searching-fulltext-queries.html
我认为使用 PHP 来做到这一点是不正确的方法,MySQL 可以轻松有效地做到这一点。我不确定你的整个架构结构是什么样的,但你可以在 MySQL 中使用搜索参数创建一个
PROCEDURE
,然后从 PHP 调用它。在 MySQL 中做类似的事情:
-- 创建带有搜索参数的过程
创建过程 sp_SearchInterests(IN p_SearchParam VARCHAR(30));
分隔符 //
从兴趣中选择interest_desc
其中interest_desc = p_SearchParam
或者interest_desc LIKE '%pSearchParam%'//
结束;
DELIMITER ;
在 PHP 中,只需
CALL sp_SearchInterests('whateveryouwant')
即可返回所需的结果。I think using PHP to do this is the incorrect approach, MySQL can easily and efficiently do this. I'm not sure what your whole schema structure is like, but you could just make a
PROCEDURE
in MySQL with the search parameters and just call it from PHP.Make something similar to this in MySQL:
-- Create proc with search parameter
CREATE PROCEDURE sp_SearchInterests(IN p_SearchParam VARCHAR(30));
DELIMITER //
SELECT interest_desc FROM interests
WHERE interest_desc = p_SearchParam
OR interest_desc LIKE '%pSearchParam%'//
END;
DELIMITER ;
From PHP just
CALL sp_SearchInterests('whateveryouwant')
to return the desired results.