配列の代わりに単純なテキストを使用して比較でき、目標がどこにあるかを正しく理解している場合は、levenshtein php関数を使用できます(これは通常、グーグルのような「Did you mean ...?」関数を与えるために使用されますphp検索エンジンで)。
これは、使用するのとは逆の方法で機能します。2つの文字列の差を返します。
例:
<?php
function check($a, $b) {
return levenshtein($a, $b);
}
$a = 'this is just a test';
$b = 'this is not test';
$c = 'this is just a test';
echo check($a, $b) . '<br />';
//return 5
echo check($a, $c) . '<br />';
//return 0, the strings are identical
?>
しかし、これによって実行速度が向上するかどうかは正確にはわかりません。しかし、おそらくそうです。多くのforeachループとarray_merge関数を削除します。
編集:
速度の簡単なテスト(30秒の書き込みスクリプトであり、100%正確ではありません):
function check($terms_in_article1, $terms_in_article2) {
$length1 = count($terms_in_article1); // number of words
$length2 = count($terms_in_article2); // number of words
$all_terms = array_merge($terms_in_article1, $terms_in_article2);
$all_terms = array_unique($all_terms);
foreach ($all_terms as $all_termsa) {
$term_vector1[$all_termsa] = 0;
$term_vector2[$all_termsa] = 0;
}
foreach ($terms_in_article1 as $terms_in_article1a) {
$term_vector1[$terms_in_article1a]++;
}
foreach ($terms_in_article2 as $terms_in_article2a) {
$term_vector2[$terms_in_article2a]++;
}
$score = 0;
foreach ($all_terms as $all_termsa) {
$score += $term_vector1[$all_termsa]*$term_vector2[$all_termsa];
}
$score = $score/($length1*$length2);
$score *= 500; // for better readability
return $score;
}
$a = array('this', 'is', 'just', 'a', 'test');
$b = array('this', 'is', 'not', 'test');
$timenow = microtime();
list($m_i, $t_i) = explode(' ', $timenow);
for($i = 0; $i != 10000; $i++){
check($a, $b);
}
$last = microtime();
list($m_f, $t_f) = explode(' ', $last);
$fine = $m_f+$t_f;
$inizio = $m_i+$t_i;
$quindi = $fine - $inizio;
$quindi = substr($quindi, 0, 7);
echo 'end in ' . $quindi . ' seconds';
印刷:0.36765秒で終了
2番目のテスト:
<?php
function check($a, $b) {
return levenshtein($a, $b);
}
$a = 'this is just a test';
$b = 'this is not test';
$timenow = microtime();
list($m_i, $t_i) = explode(' ', $timenow);
for($i = 0; $i != 10000; $i++){
check($a, $b);
}
$last = microtime();
list($m_f, $t_f) = explode(' ', $last);
$fine = $m_f+$t_f;
$inizio = $m_i+$t_i;
$quindi = $fine - $inizio;
$quindi = substr($quindi, 0, 7);
echo 'end in ' . $quindi . ' seconds';
?>
印刷:0.05023秒で終了
だから、はい、速く見えます。多くの配列アイテム(およびレーベンシュタインの多くの単語)を試してみるといいでしょう
2°編集:
同様のテキストでは、速度はレーベンシュタイン法と同じように見えます。
<?php
function check($a, $b) {
return similar_text($a, $b);
}
$a = 'this is just a test ';
$b = 'this is not test';
$timenow = microtime();
list($m_i, $t_i) = explode(' ', $timenow);
for($i = 0; $i != 10000; $i++){
check($a, $b);
}
$last = microtime();
list($m_f, $t_f) = explode(' ', $last);
$fine = $m_f+$t_f;
$inizio = $m_i+$t_i;
$quindi = $fine - $inizio;
$quindi = substr($quindi, 0, 7);
echo 'end in ' . $quindi . ' seconds';
?>
印刷:0.05988秒で終了
ただし、255文字以上かかる場合があります。
このアルゴリズムの複雑さはO(N ** 3)であることに注意してください。ここで、Nは最長の文字列の長さです。
また、類似性の値をパーセンテージで返すこともできます。
function check($a, $b) {
similar_text($a, $b, $p);
return $p;
}
さらに別の編集
すべてのデータを取得してループするのではなく、SQLクエリで直接比較するために、データベース関数を作成するのはどうですか?
Mysqlを実行している場合は、これを見てください(手作りのレーベンシュタイン関数、まだ255文字の制限)。そうでない場合は、Postgresqlを使用している場合は、この他の関数(評価する必要のある多くの関数)