Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. Instead of string_match use these two functions below
  2.  
  3.  
  4. static public function string_compare($str_a, $str_b)
  5. {
  6. $length = strlen($str_a);
  7. $length_b = strlen($str_b);
  8.  
  9. $i = 0;
  10. $segmentcount = 0;
  11. $segmentsinfo = array();
  12. $segment = '';
  13. while ($i < $length)
  14. {
  15. $char = substr($str_a, $i, 1);
  16. if (strpos($str_b, $char) !== FALSE)
  17. {
  18. $segment = $segment.$char;
  19. if (strpos($str_b, $segment) !== FALSE)
  20. {
  21. $segmentpos_a = $i - strlen($segment) + 1;
  22. $segmentpos_b = strpos($str_b, $segment);
  23. $positiondiff = abs($segmentpos_a - $segmentpos_b);
  24. $posfactor = ($length - $positiondiff) / $length_b; // <-- ?
  25. $lengthfactor = strlen($segment)/$length;
  26. $segmentsinfo[$segmentcount] = array( 'segment' => $segment, 'score' => ($posfactor * $lengthfactor));
  27. }
  28. else
  29. {
  30. $segment = '';
  31. $i--;
  32. $segmentcount++;
  33. }
  34. }
  35. else
  36. {
  37. $segment = '';
  38. $segmentcount++;
  39. }
  40. $i++;
  41. }
  42.  
  43. // PHP 5.3 lambda in array_map
  44. $totalscore = array_sum(array_map(function($v) { return $v['score']; }, $segmentsinfo));
  45. return $totalscore;
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. class StringCompareJaroWinkler
  56. {
  57. public function compare($str1, $str2)
  58. {
  59. return $this->JaroWinkler($str1, $str2, $PREFIXSCALE = 0.1 );
  60. }
  61.  
  62. private function getCommonCharacters( $string1, $string2, $allowedDistance ){
  63.  
  64. $str1_len = mb_strlen($string1);
  65. $str2_len = mb_strlen($string2);
  66. $temp_string2 = $string2;
  67.  
  68. $commonCharacters='';
  69. for( $i=0; $i < $str1_len; $i++){
  70.  
  71. $noMatch = True;
  72. // compare if char does match inside given allowedDistance
  73. // and if it does add it to commonCharacters
  74. for( $j= max( 0, $i-$allowedDistance ); $noMatch && $j < min( $i + $allowedDistance + 1, $str2_len ); $j++){
  75. if( $temp_string2[$j] == $string1[$i] ){
  76. $noMatch = False;
  77. $commonCharacters .= $string1[$i];
  78. $temp_string2[$j] = '';
  79. }
  80. }
  81. }
  82. return $commonCharacters;
  83. }
  84.  
  85. private function Jaro( $string1, $string2 ){
  86.  
  87. $str1_len = mb_strlen( $string1 );
  88. $str2_len = mb_strlen( $string2 );
  89.  
  90. // theoretical distance
  91. $distance = (int) floor(min( $str1_len, $str2_len ) / 2.0);
  92.  
  93. // get common characters
  94. $commons1 = $this->getCommonCharacters( $string1, $string2, $distance );
  95. $commons2 = $this->getCommonCharacters( $string2, $string1, $distance );
  96.  
  97. if( ($commons1_len = mb_strlen( $commons1 )) == 0) return 0;
  98. if( ($commons2_len = mb_strlen( $commons2 )) == 0) return 0;
  99. // calculate transpositions
  100. $transpositions = 0;
  101. $upperBound = min( $commons1_len, $commons2_len );
  102. for( $i = 0; $i < $upperBound; $i++){
  103. if( $commons1[$i] != $commons2[$i] ) $transpositions++;
  104. }
  105. $transpositions /= 2.0;
  106. // return the Jaro distance
  107. return ($commons1_len/($str1_len) + $commons2_len/($str2_len) + ($commons1_len - $transpositions)/($commons1_len)) / 3.0;
  108.  
  109. }
  110.  
  111. private function getPrefixLength( $string1, $string2, $MINPREFIXLENGTH = 4 ){
  112.  
  113. $n = min( array( $MINPREFIXLENGTH, mb_strlen($string1), mb_strlen($string2) ) );
  114.  
  115. for($i = 0; $i < $n; $i++){
  116. if( $string1[$i] != $string2[$i] ){
  117. // return index of first occurrence of different characters
  118. return $i;
  119. }
  120. }
  121. // first n characters are the same
  122. return $n;
  123. }
  124.  
  125. private function JaroWinkler($string1, $string2, $PREFIXSCALE = 0.1 ){
  126.  
  127. $JaroDistance = $this->Jaro( $string1, $string2 );
  128. $prefixLength = $this->getPrefixLength( $string1, $string2 );
  129. return $JaroDistance + $prefixLength * $PREFIXSCALE * (1.0 - $JaroDistance);
  130. }
  131. }
  132.  
  133. $jw = new StringCompareJaroWinkler();
  134. echo $jw->compare("jonas","asjon");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement