forked from hdastgheib/fuzzywuzzy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.php
More file actions
140 lines (119 loc) · 5.62 KB
/
Copy pathProcess.php
File metadata and controls
140 lines (119 loc) · 5.62 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
<?php
namespace k\util\fuzzy;
class Process
{
/** @var Fuzz */
private $fuzz;
/** @var \Closure */
private $getString;
/** @var \Closure */
private $getScore;
/**
* @param Fuzz $fuzz
*/
public function __construct(Fuzz $fuzz = null)
{
$this->fuzz = $fuzz ?: new Fuzz();
$this->getString = function ($x) { return $x[0]; };
$this->getScore = function ($x) { return $x[1]; };
}
/**
* Returns a Collection of matches for query, from a list of choices.
*
* @param string $query Query string.
* @param array|\Traversable $choices List of choices to match against.
* @param \Closure $processor Processing function (Default: {@link Utils::fullProcess}.
* @param \Closure $scorer Scoring function (Default: {@link Fuzz::weightedRatio}).
* @param integer $limit Limits the number of returned matches (Default: 5).
* @return Collection
*/
public function extract($query, $choices = [ ], $processor = null, $scorer = null, $limit = 5)
{
$choices = Collection::coerce($choices);
if ($choices->isEmpty()) { return $choices; }
$processor = $processor ?: function ($str) { return Utils::fullProcess($str); };
$scorer = $scorer ?: function ($s1, $s2) { return $this->fuzz->weightedRatio($s1, $s2); };
if (!is_callable($processor)) { throw new \InvalidArgumentException('processor is not callable.'); }
if (!is_callable($scorer)) { throw new \InvalidArgumentException('scorer is not callable.'); }
$scored = new Collection();
foreach ($choices as $choice) {
$processed = $processor($choice);
$score = $scorer($query, $processed);
$scored->add([$choice, $score]);
}
return $scored
->multiSort($scored->map($this->getScore)->toArray(), SORT_DESC, SORT_NUMERIC)
->slice(0, $limit)
;
}
/**
* Returns a Collection of best matches to a collection of choices.
*
* @param string $query Query string.
* @param array|\Traversable $choices List of choices to match against.
* @param \Closure $processor Processing function (Default: {@link Utils::fullProcess}.
* @param \Closure $scorer Scoring function (Default: {@link Fuzz::weightedRatio}).
* @param integer $cutoff Score cutoff for returned matches.
* @param integer $limit Limits the number of returned matches (Default: 5).
* @return Collection
*/
public function extractBests($query, $choices = [ ], $processor = null, $scorer = null, $cutoff = 0, $limit = 5)
{
$bestList = $this->extract($query, $choices, $processor, $scorer, $limit);
return $bestList->filter(function ($x) use ($cutoff) { return $x[1] >= $cutoff; });
}
/**
* Returns the best match for query from a collection of choices.
*
* @param string $query Query string.
* @param array|\Traversable $choices List of choices to match against.
* @param \Closure $processor Processing function (Default: {@link Utils::fullProcess}.
* @param \Closure $scorer Scoring function (Default: {@link Fuzz::weightedRatio}).
* @param integer $cutoff Score cutoff for returned matches.
* @return array
*/
public function extractOne($query, $choices = [ ], $processor = null, $scorer = null, $cutoff = 0)
{
$bestList = $this->extract($query, $choices, $processor, $scorer, 1);
return !$bestList->isEmpty() && $bestList[0][1] > $cutoff ? $bestList[0] : null;
}
/**
* Returns a Collection that has been filtered for duplicates using fuzzy matching.
*
* @param array|\Traversable $containsDupes List containing duplicate strings.
* @param integer $threshold Match threshold.
* @param \Closure $scorer Scoring function.
* @return Collection
*/
public function dedupe($containsDupes = [ ], $threshold = 70, $scorer = null)
{
$containsDupes = Collection::coerce($containsDupes);
$scorer = $scorer ?: function ($s1, $s2) { return $this->fuzz->tokenSetRatio($s1, $s2); };
$extractor = [ ];
# iterate over containsDupes
foreach ($containsDupes as $item) {
# return all duplicate matches found
$matches = $this->extract($item, $containsDupes, null, $scorer, null);
# filter matches based on threshold
$filtered = $matches->filter(function ($x) use ($threshold) { return $x[1] > $threshold; });
# if there is only 1 item in *filtered*, no duplicates were found, so append to *extracted*
if ($filtered->count() === 1) {
$extractor[] = $filtered[0][0];
} else {
# sort length DESC, score DESC, alpha ASC
$filtered = $filtered->multiSort(
$filtered->map(function ($x) { return mb_strlen($x[0]); })->toArray(), SORT_DESC, SORT_NUMERIC,
$filtered->map($this->getScore)->toArray(), SORT_DESC, SORT_NUMERIC,
$filtered->map($this->getString)->toArray(), SORT_ASC, SORT_STRING | SORT_FLAG_CASE
);
$extractor[] = $filtered[0][0];
}
}
# "uniquify" *extractor* list
$keys = [ ];
foreach ($extractor as $e) {
$keys[$e] = 1;
}
return count($extractor) === count($containsDupes) ? $containsDupes : $extractor;
}
}