| Total Complexity | 49 |
| Total Lines | 187 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like distance often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use distance, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class distance extends complexCommonTextSimilarities |
||
| 16 | { |
||
| 17 | public static function jaroWinkler($a, $b, $round=2) |
||
| 18 | { |
||
| 19 | if (!is_string($a)||!is_string($b)) { |
||
| 20 | return false; |
||
| 21 | } |
||
| 22 | static $distance=array(); |
||
| 23 | static $previous=array(); |
||
| 24 | if (array($a,$b)===$previous) { |
||
| 25 | return $distance; |
||
| 26 | } |
||
| 27 | $previous=array($a,$b); |
||
| 28 | return self::getJWDistance($a, $b, $distance, $round); |
||
| 29 | } |
||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | private static function getJWDistance(&$a, &$b, &$distance, $round) |
||
| 34 | { |
||
| 35 | extract(self::prepareJaroWinkler($a, $b)); |
||
| 36 | for ($i=0,$min=min(count($a), count($b)),$t=0;$i<$min;$i++) { |
||
| 37 | if ($a[$i]!==$b[$i]) { |
||
| 38 | $t++; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | $t/=2; |
||
| 42 | $distance['jaro']=1/3*($corresponding/$ca+$corresponding/$cb+($corresponding-$t)/$corresponding); |
||
| 43 | $distance['jaro-winkler']=$distance['jaro']+(min($longCommonSubstr, 4)*0.1*(1-$distance['jaro'])); |
||
| 44 | $distance=array_map(function ($v) use ($round) { |
||
| 45 | return round($v, $round); |
||
| 46 | }, $distance); |
||
| 47 | |||
| 48 | return $distance; |
||
| 49 | } |
||
| 50 | |||
| 51 | private static function prepareJaroWinkler(&$a, &$b) |
||
| 52 | { |
||
| 53 | $a=self::split($a); |
||
| 54 | $b=self::split($b); |
||
| 55 | $transpositions=array('a'=>array(),'b'=>array(),'corresponding'=>0,'longCommonSubstr'=>0,'ca'=>count($a),'cb'=>count($b)); |
||
|
|
|||
| 56 | $Δ=max($transpositions['ca'], $transpositions['cb'])/2-1; |
||
| 57 | self::jwMatches($a, $b, $transpositions, $Δ); |
||
| 58 | ksort($transpositions['a']); |
||
| 59 | ksort($transpositions['b']); |
||
| 60 | $transpositions['a']=array_values($transpositions['a']); |
||
| 61 | $transpositions['b']=array_values($transpositions['b']); |
||
| 62 | return $transpositions; |
||
| 63 | } |
||
| 64 | |||
| 65 | private static function jwMatches(&$a, &$b, &$transpositions, $Δ) |
||
| 66 | { |
||
| 67 | foreach ($a as $ind=>$chr) { |
||
| 68 | foreach ($b as $index=>$char) { |
||
| 69 | if ($chr===$char&&(abs($index-$ind)<=$Δ)) { |
||
| 70 | if ($ind!==$index) { |
||
| 71 | $transpositions['a'][$ind]=$chr; |
||
| 72 | $transpositions['b'][$index]=$char; |
||
| 73 | } else { |
||
| 74 | if ($ind-1<=$transpositions['longCommonSubstr']) { |
||
| 75 | $transpositions['longCommonSubstr']++; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | $transpositions['corresponding']++; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | public static function hamming($a, $b) |
||
| 86 | { |
||
| 87 | if (!is_string($a)||!is_string($b)||(strlen($a)!==strlen($b))) { |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | static $distance=0; |
||
| 91 | static $previous=array(); |
||
| 92 | if (array($a,$b)===$previous) { |
||
| 93 | return $distance; |
||
| 94 | } |
||
| 95 | $previous=array($a,$b); |
||
| 96 | $a=self::split($a); |
||
| 97 | $b=self::split($b); |
||
| 98 | $distance=count(array_diff_assoc($a, $b)); |
||
| 99 | return $distance; |
||
| 100 | } |
||
| 101 | |||
| 102 | public static function dice($a, $b, $round=2) |
||
| 103 | { |
||
| 104 | if (!is_string($a)||!is_string($b)) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | if (empty($a)||empty($b)) { |
||
| 108 | return 0.0; |
||
| 109 | } |
||
| 110 | if ($a===$b) { |
||
| 111 | return 1.0; |
||
| 112 | } |
||
| 113 | |||
| 114 | static $distance=0; |
||
| 115 | static $previous=array(); |
||
| 116 | if (array($a,$b)===$previous) { |
||
| 117 | return $distance; |
||
| 118 | } |
||
| 119 | $previous=array($a,$b); |
||
| 120 | $a=self::split($a, 2); |
||
| 121 | $b=self::split($b, 2); |
||
| 122 | $ca=($caGrams=count($a))*2-self::getEndStrLen($a); |
||
| 123 | $cb=($cbGrams=count($b))*2-self::getEndStrLen($b); |
||
| 124 | $distance=round(2*count($caGrams>$cbGrams?array_intersect($a, $b):array_intersect($b, $a))/($ca+$cb), $round); |
||
| 125 | return $distance; |
||
| 126 | } |
||
| 127 | |||
| 128 | private static function getEndStrLen($a) |
||
| 129 | { |
||
| 130 | if (function_exists('array_key_last')) { |
||
| 131 | $end=array_key_last($a); |
||
| 132 | $end=(isset($end[1]))?0:1; |
||
| 133 | } else { |
||
| 134 | $end=end($a); |
||
| 135 | $end=(isset($end[1]))?0:1; |
||
| 136 | reset($a); |
||
| 137 | } |
||
| 138 | return $end; |
||
| 139 | } |
||
| 140 | |||
| 141 | public static function levenshtein($a, $b) |
||
| 142 | { |
||
| 143 | if (!is_string($a)||!is_string($b)) { |
||
| 144 | return false; |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | static $distance=0; |
||
| 149 | static $previous=array(); |
||
| 150 | if (array($a,$b)===$previous) { |
||
| 151 | return $distance; |
||
| 152 | } |
||
| 153 | $previous=array($a,$b); |
||
| 154 | $a=self::split($a); |
||
| 155 | $b=self::split($b); |
||
| 156 | $ca = count($a); |
||
| 157 | $cb = count($b); |
||
| 158 | $dis = range(0, $cb); |
||
| 159 | self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis); |
||
| 160 | |||
| 161 | return $distance=$dis[$cb]; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | public static function levenshteinDamerau($a, $b) |
||
| 166 | { |
||
| 167 | if (!is_string($a)||!is_string($b)) { |
||
| 168 | return false; |
||
| 169 | } |
||
| 170 | |||
| 171 | static $distance=0; |
||
| 172 | static $previous=array(); |
||
| 173 | if (array($a,$b)===$previous) { |
||
| 174 | return $distance; |
||
| 175 | } |
||
| 176 | $previous=array($a,$b); |
||
| 177 | $a=self::split($a); |
||
| 178 | $b=self::split($b); |
||
| 179 | $ca = count($a); |
||
| 180 | $cb = count($b); |
||
| 181 | $dis = range(0, $cb); |
||
| 182 | self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis, true); |
||
| 183 | |||
| 184 | return $distance=$dis[$cb]; |
||
| 185 | } |
||
| 186 | |||
| 187 | private static function BuildLevenshteinCostMatrix($a, $b, $ca, $cb, &$dis, $damerau=false) |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | |||
| 207 | } |
||
| 208 |