我目前正在尝试找出两个字符串中匹配的单词的百分比
其想法是,将一个字符串与另一个字符串进行匹配,并获得它们之间的相似度百分比。
我现在有一个粗略的想法,我想知道我是否可以得到一些帮助,这个想法是将每个字符串转换为一个数组,您可以逐个迭代列表,如果匹配,则将1添加到$matches,如果不匹配,则添加0
<?php>
$originalText = "The quick brown fox jumps over the lazy dog";
$comparisonText = "The quick red fox leaps over the sleeping dog";
//to get a count the number of words we are trying to match
$strNum = str_word_count($originalText);
$arroriginalText = explode(" ", $originalText);
$arrcomparisonText = explode(" ", $comparisonText);
//THIS IS WHERE I'M STUCK
//creating some form of a loop to go through the array of strings
if (preg_match("*word from $arroriginalText*, *word from $arrcomparisonText*", $matches)) {
//not fully understanding what to put here
}
//i'm bad at maths
$strNum - $matches = $percentageFind
$percentageFind / $strNum = $decimal
$decimal * 100 = $theAnswer
?>我不确定我是否已经清楚地表达了我的想法,但如果有任何帮助,我将非常感激。
发布于 2015-01-08 18:27:32
$matches = 0;
if( empty( $arroriginalText ) ) {
echo 'Empty';
die();
}
for( $n = 0; $n < count( $arroriginalText ); $n++ ) {
if( $arrcomparisonText[$n] === $arroriginalText[$n] ) {
$matches++;
}
}
$percentage = 100 * $matches / count( $arroriginalText );或者更好(未测试)
$percentage = count( array_diff( $arroriginalText, $arrcomparisonText ) ) / count( $arroriginalText );https://stackoverflow.com/questions/27837797
复制相似问题