2011-08-27 6 views
11

私は例えば、PHPの配列で持っている:入力された単語に関連して類似性によって配列をソートする方法。

$arr = array("hello", "try", "hel", "hey hello"); 

は、今私は、アレイと私の$検索VARとの間に最も近い近い単語に基づいてされる配列の再配置をしたいです。

どうすればいいですか?

+0

あなたは、ユーザーが入力した入力値に基づいて、特定の順序で配列を手配意味ですか? – Drewdin

+0

[soundex](http://php.net/manual/de/function.soundex.php)と[metaphone](http://php.net/metaphone)をご覧ください。これは、2つの単語の違いを計算するPHPの一般的な2つの方法です。しかし、私は現在、意味のある指標に基づいてそれらをどのように並べ替えるべきか考えていません。ソート順をもう少し説明できますか? –

答えて

4

あなたはlevenshtein機能

<?php 
// input misspelled word 
$input = 'helllo'; 

// array of words to check against 
$words = array('hello' 'try', 'hel', 'hey hello'); 

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

// loop through words to find the closest 
foreach ($words as $word) { 

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

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

     // closest word is this one (exact match) 
     $closest = $word; 
     $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 "Input word: $input\n"; 
if ($shortest == 0) { 
    echo "Exact match found: $closest\n"; 
} else { 
    echo "Did you mean: $closest?\n"; 
} 

?> 
1

を使用することができるもう一つの方法は、パーセントで結果を返しますsimilar_text機能を使用することです。 続きを見るhttp://www.php.net/manual/en/function.similar-text.phpを参照してください。あなたの配列をソートしたい場合は

2

、あなたはこれを行うことができます。

$arr = array("hello", "try", "hel", "hey hello"); 
$search = "hey"; //your search var 

for($i=0; $i<count($arr); $i++) { 
    $temp_arr[$i] = levenshtein($search, $arr[$i]); 
} 
asort($temp_arr); 
foreach($temp_arr as $k => $v) { 
    $sorted_arr[] = $arr[$k]; 
} 

$sorted_arr

は、検索語に最も近い単語で始まる降順にする必要があります。

10

これはhttp://php.net/manual/en/function.similar-text.phpを使用して、迅速な解決策が考えられます。プログラミングのクラシックで説明したように

これは、2つの文字列の間の類似度を計算:オリバー(ISBN 0-131-00413-1)で世界最高のアルゴリズムを実装。この実装では、Oliverの擬似コードのようにスタックは使用されませんが、プロセス全体の処理速度を上げる場合としない場合があります。このアルゴリズムの複雑さはO(N ** 3)であり、Nは最長ストリングの長さであることにも注意してください。

$userInput = 'Bradley123'; 

$list = array('Bob', 'Brad', 'Britney'); 

usort($list, function ($a, $b) use ($userInput) { 
    similar_text($userInput, $a, $percentA); 
    similar_text($userInput, $b, $percentB); 

    return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1); 
}); 

var_dump($list); //output: array("Brad", "Britney", "Bob"); 

http://php.net/manual/en/function.levenshtein.phpを使用して:

レーベンシュタイン距離はあなたがSTR2にSTR1を変換するために、置き換える挿入または削除する必要があり、文字の最小数として定義されます。アルゴリズムの複雑さはO(m * n)であり、ここでnとmはstr1とstr2の長さであり(similar_text()と比較してむしろ良い、O(max(n、m)** 3)まだ高価です)。

$userInput = 'Bradley123'; 

$list = array('Bob', 'Brad', 'Britney'); 

usort($list, function ($a, $b) use ($userInput) { 
    $levA = levenshtein($userInput, $a); 
    $levB = levenshtein($userInput, $b); 

    return $levA === $levB ? 0 : ($levA > $levB ? 1 : -1); 
}); 

var_dump($list); //output: array("Britney", "Brad", "Bob"); 
+0

これは複数のupvotesを必要とする..ありがとう男! php.net levensteinの例も驚くほど明確です! –

+0

多次元配列にsimilar_textをどのように適用できますか? – MikeeeGeee

関連する問題