forked from rapidfuzz/rapidfuzz-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz.hpp
More file actions
320 lines (302 loc) · 12.1 KB
/
fuzz.hpp
File metadata and controls
320 lines (302 loc) · 12.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* SPDX-License-Identifier: MIT */
/* Copyright © 2020 Max Bachmann */
/* Copyright © 2011 Adam Cohen */
#pragma once
#include "utils.hpp"
#include <type_traits>
namespace rapidfuzz {
namespace fuzz {
/**
* @defgroup Fuzz Fuzz
* A collection of string matching algorithms from FuzzyWuzzy
* @{
*/
/**
* @brief calculates a simple ratio between two strings
*
* @details
* @code{.cpp}
* // score is 96.55
* double score = ratio("this is a test", "this is a test!")
* @endcode
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent ratio(const Sentence1& s1, const Sentence2& s2, const percent score_cutoff = 0);
/**
* @brief calculates the fuzz::ratio of the optimal string alignment
*
* @details
* @code{.cpp}
* // score is 100
* double score = partial_ratio("this is a test", "this is a test!")
* @endcode
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent partial_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Sorts the words in the strings and calculates the fuzz::ratio between
* them
*
* @details
* @code{.cpp}
* // score is 100
* double score = token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a
* bear")
* @endcode
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2, typename CharT1 = char_type<Sentence1>,
typename CharT2 = char_type<Sentence2>>
percent token_sort_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Sorts the words in the strings and calculates the fuzz::partial_ratio
* between them
*
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2, typename CharT1 = char_type<Sentence1>,
typename CharT2 = char_type<Sentence2>>
percent partial_token_sort_ratio(const Sentence1& s1, const Sentence2& s2,
percent score_cutoff = 0);
/**
* @brief Compares the words in the strings based on unique and common words
* between them using fuzz::ratio
*
* @details
* @code{.cpp}
* // score1 is 83.87
* double score1 = token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a
* bear")
* // score2 is 100
* double score2 = token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")
* @endcode
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent token_set_ratio(const Sentence1& s1, const Sentence2& s2, const percent score_cutoff = 0);
/**
* @brief Compares the words in the strings based on unique and common words
* between them using fuzz::partial_ratio
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2, typename CharT1 = char_type<Sentence1>,
typename CharT2 = char_type<Sentence2>>
percent partial_token_set_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Helper method that returns the maximum of fuzz::token_set_ratio and
* fuzz::token_sort_ratio (faster than manually executing the two functions)
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent token_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Helper method that returns the maximum of
* fuzz::partial_token_set_ratio and fuzz::partial_token_sort_ratio (faster than
* manually executing the two functions)
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2, typename CharT1 = char_type<Sentence1>,
typename CharT2 = char_type<Sentence2>>
percent partial_token_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Calculates a quick estimation of fuzz::ratio by comparing the length
* of the two sentences. Guaranteed to be equal or higher than fuzz::ratio
* (internally used by fuzz::ratio already when providing it with a score_cutoff
* to speed up the matching)
*
* @details
* Since it only compares the known lengths of the strings it runs in constant
* time (O(1)), while most other algorithms use a weighted levenshtein distance
* and therefore have a quadratic runtime (O(N*M)). It returns the same result
* as the weighted levenshtein ratio for two strings where one string is part of
* the other string. The main purpose is to have a really quick ratio to remove
* some very bad matches early on
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent length_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Calculates a quick estimation of fuzz::ratio by counting uncommon
* letters between the two sentences. Guaranteed to be equal or higher than
* fuzz::ratio. (internally used by fuzz::ratio already when providing it with a
* score_cutoff to speed up the matching)
*
* @details
* Since it only counts the uncommon characters it runs in linear time (O(N)),
* while most other algorithms use a weighted levenshtein distance and therefore
* have a quadratic runtime (O(N*M)). The result is equal to the weighted
* levenshtein ratio of the two sentences with the letters in a sorted order and
* is therefore always equal or higher than fuzz::ratio
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent quick_lev_ratio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**
* @brief Calculates a weighted ratio based on the other ratio algorithms
*
* @details
* @todo add a detailed description
*
* @tparam Sentence1 This is a string that can be converted to
* basic_string_view<char_type>
* @tparam Sentence2 This is a string that can be converted to
* basic_string_view<char_type>
*
* @param s1 string to compare with s2 (for type info check Template parameters
* above)
* @param s2 string to compare with s1 (for type info check Template parameters
* above)
* @param score_cutoff Optional argument for a score threshold between 0% and
* 100%. Matches with a lower score than this number will not be returned.
* Defaults to 0.
*
* @return returns the ratio between s1 and s2 or 0 when ratio < score_cutoff
*/
template <typename Sentence1, typename Sentence2>
percent WRatio(const Sentence1& s1, const Sentence2& s2, percent score_cutoff = 0);
/**@}*/
} // namespace fuzz
} // namespace rapidfuzz
#include "fuzz.txx"