4

このページlevenshtein()では、次の変数を使用して例 #1 を使用しています。

// input misspelled word
$input = 'htc corporation';

// array of words to check against
$words = array('htc', 'Sprint Nextel', 'Sprint', 'banana', 'orange',
        'radish', 'carrot', 'pea', 'bean');

予想される結果がhtcではなくニンジンである理由を教えてください。ありがとう

4

3 に答える 3

4

からのレーベンシュタイン距離htc corporationは 12 であるのに対し、ニンジンまでの距離はわずか 11 であるためです。

levenshtein 関数は、特定の単語を取得するために追加または置換する必要がある文字数を計算htc corporationhtcますhtccarrotそこから言葉にたどり着くには、htc corporation11の変更が必要です。

于 2013-08-02T15:52:58.777 に答える
3

「htc Corporation」から「htc」までの距離は 12 です (「corporation」を削除 = 12 文字)。「htc Corporation」から「carrot」までの距離は 11 以下です。

"htc Corporation" => "corporation": 4
"corporation" => "corporat": 3
"corporat" => "corrat": 2
"corrat" => "carrat": 1
"carrat" => "carrot": 1

4 + 3 + 2 + 1 + 1 = 11

あなたが探しているのは、まっすぐなレーベンシュタイン距離ではなく、「最も近い部分文字列」の一致のようです。ここには、修正されたレーベンシュタイン アルゴリズムを使用した実装例があります。このアルゴリズムを使用すると、次のスコアが得られます。

htc: 0
スプリント Nextel: 11
スプリント: 4
バナナ: 5
オレンジ: 3
大根: 3
ニンジン: 3
エンドウ豆: 2
豆: 3

これは、「htc」を部分文字列の完全一致として認識し、スコアを 0 にします。次点の「エンドウ」のスコアは 2 です。これは、「p」、「e」、または「a」と組み合わせて、他の 2 つの文字を置き換えることができるためです。このアルゴリズムを使用する場合、スコアが「針」文字列の長さよりも高くなることはないことに注意する必要があります。そのため、短い文字列は通常、スコアが低くなります (「一致しやすい」)。

于 2013-08-02T15:57:39.593 に答える
2

レーベンシュタイン距離は、2 つのシーケンス間の差を測定するための文字列メトリックです。非公式には、2 つの単語間のレーベンシュタイン距離は、1 つの単語を別の単語に変更するために必要な 1 文字の編集 (挿入、削除、置換) の最小数です。

ここに簡単な分析があります

$input = 'htc corporation';

// array of words to check against
$words = array(
    'htc',
    'Sprint Nextel',
    'Sprint',
    'banana',
    'orange',
    'radish',
    'carrot',
    'pea',
    'bean' 
);

foreach ( $words as $word ) {

    // Check for Intercept
    $ic = array_intersect(str_split($input), str_split($word));

    printf("%s \t l= %s , s = %s , c = %d \n",$word ,  
    levenshtein($input, $word), 
    similar_text($input, $word), 
    count($ic));
}

出力

htc      l= 12 , s = 3 , c = 5 
Sprint Nextel    l= 14 , s = 3 , c = 8 
Sprint   l= 12 , s = 1 , c = 7 
banana   l= 14 , s = 2 , c = 2 
orange   l= 12 , s = 4 , c = 7 
radish   l= 12 , s = 3 , c = 5 
carrot   l= 11 , s = 1 , c = 10  
pea      l= 13 , s = 2 , c = 2 
bean     l= 13 , s = 2 , c = 2 

htc には の距離があることは明らかですが、htc が12必要な場合は11Levenshteinだけでは十分ではありません..正確な単語を比較して、優先順位を設定する必要があります

于 2013-08-02T15:59:58.253 に答える