繁体   English   中英

PHP在SQL数据库中找到最匹配的句子

[英]php find sentence with most match in sql database

我正在制作一个问答应用程序,作为该应用程序的一部分,它需要查看您先前存储在SQL数据库中的问题,并找到具有最匹配词的先前问题,然后它将从数据库中获取一个属性。对于那条线。

我已经能够得到它为数据库中的每一行产生一个匹配单词的数组。 但是我需要一种组织这些数组以选择匹配度最高的数组的方法。 这是我到目前为止使用过的SQL和PHP。

$questions1 = $_GET['question'];

$questionsarray =  explode(" ",$questions1);

新问题变成一个数组,下面将与其他所有问题进行比对

$sql = "SELECT * FROM records WHERE userid= '24.9.71.79'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

 $questionsasked = $row['old_questions']; 

 // turns old question into an array
 $last_q_array =  explode(" ",$questionsasked);

 //finds matches between the old and new question    
 $intersectarray = array_intersect($last_q_array,$questionsarray);

然后,它使用array_diff()清除常用词,以帮助其专注于找到真正的主题

 $cleanedarray = array_diff($intersectarray ,$commonwords);

 //prints the array if it find matches and sets the var
 if(count($cleanedarray)>0) {

    print_r($cleanedarray);

    $desiredattri = $row[last_answer_type];

    echo "<br />----------------<br />";
 }

}
}

我正在使用print_r只是为了测试。 因此,生成少量仅显示匹配单词的数组非常有用。 看起来像这样

Array ( [3] => card ) 
----------------
Array ( [3] => card [7] => work?  ) 
----------------
Array ( [0] => find [2] => card [7] => work? ) 

因此,现在我需要找到一种解析这些数组并找到匹配次数最多的数组的方法。 我可以使用count()来计算每个数组中的匹配项,但仍然需要将该数字与其余数组计数进行比较,然后使用匹配项最多的数组的属性。

您可以尝试这样的事情。 它将单词本身用作数组键,并且数组值就是计数。

$result = array();
foreach ($cleanedarray as $word) {
    if (!array_key_exists($word, $result)) {
        $result[$word] = 0;
    }

    $result[$word]++; // keep count using the word as key
}

我确定可能还有其他内置的PHP函数可以为您完成此操作,但这是我很快就想到的一种n脏方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM