MySQL列格栅计数匹配查询的单词数量?
我在尝试根据查询中在该行中找到的单词数来对匹配进行评分。我盯着这样做:MySQL列格栅计数匹配查询的单词数量?
SELECT Text, MATCH(`Text`) AGAINST ('$s') AS Grade
但很快我意识到了这一点,因为Grade
没有工作,是基于很多东西一样,例如订单的话,每个字的长度等。
我只想知道连续出现的单词的百分比。
EG:
$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);
我看到它应该如何工作的单行,但我没有看到它如何用来选择符合条件的行,除非我在表的每一行中单独运行代码。 – lisovaccaro 2012-07-17 07:50:19
@ Liso22如果您想将此代码应用于较大的结果集 - 将此代码迁移到存储过程,并在循环中调用它。否则,你可以在'INSERT'上调用这段代码 - 这对我来说听起来更理想。 – alfasin 2012-07-17 15:53:24
谢谢@alfasin,这正是我所需要的类似问题! – AdamJones 2014-08-13 01:59:30
怎么样'比萨饼我想 - 我too'?它也是100%吗?什么是评分标准? – alfasin 2012-07-17 06:45:00
也应该是100%。我不在乎是否有额外的单词。 – lisovaccaro 2012-07-17 06:52:25
可能['soundex'](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex)可以帮助你 – diEcho 2012-07-17 06:56:26