0

我有一个界面,用户将在其中输入公司名称。然后,它将他们输入的内容与数据库中的当前条目进行比较,如果发现类似的内容,它会向他们提供选项(以防拼写错误),或者他们可以单击一个按钮,确认他们输入的内容绝对是新的和独特的。

我遇到的问题是它不是很准确,并且经常会出现几十个根本不相似的“相似”匹配!

这就是我现在所拥有的,我没有制作的第一个大型功能,我不清楚它到底做了什么。有没有更简单的方法来实现我想要的?

// Compares strings and determines how similar they are based on a nth letter split     comparison.
function cmp_by_optionNumber($b, $a) {
if ($a["score"] == $b["score"]) return 0;
if ($a["score"] > $b["score"]) return 1;
return -1;
}
function string_compare($str_a, $str_b)
{
$length = strlen($str_a);
$length_b = strlen($str_b);

$i = 0;
$segmentcount = 0;
$segmentsinfo = array();
$segment = '';
while ($i < $length)
{
    $char = substr($str_a, $i, 1);
    if (strpos($str_b, $char) !== FALSE)
    {
        $segment = $segment.$char;
        if (strpos($str_b, $segment) !== FALSE)
        {
            $segmentpos_a = $i - strlen($segment) + 1;
            $segmentpos_b = strpos($str_b, $segment);
            $positiondiff = abs($segmentpos_a - $segmentpos_b);
            $posfactor = ($length - $positiondiff) / $length_b; // <-- ?
            $lengthfactor = strlen($segment)/$length;
           $segmentsinfo[$segmentcount] = array( 'segment' => $segment, 'score' => ($posfactor * $lengthfactor));
        }
        else
        {
             $segment = '';
             $i--;
             $segmentcount++;
         }
     }
     else
     {
         $segment = '';
        $segmentcount++;
     }
     $i++;
 }

 // PHP 5.3 lambda in array_map
 $totalscore = array_sum(array_map(function($v) { return $v['score'];  },    $segmentsinfo));
 return $totalscore;
}

$q = $_POST['stringA'] ;
$qLengthMin = strlen($q) - 5 ; // Part of search calibration. Smaller number = stricter.
$qLengthMax = strlen($q) + 2 ; // not in use.

$main = array() ;

include("pdoconnect.php") ;

$result = $dbh->query("SELECT id, name FROM entity_details WHERE
                  name LIKE '{$q[0]}%'
                  AND CHAR_LENGTH(name) >= '$qLengthMin'
                  #LIMIT 50") ; // The first letter MUST be correct.     This assumption makes checker faster and reduces irrelivant results.
$x = 0 ;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {

$percent = string_compare(strtolower($q), strtolower(rawurldecode($row['name']))) ;

if($percent == 1) {
    //echo 1 ;// 1 signifies an exact match on a company already in our DB.
    echo $row['id'] ;
    exit() ;
}
elseif($percent >= 0.6) { // Part of search calibration. Higher deci number = stricter.
    $x++ ;
    $main[$x]['name'] = rawurldecode($row['name']) ;
    $main[$x]['score'] = round($percent, 2) * 100;

    //array_push($overs, urldecode($row['name']) . " ($percent)<br />") ;
}

}

usort($main, "cmp_by_optionNumber") ;
$z = 0 ;
echo '<div style="overflow-y:scroll;height:175px;width:460px;">' ;
foreach($main as $c) {
if($c['score'] > 100) $c['score'] = 100 ;
if(count($main) > 1) {
echo '<div id="anysuggested' . $z . '" class="hoverdiv" onclick="selectAuto(' . "'score$z'" . ');">' ;
}
else echo '<div id="anysuggested' . $z . '" class="hoverdiv" style="color:#009444;" onclick="selectAuto(' . "'score$z'" . ');">' ;
echo '<span id="autoscore' . $z . '">' . $c['name'] . '</span></div>' ;
$z++ ;
}
echo '</div>' ;
4

2 回答 2

1

比较字符串是一个很大的话题,有很多方法可以做到这一点。一种非常常见的算法称为Levenshtein 差分。这是PHP 中的本机实现,但在 MySQL 中没有。但是,这里有一个您可以使用的实现。

于 2012-04-15T09:21:29.437 回答
0

您需要近似/模糊字符串匹配。

阅读更多关于 http://php.net/manual/en/function.levenshtein.phphttp://www.slideshare.net/kyleburton/fuzzy-string-matching

最好的方法是使用一些基于索引的搜索引擎,例如 SOLR http://lucene.apache.org/solr/

于 2012-04-15T09:23:54.070 回答