Dice Coefficient in PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /*----------------------------------------------------------------------------
  4.  
  5. PhpDiceCoeff - An Implementation of the Dice-Coefficient in PHP
  6.  
  7. Version 0.0.01a
  8. (C)2010 by Emil Endgut
  9.  
  10.  
  11. ==============================================================
  12. This is Friendware, which means that you may use it as if you
  13. wrote it, after you became a friend of mine on Facebook.
  14. Please Visit:
  15. http://www.facebook.com/profile.php?id=100001062124472
  16. ==============================================================
  17.  
  18. Usage:
  19.  
  20. php dice.php "stringA" "stringB"
  21.  
  22. ----------------------------------------------------------------------------*/
  23.  
  24. $str_a =$argv[1];
  25. $str_b =$argv[2];
  26.  
  27. echo "dice(\"".$str_a . "\", \"". $str_b. "\") = " . dice($str_a, $str_b) . "\n";
  28.  
  29. function dice($a, $b)
  30. {
  31. $az=zerleg($a);
  32. $ac=count($az);
  33.  
  34. $bz=zerleg($b);
  35. $bc=count($bz);
  36.  
  37. $is=array_intersect($az, $bz);
  38. $ic=count($is);
  39.  
  40. return 2 * $ic / ($ac + $bc);
  41. }
  42.  
  43. function zerleg($str)
  44. {
  45. $a=strtolower($str);
  46. $len=strlen($a);
  47.  
  48. for($n=2;$n<$len;$n++)
  49. for($i=0 ; $i+$n<=$len ; $i++)
  50. $arr[]=substr($a,$i,$n);
  51. return $arr;
  52. }
  53.  
  54.  
  55.  
  56. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.