gpt4 book ai didi

计算匹配查询的单词数的 MySQL 列格栅?

转载 作者:行者123 更新时间:2023-11-29 00:42:50 25 4
gpt4 key购买 nike

我正在尝试根据查询中在行中找到的单词数来对匹配项进行评级。我盯着看这样做:

SELECT Text, MATCH(`Text`) AGAINST ('$s') AS Grade

但很快我意识到这行不通,因为 Grade 是基于很多东西的,例如单词的顺序、每个单词的长度等等。

我只想知道出现在一行中的单词的百分比。

如:

$s = 'i want pizza'
`Text` = 'pizza I want' // In this case Grade should be 100 as all words are found

其他例子:

Text             | Grade
pizza I want too | 100 // All words were found, It doesn't matter if there are extra words
pizza I want | 100
i want | 66 // Only 66% of the words are present
want want want | 33 // Only 33% of the words are present

最佳答案

$s = 'i want pizza';
$text = 'pizza I want';

//move to lower-case to ignore case-differences
$s = strtolower($s);
$text = strtolower($text);

//count the number of words in $s
$slen = count(explode(" ", $s));
//create an array of words from the text that we check
$arr = explode(" ", $text);
$count = 0;
//go over the words from $text and count words that appear on $s
foreach ($arr as $word) {
if(strpos($s, $word) !== false){
$count++;
}
}
//display the percentage in format XX.XX
echo number_format((double)(100 * $count/$slen),2);

关于计算匹配查询的单词数的 MySQL 列格栅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517125/

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