-1

PHP でのテキスト文字列の照合/比較

こんにちは、基本的に製品のフィードに製品があるかどうかを理解するために、いくつかの文字列を比較しようとしています。ソースが異なるため、完全一致 (同一) は確実ではありません。製品の名前には多かれ少なかれ文字が含まれていることがあるので (iPad white と iPad Apple white)、Lucene のあいまい検索 (~) と似たような、おおよその一致を作成したいと思います。

私はこれまで知っていて、preg_matchとlevenshteinを使用しました。PHP の文字列の類似性を照合する他の方法をお勧めできますか?

4

1 に答える 1

2

あなたは誰かが使用法についてアイデアを持っているかどうかを尋ねました.まあ、これはPHPサイトの例ですが、それがあなたの助けになると思います.

(あなたのサイトのエクスペリエンスの 1 つに適合するようにコードを変更しました):

<?php

$productString= 'Apple white IPOD';

// array of words to check against
$products = array('zen','dell laptop','apple laptop','apple black ipod',
                'apple mini','Random product');

// no shortest distance found, yet
$shortest = -1;

// loop through products to find the closest product
foreach ($products as $product) {

    // calculate the distance between the input word,
    // and the current word
    $lev = levenshtein($productString, $product);

    // check for an exact match
    if ($lev == 0) {

        // closest word is this one (exact match)
        $closest = $product;
        $shortest = 0;

        // break out of the loop; we've found an exact match
        break;
    }

    // if this distance is less than the next found shortest
    // distance, OR if a next shortest word has not yet been found
    if ($lev <= $shortest || $shortest < 0) {
        // set the closest match, and shortest distance
        $closest  = $word;
        $shortest = $lev;
    }
}

echo "Search product: $productString\n";
if ($shortest == 0) {
    echo "Exact match found: $closest\n";
} else {
    echo "Did you mean: $closest?\n";
}

?>

上記のコードは、製品のリスト、配列を検索し、最も近い一致を見つけます。完全一致が見つかった場合は、代わりにそれが使用されます。

于 2013-04-02T14:25:58.020 に答える