forked from wyndow/fuzzywuzzy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFuzz.php
More file actions
278 lines (232 loc) · 7.77 KB
/
Copy pathFuzz.php
File metadata and controls
278 lines (232 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace k\util\fuzzy;
use k\util\fuzzy\Diff\SequenceMatcher;
/**
* A collection of fuzzy string matching algorithms, based on the SeatGeek
* python library of the same name.
*
* @link http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/
*
* @author Michael Crumm <mike@crumm.net>
*/
class Fuzz
{
/**
* Returns a basic ratio score between the two strings.
*
* @param string $s1
* @param string $s2
*
* @return integer
*/
public function ratio($s1, $s2)
{
if (null === $s1) { throw new \UnexpectedValueException('s1 is null'); }
if (null === $s2) { throw new \UnexpectedValueException('s2 is null'); }
if (mb_strlen($s1) === 0 || mb_strlen($s2) === 0) {
return 0;
}
$m = new Diff\SequenceMatcher($s1, $s2);
return Utils::intr(100 * $m->Ratio());
}
/**
*
* @todo Skip duplicate indexes for a little more speed.
*
* @param string $s1
* @param string $s2
*
* @return int
*/
public function partialRatio($s1, $s2)
{
if (null === $s1) { throw new \UnexpectedValueException('s1 is null'); }
if (null === $s2) { throw new \UnexpectedValueException('s2 is null'); }
if (mb_strlen($s1) === 0 || mb_strlen($s2) === 0) {
return 0;
}
if (mb_strlen($s1) <= mb_strlen($s2)) {
$shorter = $s1;
$longer = $s2;
} else {
$shorter = $s2;
$longer = $s1;
}
$m = new Diff\SequenceMatcher($shorter, $longer);
$blocks = $m->getMatchingBlocks();
$scores = [ ];
foreach ($blocks as $block) {
$longStart = $block[1] - $block[0] > 0 ? $block[1] - $block[0] : 0;
$longEnd = $longStart + strlen($shorter);
$longSubstr = mb_substr($longer, $longStart, $longEnd);
$m2 = new Diff\SequenceMatcher($shorter, $longSubstr);
$r = $m2->Ratio();
if ($r > .995) {
return 100;
} else {
$scores[] = $r;
}
}
return Utils::intr(100 * max($scores));
}
/**
* Returns a measure of the sequences' similarity between 0 and 100,
* using different algorithms.
*
* @param string $s1
* @param string $s2
* @param boolean $forceAscii
* @return integer
*/
public function weightedRatio($s1, $s2, $forceAscii = true)
{
$p1 = Utils::fullProcess($s1, $forceAscii);
$p2 = Utils::fullProcess($s2, $forceAscii);
if (! Utils::validateString($p1)) {
return 0;
}
if (! Utils::validateString($p2)) {
return 0;
}
# should we look at partials?
$try_partial = true;
$unbase_scale = .95;
$partial_scale = .90;
$base = $this->ratio($p1, $p2);
$len_ratio = (float)((max(mb_strlen($p1), mb_strlen($p2))) / min(mb_strlen($p1), mb_strlen($p2)));
# if strings are similar length, don't use partials
if ($len_ratio < 1.5) {
$try_partial = false;
}
# if one string is much much shorter than the other
if ($len_ratio > 8) {
$partial_scale = .6;
}
if ($try_partial) {
$partial = $this->partialRatio($p1, $p2) * $partial_scale;
$ptsor = $this->tokenSortPartialRatio($p1, $p2, $forceAscii) * $unbase_scale * $partial_scale;
$ptser = $this->tokenSetPartialRatio($p1, $p2, $forceAscii) * $unbase_scale * $partial_scale;
return (int) max($base, $partial, $ptsor, $ptser);
}
$tsor = $this->tokenSortRatio($p1, $p2, $forceAscii) * $unbase_scale;
$tser = $this->tokenSetRatio($p1, $p2, $forceAscii) * $unbase_scale;
return (int) max($base, $tsor, $tser);
}
/**
* @param $s1
* @param $s2
* @param bool $forceAscii
* @return int|mixed
*/
public function tokenSetPartialRatio($s1, $s2, $forceAscii = true)
{
return $this->tokenSet($s1, $s2, true, $forceAscii);
}
/**
* @param $s1
* @param $s2
* @param bool $forceAscii
* @return int|mixed
*/
public function tokenSetRatio($s1, $s2, $forceAscii = true)
{
return $this->tokenSet($s1, $s2, false, $forceAscii);
}
/**
* @param $s1
* @param $s2
* @param bool $forceAscii
* @return int
*/
public function tokenSortPartialRatio($s1, $s2, $forceAscii = true)
{
return $this->tokenSort($s1, $s2, true, $forceAscii);
}
/**
* @param $s1
* @param $s2
* @param bool $forceAscii
* @return int
*/
public function tokenSortRatio($s1, $s2, $forceAscii = true)
{
return $this->tokenSort($s1, $s2, false, $forceAscii);
}
/**
* Find all alphanumeric tokens in each string...
*
* - treat them as a set
* - construct two strings of the form: <sorted_intersection><sorted_remainder>
* - take ratios of those two strings
* - controls for unordered partial matches
*
* @param $s1
* @param $s2
* @param bool $partial
* @param bool $forceAscii
* @return int|mixed
*/
private function tokenSet($s1, $s2, $partial = true, $forceAscii = true)
{
if (null === $s1) { throw new \UnexpectedValueException('s1 is null'); }
if (null === $s2) { throw new \UnexpectedValueException('s2 is null'); }
$p1 = Utils::fullProcess($s1, $forceAscii);
$p2 = Utils::fullProcess($s2, $forceAscii);
if (!Utils::validateString($p1)) {
return 0;
}
if (!Utils::validateString($p2)) {
return 0;
}
# pull tokens
$tokens1 = StringProcessor::split(Utils::fullProcess($p1));
$tokens2 = StringProcessor::split(Utils::fullProcess($p2));
$intersection = $tokens1->intersection($tokens2);
$diff1to2 = $tokens1->difference($tokens2);
$diff2to1 = $tokens2->difference($tokens1);
$sorted_sect = $intersection->sort()->join();
$sorted_1to2 = $diff1to2->sort()->join();
$sorted_2to1 = $diff2to1->sort()->join();
$combined_1to2 = $sorted_sect . ' ' . $sorted_1to2;
$combined_2to1 = $sorted_sect . ' ' . $sorted_2to1;
# strip
$sorted_sect = trim($sorted_sect);
$combined_1to2 = trim($combined_1to2);
$combined_2to1 = trim($combined_2to1);
$ratioFunc = (boolean) $partial ? 'partialRatio' : 'ratio';
$pairwise = [
call_user_func([$this, $ratioFunc], $sorted_sect, $combined_1to2),
call_user_func([$this, $ratioFunc], $sorted_sect, $combined_2to1),
call_user_func([$this, $ratioFunc], $combined_1to2, $combined_2to1)
];
return max($pairwise);
}
/**
* @param $s1
* @param $s2
* @param bool $partial
* @param bool $forceAscii
* @return int
*/
private function tokenSort($s1, $s2, $partial = true, $forceAscii = true)
{
if (null === $s1) { throw new \UnexpectedValueException('s1 is null'); }
if (null === $s2) { throw new \UnexpectedValueException('s2 is null'); }
$sorted1 = $this->processAndSort($s1, $forceAscii);
$sorted2 = $this->processAndSort($s2, $forceAscii);
if ((boolean) $partial) {
return $this->partialRatio($sorted1, $sorted2);
}
return $this->ratio($sorted1, $sorted2);
}
/**
* @param string $str
* @param boolean $forceAscii
* @return string
*/
private function processAndSort($str, $forceAscii = true)
{
$tokens = StringProcessor::split(Utils::fullProcess($str, $forceAscii));
return StringProcessor::strip($tokens->sort()->join());
}
}