gpt4 book ai didi

php - 相似文本 php mysql

转载 作者:行者123 更新时间:2023-11-30 23:29:50 24 4
gpt4 key购买 nike

我有以下代码,当我搜索一个词时,它会显示“你是不是要找我”。问题是,如果我输入“clin”,我希望它返回临床,但它返回“reschedule”

$my_word = $_POST['value'];
$bestMatch = array('word' = > $my_word, 'match' = > 2);
$result = mysql_query("SELECT keyword FROM athena");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
similar_text($row['keyword'], $my_word, $percent);
if ($percent > $bestMatch['match'])
$bestMatch = array('word' = > $row['keyword'], 'match' = > $percent);
}
if ($bestMatch['match'] < 70)
echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong>';

最佳答案

我刚刚用一小组测试条目尝试了您的代码,它运行良好。也许您从查询中得到奇怪的结果?这是代码:

$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = array("exam", "clinicals", "templates", "reschedule", "crafting", "php", "answer");
$storeArray = Array();
foreach ($result as $keyword) {
similar_text($keyword, $my_word, $percent);
if ($percent > $bestMatch['match'])
$bestMatch = array('word' => $keyword, 'match' => $percent);
}
if ($bestMatch['match'] < 70)
echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> p:'.$bestMatch['match'];

在任何情况下,similar_text() 可能都不是正确的函数,因为它通常会在短字符串/单词 上产生误导性的结果。

正如已经指出的那样,您应该使用 levenshtein() 来完成这样的任务。它计算必须进行多少更改才能匹配单词,其中删除、添加和更改字符是一次更改。您可以(在这种情况下应该)修改更改为 2 的成本,以便使用短字符串获得更好的结果。

levenshtein 函数在性能方面比 similar_text 成本更低,但它不会以百分比形式返回结果!

levenshtein 方法的代码:

$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = array("exam", "clinicals", "templates", "reschedule", "crafting", "php", "answer");
$storeArray = Array();
foreach ($result as $keyword) {
$lev = levenshtein ($keyword, $my_word, 1, 2, 1);
if (!isset($lowest) || $lev < $lowest) {
$bestMatch = array('word' => $keyword, 'match' => $lev);
$lowest = $lev;
}
}
if ($bestMatch['match'] > 0)
echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> l:'.$bestMatch['match'];

关于php - 相似文本 php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11321376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com