Passed
Pull Request — master (#10)
by Akpé Aurelle Emmanuel Moïse
02:03
created
src/simpleCommonTextSimilarities.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@
 block discarded – undo
36 36
         
37 37
         public static function wordsReorderOccured($a, $b, $considerPunctuation = true)
38 38
         {
39
-            $filter = function ($v) use ($considerPunctuation) {
39
+            $filter = function($v) use ($considerPunctuation) {
40 40
                 return $considerPunctuation ? !(ctype_space($v) || ctype_punct($v)) : !ctype_space($v);
41 41
             };
42 42
             return self::similarText($a, $b, 2, true, $check, true) && is_array($check) && self::wro_filter($check, $filter) ?true :false;
Please login to merge, or discard this patch.
src/similar_text.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -76,13 +76,13 @@  discard block
 block discarded – undo
76 76
             return $stats;
77 77
         }
78 78
 
79
-        protected static function getParts($b, &$c = 0, $lengthCapture=false)
79
+        protected static function getParts($b, &$c = 0, $lengthCapture = false)
80 80
         {
81 81
             $parts = array();
82 82
             $tmp = '';
83 83
             $c = 0;
84
-            $length=0;
85
-            $lengthCapture=(bool)$lengthCapture;
84
+            $length = 0;
85
+            $lengthCapture = (bool) $lengthCapture;
86 86
             if ($lengthCapture) {
87 87
                 self::capturePartsWithLength($b, $length, $tmp, $c, $parts);
88 88
             } else {
@@ -114,17 +114,17 @@  discard block
 block discarded – undo
114 114
             foreach ($b as $k=>$v) {
115 115
                 $length++;
116 116
                 if (ctype_space($v) || ctype_punct($v)) {
117
-                    $parts[] =array($tmp,$length-1);
118
-                    $parts[] = array($v,1);
117
+                    $parts[] = array($tmp, $length - 1);
118
+                    $parts[] = array($v, 1);
119 119
                     $c += 2;
120 120
                     $tmp = '';
121
-                    $length=0;
121
+                    $length = 0;
122 122
                     continue;
123 123
                 }
124 124
                 $tmp .= $v;
125 125
             }
126 126
             if (!empty($tmp)) {
127
-                $parts[] = array($tmp,$length);
127
+                $parts[] = array($tmp, $length);
128 128
                 $c++;
129 129
             }
130 130
         }
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
             if (is_array($split)) {
147 147
                 return
148 148
                     array_map(
149
-                        function ($val) {
149
+                        function($val) {
150 150
                             if (self::is_ascii($val)) {
151 151
                                 return strtolower($val);
152 152
                             }
@@ -161,29 +161,29 @@  discard block
 block discarded – undo
161 161
             }
162 162
         }
163 163
         
164
-        protected static function split($str, $grams=false)
164
+        protected static function split($str, $grams = false)
165 165
         {
166 166
             if (!is_string($str)) {
167 167
                 return array();
168 168
             }
169 169
             static $split = [];
170 170
             static $old = '';
171
-            static $oldGrams=1;
172
-            $grams=is_int($grams) && $grams >=1 && $grams <= strlen($str) ? $grams : false;
171
+            static $oldGrams = 1;
172
+            $grams = is_int($grams) && $grams >= 1 && $grams <= strlen($str) ? $grams : false;
173 173
             return self::getSplit($str, $split, $old, $oldGrams, $grams);
174 174
         }
175 175
         
176 176
         private static function _split(&$str, &$split, &$old, &$oldGrams, $grams)
177 177
         {
178 178
             $old = $str;
179
-            $oldGrams=$grams;
180
-            $split = !$grams ? preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY):preg_split('/(.{'.$grams.'})/su', $str, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
179
+            $oldGrams = $grams;
180
+            $split = !$grams ? preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY) : preg_split('/(.{'.$grams.'})/su', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
181 181
             return $split;
182 182
         }
183 183
         
184 184
         private static function getSplit(&$str, &$split, &$old, &$oldGrams, $grams)
185 185
         {
186
-            if ($old === $str && $oldGrams===$grams) {
186
+            if ($old === $str && $oldGrams === $grams) {
187 187
                 return $split;
188 188
             } else {
189 189
                 return self::_split($str, $split, $old, $oldGrams, $grams);
Please login to merge, or discard this patch.
similar_text.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
     }
102 102
     
103 103
     
104
-    function dice($a, $b, $round=2)
104
+    function dice($a, $b, $round = 2)
105 105
     {
106 106
         return diceDistance::dice($a, $b, $round);
107 107
     }
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
     }
114 114
     
115 115
     
116
-    function jaroWinkler($a, $b, $round=2)
116
+    function jaroWinkler($a, $b, $round = 2)
117 117
     {
118 118
         return jaroWinklerDistance::jaroWinkler($a, $b, $round);
119 119
     }
Please login to merge, or discard this patch.
src/diceDistance.php 1 patch
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -14,51 +14,51 @@
 block discarded – undo
14 14
 namespace EZAMA{
15 15
     class diceDistance extends distance
16 16
     {
17
-        public static function dice($a, $b, $round=2)
17
+        public static function dice($a, $b, $round = 2)
18 18
         {
19
-            if ($distance=in_array(self::handleVeryCommonDiceCases($a, $b), array(false,0.0,1.0), true)) {
19
+            if ($distance = in_array(self::handleVeryCommonDiceCases($a, $b), array(false, 0.0, 1.0), true)) {
20 20
                 return $distance;
21 21
             }
22
-            static $distance=0;
23
-            static $previous=array();
24
-            if (array($a,$b)===$previous) {
22
+            static $distance = 0;
23
+            static $previous = array();
24
+            if (array($a, $b) === $previous) {
25 25
                 return $distance;
26 26
             }
27
-            $previous=array($a,$b);
28
-            $a=self::split($a, 2);
29
-            $b=self::split($b, 2);
27
+            $previous = array($a, $b);
28
+            $a = self::split($a, 2);
29
+            $b = self::split($b, 2);
30 30
             self::diceDistance($distance, $a, $b, $round);
31 31
             return $distance;
32 32
         }
33 33
         
34 34
         private static function handleVeryCommonDiceCases(&$a, &$b)
35 35
         {
36
-            if (!is_string($a)||!is_string($b)) {
36
+            if (!is_string($a) || !is_string($b)) {
37 37
                 return false;
38 38
             }
39
-            if (empty($a)||empty($b)) {
39
+            if (empty($a) || empty($b)) {
40 40
                 return 0.0;
41 41
             }
42
-            if ($a===$b) {
42
+            if ($a === $b) {
43 43
                 return 1.0;
44 44
             }
45 45
         }
46 46
         
47 47
         private static function diceDistance(&$distance, &$a, &$b, $round)
48 48
         {
49
-            $ca=($caGrams=count($a))*2-self::getEndStrLen($a);
50
-            $cb=($cbGrams=count($b))*2-self::getEndStrLen($b);
51
-            $distance=round(2*count($caGrams>$cbGrams?array_intersect($a, $b):array_intersect($b, $a))/($ca+$cb), $round);
49
+            $ca = ($caGrams = count($a)) * 2 - self::getEndStrLen($a);
50
+            $cb = ($cbGrams = count($b)) * 2 - self::getEndStrLen($b);
51
+            $distance = round(2 * count($caGrams > $cbGrams ?array_intersect($a, $b) : array_intersect($b, $a)) / ($ca + $cb), $round);
52 52
         }
53 53
         
54 54
         private static function getEndStrLen($a)
55 55
         {
56 56
             if (function_exists('array_key_last')) {
57
-                $end=array_key_last($a);
58
-                $end=(isset($end[1]))?0:1;
57
+                $end = array_key_last($a);
58
+                $end = (isset($end[1])) ? 0 : 1;
59 59
             } else {
60
-                $end=end($a);
61
-                $end=(isset($end[1]))?0:1;
60
+                $end = end($a);
61
+                $end = (isset($end[1])) ? 0 : 1;
62 62
                 reset($a);
63 63
             }
64 64
             return $end;
Please login to merge, or discard this patch.
src/hammingDistance.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -16,18 +16,18 @@
 block discarded – undo
16 16
     {
17 17
         public static function hamming($a, $b)
18 18
         {
19
-            if (!is_string($a)||!is_string($b)||(strlen($a)!==strlen($b))) {
19
+            if (!is_string($a) || !is_string($b) || (strlen($a) !== strlen($b))) {
20 20
                 return false;
21 21
             }
22
-            static $distance=0;
23
-            static $previous=array();
24
-            if (array($a,$b)===$previous) {
22
+            static $distance = 0;
23
+            static $previous = array();
24
+            if (array($a, $b) === $previous) {
25 25
                 return $distance;
26 26
             }
27
-            $previous=array($a,$b);
28
-            $a=self::split($a);
29
-            $b=self::split($b);
30
-            $distance=count(array_diff_assoc((array)$a, (array)$b));
27
+            $previous = array($a, $b);
28
+            $a = self::split($a);
29
+            $b = self::split($b);
30
+            $distance = count(array_diff_assoc((array) $a, (array) $b));
31 31
             return $distance;
32 32
         }
33 33
     }
Please login to merge, or discard this patch.
src/jaroWinklerDistance.php 1 patch
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -14,17 +14,17 @@  discard block
 block discarded – undo
14 14
 namespace EZAMA{
15 15
     class jaroWinklerDistance extends distance
16 16
     {
17
-        public static function jaroWinkler($a, $b, $round=2)
17
+        public static function jaroWinkler($a, $b, $round = 2)
18 18
         {
19
-            if (!is_string($a)||!is_string($b)) {
19
+            if (!is_string($a) || !is_string($b)) {
20 20
                 return false;
21 21
             }
22
-            static $distance=array();
23
-            static $previous=array();
24
-            if (array($a,$b)===$previous) {
22
+            static $distance = array();
23
+            static $previous = array();
24
+            if (array($a, $b) === $previous) {
25 25
                 return $distance;
26 26
             }
27
-            $previous=array($a,$b);
27
+            $previous = array($a, $b);
28 28
             return self::getJWDistance($a, $b, $distance, $round);
29 29
         }
30 30
         
@@ -33,15 +33,15 @@  discard block
 block discarded – undo
33 33
         private static function getJWDistance(&$a, &$b, &$distance, $round)
34 34
         {
35 35
             extract(self::prepareJaroWinkler($a, $b));
36
-            for ($i=0,$min=min(count((array)$a), count((array)$b)),$t=0;$i<$min;$i++) {
37
-                if ($a[$i]!==$b[$i]) {
36
+            for ($i = 0, $min = min(count((array) $a), count((array) $b)), $t = 0; $i < $min; $i++) {
37
+                if ($a[$i] !== $b[$i]) {
38 38
                     $t++;
39 39
                 }
40 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) {
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 45
                 return round($v, $round);
46 46
             }, $distance);
47 47
             
@@ -50,15 +50,15 @@  discard block
 block discarded – undo
50 50
         
51 51
         private static function prepareJaroWinkler(&$a, &$b)
52 52
         {
53
-            $a=self::split($a);
54
-            $b=self::split($b);
55
-            $transpositions=array('a'=>array(),'b'=>array(),'corresponding'=>0,'longCommonSubstr'=>0,'ca'=>count((array)$a),'cb'=>count((array)$b));
56
-            $Δ=max($transpositions['ca'], $transpositions['cb'])/2-1;
53
+            $a = self::split($a);
54
+            $b = self::split($b);
55
+            $transpositions = array('a'=>array(), 'b'=>array(), 'corresponding'=>0, 'longCommonSubstr'=>0, 'ca'=>count((array) $a), 'cb'=>count((array) $b));
56
+            $Δ = max($transpositions['ca'], $transpositions['cb']) / 2 - 1;
57 57
             self::jwMatches($a, $b, $transpositions, $Δ);
58 58
             ksort($transpositions['a']);
59 59
             ksort($transpositions['b']);
60
-            $transpositions['a']=array_values($transpositions['a']);
61
-            $transpositions['b']=array_values($transpositions['b']);
60
+            $transpositions['a'] = array_values($transpositions['a']);
61
+            $transpositions['b'] = array_values($transpositions['b']);
62 62
             return $transpositions;
63 63
         }
64 64
         
@@ -73,12 +73,12 @@  discard block
 block discarded – undo
73 73
         
74 74
         private static function _jwMatches($chr, $char, $index, $ind, &$transpositions, $Δ)
75 75
         {
76
-            if ($chr===$char&&(abs($index-$ind)<=$Δ)) {
77
-                if ($ind!==$index) {
78
-                    $transpositions['a'][$ind]=$chr;
79
-                    $transpositions['b'][$index]=$char;
76
+            if ($chr === $char && (abs($index - $ind) <= $Δ)) {
77
+                if ($ind !== $index) {
78
+                    $transpositions['a'][$ind] = $chr;
79
+                    $transpositions['b'][$index] = $char;
80 80
                 } else {
81
-                    if ($ind-1<=$transpositions['longCommonSubstr']) {
81
+                    if ($ind - 1 <= $transpositions['longCommonSubstr']) {
82 82
                         $transpositions['longCommonSubstr']++;
83 83
                     }
84 84
                 }
Please login to merge, or discard this patch.
src/complexCommonTextSimilarities.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -65,7 +65,7 @@  discard block
 block discarded – undo
65 65
             
66 66
             $a = self::getParts(self::strtolower($a));
67 67
             $b = self::getParts(self::strtolower($b));
68
-            foreach ((array)$a as $index=>$word) {
68
+            foreach ((array) $a as $index=>$word) {
69 69
                 if (!self::haveSameRoot($word, $b[$index])) {
70 70
                     return false;
71 71
                 }
@@ -78,32 +78,32 @@  discard block
 block discarded – undo
78 78
             if (!is_string($a) || !is_string($b)) {
79 79
                 return false;
80 80
             }
81
-            $filter = function ($v) {
81
+            $filter = function($v) {
82 82
                 return !(ctype_space($v));
83 83
             };
84 84
             self::filter($a, $b, $filter, true);
85 85
             return self::waorDiff($a, $b, count($a), count($b));
86 86
         }
87 87
         
88
-        private static function filter(&$a, &$b, $filter, $insensitive = true, $captureLength=false)
88
+        private static function filter(&$a, &$b, $filter, $insensitive = true, $captureLength = false)
89 89
         {
90 90
             if ($insensitive) {
91 91
                 $a = array_filter(self::getParts(self::strtolower($a), $c, $captureLength), $filter);
92
-                if ($c===1) {
93
-                    $a=self::strtolower($a);
92
+                if ($c === 1) {
93
+                    $a = self::strtolower($a);
94 94
                 }
95 95
                 $b = array_filter(self::getParts(self::strtolower($b), $c, $captureLength), $filter);
96
-                if ($c===1) {
97
-                    $b=self::strtolower($b);
96
+                if ($c === 1) {
97
+                    $b = self::strtolower($b);
98 98
                 }
99 99
             } else {
100 100
                 $a = array_filter(self::getParts(self::split($a), $c, $captureLength), $filter);
101
-                if ($c===1) {
102
-                    $a=self::strtolower($a);
101
+                if ($c === 1) {
102
+                    $a = self::strtolower($a);
103 103
                 }
104 104
                 $b = array_filter(self::getParts(self::split($b), $c, $captureLength), $filter);
105
-                if ($c===1) {
106
-                    $b=self::strtolower($b);
105
+                if ($c === 1) {
106
+                    $b = self::strtolower($b);
107 107
                 }
108 108
             }
109 109
         }
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
         
117 117
         public static function punctuationChangesOccured($a, $b, $insensitive = true, $considerSpace = true)
118 118
         {
119
-            $filter = function ($v) use ($considerSpace) {
119
+            $filter = function($v) use ($considerSpace) {
120 120
                 return $considerSpace ? !(ctype_space($v) || ctype_punct($v)) : !ctype_punct($v);
121 121
             };
122 122
             if (!is_string($a) || !is_string($b)) {
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
             if (!is_string($a) || !is_string($b)) {
133 133
                 return false;
134 134
             }
135
-            $filter = function ($v) {
135
+            $filter = function($v) {
136 136
                 return !(ctype_space($v[0]) || ctype_punct($v[0]));
137 137
             };
138 138
             
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
         private static function aoeStemming($a, $b)
144 144
         {
145 145
             foreach ($a as $index=>$word) {
146
-                if (!self::haveSameRoot($word[0], $b[$index][0]) || ($a[$index][1]>2 && $b[$index][1]>2)) {
146
+                if (!self::haveSameRoot($word[0], $b[$index][0]) || ($a[$index][1] > 2 && $b[$index][1] > 2)) {
147 147
                     return false;
148 148
                 }
149 149
             }
Please login to merge, or discard this patch.
src/levenshteinDistance.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -16,56 +16,56 @@  discard block
 block discarded – undo
16 16
     {
17 17
         public static function levenshtein($a, $b)
18 18
         {
19
-            if (!is_string($a)||!is_string($b)) {
19
+            if (!is_string($a) || !is_string($b)) {
20 20
                 return false;
21 21
             }
22 22
             
23 23
             
24
-            static $distance=0;
25
-            static $previous=array();
26
-            if (array($a,$b)===$previous) {
24
+            static $distance = 0;
25
+            static $previous = array();
26
+            if (array($a, $b) === $previous) {
27 27
                 return $distance;
28 28
             }
29
-            $previous=array($a,$b);
30
-            $a=self::split($a);
31
-            $b=self::split($b);
32
-            $ca = count((array)$a);
33
-            $cb = count((array)$b);
29
+            $previous = array($a, $b);
30
+            $a = self::split($a);
31
+            $b = self::split($b);
32
+            $ca = count((array) $a);
33
+            $cb = count((array) $b);
34 34
             $dis = range(0, $cb);
35 35
             self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis);
36 36
 
37
-            return $distance=$dis[$cb];
37
+            return $distance = $dis[$cb];
38 38
         }
39 39
         
40 40
         
41 41
         public static function levenshteinDamerau($a, $b)
42 42
         {
43
-            if (!is_string($a)||!is_string($b)) {
43
+            if (!is_string($a) || !is_string($b)) {
44 44
                 return false;
45 45
             }
46 46
               
47
-            static $distance=0;
48
-            static $previous=array();
49
-            if (array($a,$b)===$previous) {
47
+            static $distance = 0;
48
+            static $previous = array();
49
+            if (array($a, $b) === $previous) {
50 50
                 return $distance;
51 51
             }
52
-            $previous=array($a,$b);
53
-            $a=self::split($a);
54
-            $b=self::split($b);
55
-            $ca = count((array)$a);
56
-            $cb = count((array)$b);
52
+            $previous = array($a, $b);
53
+            $a = self::split($a);
54
+            $b = self::split($b);
55
+            $ca = count((array) $a);
56
+            $cb = count((array) $b);
57 57
             $dis = range(0, $cb);
58 58
             self::BuildLevenshteinCostMatrix($a, $b, $ca, $cb, $dis, true);
59 59
         
60
-            return $distance=$dis[$cb];
60
+            return $distance = $dis[$cb];
61 61
         }
62 62
         
63
-        private static function BuildLevenshteinCostMatrix($a, $b, $ca, $cb, &$dis, $damerau=false)
63
+        private static function BuildLevenshteinCostMatrix($a, $b, $ca, $cb, &$dis, $damerau = false)
64 64
         {
65
-            $dis_new=array();
66
-            for ($x=1;$x<=$ca;$x++) {
67
-                $dis_new[0]=$x;
68
-                for ($y=1;$y<=$cb;$y++) {
65
+            $dis_new = array();
66
+            for ($x = 1; $x <= $ca; $x++) {
67
+                $dis_new[0] = $x;
68
+                for ($y = 1; $y <= $cb; $y++) {
69 69
                     self::costMatrix($a, $b, $dis_new, $dis, $damerau, $x, $y);
70 70
                 }
71 71
                 $dis = $dis_new;
@@ -74,11 +74,11 @@  discard block
 block discarded – undo
74 74
         
75 75
         private static function costMatrix(&$a, &$b, &$dis_new, &$dis, $damerau, $x, $y)
76 76
         {
77
-            $c = ($a[$x-1] == $b[$y-1])?0:1;
78
-            $dis_new[$y] = min($dis[$y]+1, $dis_new[$y-1]+1, $dis[$y-1]+$c);
77
+            $c = ($a[$x - 1] == $b[$y - 1]) ? 0 : 1;
78
+            $dis_new[$y] = min($dis[$y] + 1, $dis_new[$y - 1] + 1, $dis[$y - 1] + $c);
79 79
             if ($damerau) {
80
-                if ($x > 1 && $y > 1 && $a[$x-1] == $b[$y-2] && $a[$x-2] == $b[$y-1]) {
81
-                    $dis_new[$y]= min($dis_new[$y-1], $dis[$y-3] + $c) ;
80
+                if ($x > 1 && $y > 1 && $a[$x - 1] == $b[$y - 2] && $a[$x - 2] == $b[$y - 1]) {
81
+                    $dis_new[$y] = min($dis_new[$y - 1], $dis[$y - 3] + $c);
82 82
                 }
83 83
             }
84 84
         }
Please login to merge, or discard this patch.