简体   繁体   English

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

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

I am making a question answering app and as a part of it, it needs to look at your previous questions which are stored in a SQL database and find the previous questions that has the most matching words and then it will take an attribute from the DB for that line. 我正在制作一个问答应用程序,作为该应用程序的一部分,它需要查看您先前存储在SQL数据库中的问题,并找到具有最匹配词的先前问题,然后它将从数据库中获取一个属性。对于那条线。

I have been able to get it to produce an array of matching words for each row in the database. 我已经能够得到它为数据库中的每一行产生一个匹配单词的数组。 But I need a way of organizing those arrays to select the one with the most matches. 但是我需要一种组织这些数组以选择匹配度最高的数组的方法。 here is the SQL and the PHP I have used to far. 这是我到目前为止使用过的SQL和PHP。

$questions1 = $_GET['question'];

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

The new question was turned into an array, below it will be compared against all other questions for match's 新问题变成一个数组,下面将与其他所有问题进行比对

$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);

It then uses array_diff() to clean out common words to help it focus on finding the true topic 然后,它使用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 />";
 }

}
}

I'm using print_r just for testing. 我正在使用print_r只是为了测试。 So it does a great job of producing a handful of arrays that show just the matching words. 因此,生成少量仅显示匹配单词的数组非常有用。 that looks something like this 看起来像这样

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

So now I need to find a way to parse those arrays and find the one that has the most matches. 因此,现在我需要找到一种解析这些数组并找到匹配次数最多的数组的方法。 I can use count() to count the matches in each array but still need to compare that number against the rest of the array counts and then use the attribute of the array with most matches. 我可以使用count()来计算每个数组中的匹配项,但仍然需要将该数字与其余数组计数进行比较,然后使用匹配项最多的数组的属性。

You could try something like this. 您可以尝试这样的事情。 It will use the words themselves as array keys and the array value is the 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
}

I'm sure there might be other built-in PHP functions which could do this for you, but this was a quick-n-dirty way that came to me off hand. 我确定可能还有其他内置的PHP函数可以为您完成此操作,但这是我很快就想到的一种n脏方法。

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

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