Total Complexity | 54 |
Total Lines | 206 |
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) |
||
49 | } |
||
50 | |||
51 | private static function prepareJaroWinkler(&$a, &$b) |
||
63 | } |
||
64 | |||
65 | private static function jwMatches(&$a, &$b, &$transpositions, $Δ) |
||
66 | { |
||
67 | foreach ($a as $ind=>$chr) { |
||
68 | foreach ($b as $index=>$char) { |
||
69 | self::_jwMatches($chr, $char, $index, $ind, $transpositions, $Δ); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | private static function _jwMatches($chr, $char, $index, $ind, &$transpositions, $Δ) |
||
86 | } |
||
87 | } |
||
88 | |||
89 | |||
90 | public static function hamming($a, $b) |
||
105 | } |
||
106 | |||
107 | public static function dice($a, $b, $round=2) |
||
108 | { |
||
109 | if ($distance=in_array(self::handleVeryCommonDiceCases($a, $b), array(false,0.0,1.0), true)) { |
||
110 | return $distance; |
||
111 | } |
||
112 | static $distance=0; |
||
113 | static $previous=array(); |
||
114 | if (array($a,$b)===$previous) { |
||
115 | return $distance; |
||
116 | } |
||
117 | $previous=array($a,$b); |
||
118 | $a=self::split($a, 2); |
||
119 | $b=self::split($b, 2); |
||
120 | return self::getDiceDistance($distance, $a, $b, $round); |
||
121 | } |
||
122 | |||
123 | private static function handleVeryCommonDiceCases(&$a, &$b) |
||
124 | { |
||
125 | if (!is_string($a)||!is_string($b)) { |
||
126 | return false; |
||
127 | } |
||
128 | if (empty($a)||empty($b)) { |
||
129 | return 0.0; |
||
130 | } |
||
131 | if ($a===$b) { |
||
132 | return 1.0; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | private static function getDiceDistance(&$distance, &$a, &$b, $round) |
||
137 | { |
||
138 | $ca=($caGrams=count($a))*2-self::getEndStrLen($a); |
||
139 | $cb=($cbGrams=count($b))*2-self::getEndStrLen($b); |
||
140 | $distance=round(2*count($caGrams>$cbGrams?array_intersect($a, $b):array_intersect($b, $a))/($ca+$cb), $round); |
||
141 | } |
||
142 | |||
143 | private static function getEndStrLen($a) |
||
144 | { |
||
145 | if (function_exists('array_key_last')) { |
||
146 | $end=array_key_last($a); |
||
147 | $end=(isset($end[1]))?0:1; |
||
148 | } else { |
||
149 | $end=end($a); |
||
150 | $end=(isset($end[1]))?0:1; |
||
151 | reset($a); |
||
152 | } |
||
153 | return $end; |
||
154 | } |
||
155 | |||
156 | public static function levenshtein($a, $b) |
||
157 | { |
||
158 | if (!is_string($a)||!is_string($b)) { |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | |||
163 | static $distance=0; |
||
164 | static $previous=array(); |
||
165 | if (array($a,$b)===$previous) { |
||
166 | return $distance; |
||
167 | } |
||
168 | $previous=array($a,$b); |
||
169 | $a=self::split($a); |
||
170 | $b=self::split($b); |
||
171 | $ca = count($a); |
||
172 | $cb = count($b); |
||
173 | $dis = range(0, $cb); |
||
174 | self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis); |
||
175 | |||
176 | return $distance=$dis[$cb]; |
||
177 | } |
||
178 | |||
179 | |||
180 | public static function levenshteinDamerau($a, $b) |
||
181 | { |
||
182 | if (!is_string($a)||!is_string($b)) { |
||
183 | return false; |
||
184 | } |
||
185 | |||
186 | static $distance=0; |
||
187 | static $previous=array(); |
||
188 | if (array($a,$b)===$previous) { |
||
189 | return $distance; |
||
190 | } |
||
191 | $previous=array($a,$b); |
||
192 | $a=self::split($a); |
||
193 | $b=self::split($b); |
||
194 | $ca = count($a); |
||
195 | $cb = count($b); |
||
196 | $dis = range(0, $cb); |
||
197 | self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis, true); |
||
198 | |||
199 | return $distance=$dis[$cb]; |
||
200 | } |
||
201 | |||
202 | private static function BuildLevenshteinCostMatrix($a, $b, $ca, $cb, &$dis, $damerau=false) |
||
211 | } |
||
212 | } |
||
213 | |||
214 | private static function costMatrix(&$a, &$b, &$dis_new, &$dis, $damerau, $x, $y) |
||
215 | { |
||
221 | } |
||
222 | } |
||
223 | } |
||
228 |