6#include <rapidfuzz/details/CharSet.hpp>
7#include <rapidfuzz/details/PatternMatchVector.hpp>
8#include <rapidfuzz/details/common.hpp>
9#include <rapidfuzz/distance/Indel.hpp>
44template <
typename Sentence1,
typename Sentence2>
45double ratio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
47template <
typename InputIt1,
typename InputIt2>
48double ratio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
double score_cutoff = 0);
51namespace experimental {
55 MultiRatio(
size_t count) : input_count(count), scorer(count)
58 size_t result_count()
const
60 return scorer.result_count();
63 template <
typename Sentence1>
64 void insert(
const Sentence1& s1_)
66 insert(detail::to_begin(s1_), detail::to_end(s1_));
69 template <
typename InputIt1>
70 void insert(InputIt1 first1, InputIt1 last1)
72 scorer.insert(first1, last1);
75 template <
typename InputIt2>
76 void similarity(
double* scores,
size_t score_count, InputIt2 first2, InputIt2 last2,
77 double score_cutoff = 0.0)
const
79 similarity(scores, score_count, detail::make_range(first2, last2), score_cutoff);
82 template <
typename Sentence2>
83 void similarity(
double* scores,
size_t score_count,
const Sentence2& s2,
double score_cutoff = 0)
const
85 scorer.normalized_similarity(scores, score_count, s2, score_cutoff / 100.0);
87 for (
size_t i = 0; i < input_count; ++i)
93 rapidfuzz::experimental::MultiIndel<MaxLen> scorer;
99template <
typename CharT1>
101 template <
typename InputIt1>
102 CachedRatio(InputIt1 first1, InputIt1 last1) : cached_indel(first1, last1)
105 template <
typename Sentence1>
106 CachedRatio(
const Sentence1& s1) : cached_indel(s1)
109 template <
typename InputIt2>
110 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
111 double score_hint = 0.0)
const;
113 template <
typename Sentence2>
114 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
117 CachedIndel<CharT1> cached_indel;
120#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
121template <
typename Sentence1>
122CachedRatio(
const Sentence1& s1) -> CachedRatio<char_type<Sentence1>>;
124template <
typename InputIt1>
125CachedRatio(InputIt1 first1, InputIt1 last1) -> CachedRatio<iter_value_t<InputIt1>>;
128template <
typename InputIt1,
typename InputIt2>
129ScoreAlignment<double> partial_ratio_alignment(InputIt1 first1, InputIt1 last1, InputIt2 first2,
130 InputIt2 last2,
double score_cutoff = 0);
132template <
typename Sentence1,
typename Sentence2>
133ScoreAlignment<double> partial_ratio_alignment(
const Sentence1& s1,
const Sentence2& s2,
134 double score_cutoff = 0);
161template <
typename Sentence1,
typename Sentence2>
162double partial_ratio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
164template <
typename InputIt1,
typename InputIt2>
165double partial_ratio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
166 double score_cutoff = 0);
169template <
typename CharT1>
170struct CachedPartialRatio {
172 friend struct CachedWRatio;
174 template <
typename InputIt1>
175 CachedPartialRatio(InputIt1 first1, InputIt1 last1);
177 template <
typename Sentence1>
178 explicit CachedPartialRatio(
const Sentence1& s1_)
179 : CachedPartialRatio(detail::to_begin(s1_), detail::to_end(s1_))
182 template <
typename InputIt2>
183 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
184 double score_hint = 0.0)
const;
186 template <
typename Sentence2>
187 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
190 std::vector<CharT1> s1;
191 rapidfuzz::detail::CharSet<CharT1> s1_char_set;
192 CachedRatio<CharT1> cached_ratio;
195#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
196template <
typename Sentence1>
197explicit CachedPartialRatio(
const Sentence1& s1) -> CachedPartialRatio<char_type<Sentence1>>;
199template <
typename InputIt1>
200CachedPartialRatio(InputIt1 first1, InputIt1 last1) -> CachedPartialRatio<iter_value_t<InputIt1>>;
229template <
typename Sentence1,
typename Sentence2>
230double token_sort_ratio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
232template <
typename InputIt1,
typename InputIt2>
233double token_sort_ratio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
234 double score_cutoff = 0);
237namespace experimental {
239struct MultiTokenSortRatio {
241 MultiTokenSortRatio(
size_t count) : scorer(count)
244 size_t result_count()
const
246 return scorer.result_count();
249 template <
typename Sentence1>
250 void insert(
const Sentence1& s1_)
252 insert(detail::to_begin(s1_), detail::to_end(s1_));
255 template <
typename InputIt1>
256 void insert(InputIt1 first1, InputIt1 last1)
258 scorer.insert(detail::sorted_split(first1, last1).join());
261 template <
typename InputIt2>
262 void similarity(
double* scores,
size_t score_count, InputIt2 first2, InputIt2 last2,
263 double score_cutoff = 0.0)
const
265 scorer.similarity(scores, score_count, detail::sorted_split(first2, last2).join(), score_cutoff);
268 template <
typename Sentence2>
269 void similarity(
double* scores,
size_t score_count,
const Sentence2& s2,
double score_cutoff = 0)
const
271 similarity(scores, score_count, detail::to_begin(s2), detail::to_end(s2), score_cutoff);
275 MultiRatio<MaxLen> scorer;
282template <
typename CharT1>
283struct CachedTokenSortRatio {
284 template <
typename InputIt1>
285 CachedTokenSortRatio(InputIt1 first1, InputIt1 last1)
286 : s1_sorted(detail::sorted_split(first1, last1).join()), cached_ratio(s1_sorted)
289 template <
typename Sentence1>
290 explicit CachedTokenSortRatio(
const Sentence1& s1)
291 : CachedTokenSortRatio(detail::to_begin(s1), detail::to_end(s1))
294 template <
typename InputIt2>
295 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
296 double score_hint = 0.0)
const;
298 template <
typename Sentence2>
299 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
302 std::vector<CharT1> s1_sorted;
303 CachedRatio<CharT1> cached_ratio;
306#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
307template <
typename Sentence1>
308explicit CachedTokenSortRatio(
const Sentence1& s1) -> CachedTokenSortRatio<char_type<Sentence1>>;
310template <
typename InputIt1>
311CachedTokenSortRatio(InputIt1 first1, InputIt1 last1) -> CachedTokenSortRatio<iter_value_t<InputIt1>>;
334template <
typename Sentence1,
typename Sentence2>
337template <
typename InputIt1,
typename InputIt2>
339 double score_cutoff = 0);
342template <
typename CharT1>
343struct CachedPartialTokenSortRatio {
344 template <
typename InputIt1>
345 CachedPartialTokenSortRatio(InputIt1 first1, InputIt1 last1)
346 : s1_sorted(detail::sorted_split(first1, last1).join()), cached_partial_ratio(s1_sorted)
349 template <
typename Sentence1>
350 explicit CachedPartialTokenSortRatio(
const Sentence1& s1)
351 : CachedPartialTokenSortRatio(detail::to_begin(s1), detail::to_end(s1))
354 template <
typename InputIt2>
355 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
356 double score_hint = 0.0)
const;
358 template <
typename Sentence2>
359 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
362 std::vector<CharT1> s1_sorted;
363 CachedPartialRatio<CharT1> cached_partial_ratio;
366#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
367template <
typename Sentence1>
368explicit CachedPartialTokenSortRatio(
const Sentence1& s1)
369 -> CachedPartialTokenSortRatio<char_type<Sentence1>>;
371template <
typename InputIt1>
372CachedPartialTokenSortRatio(InputIt1 first1,
373 InputIt1 last1) -> CachedPartialTokenSortRatio<iter_value_t<InputIt1>>;
404template <
typename Sentence1,
typename Sentence2>
405double token_set_ratio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
407template <
typename InputIt1,
typename InputIt2>
408double token_set_ratio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
409 double score_cutoff = 0);
412template <
typename CharT1>
413struct CachedTokenSetRatio {
414 template <
typename InputIt1>
415 CachedTokenSetRatio(InputIt1 first1, InputIt1 last1)
416 : s1(first1, last1), tokens_s1(detail::sorted_split(std::begin(s1), std::end(s1)))
419 template <
typename Sentence1>
420 explicit CachedTokenSetRatio(
const Sentence1& s1_)
421 : CachedTokenSetRatio(detail::to_begin(s1_), detail::to_end(s1_))
424 template <
typename InputIt2>
425 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
426 double score_hint = 0.0)
const;
428 template <
typename Sentence2>
429 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
432 std::vector<CharT1> s1;
433 detail::SplittedSentenceView<typename std::vector<CharT1>::iterator> tokens_s1;
436#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
437template <
typename Sentence1>
438explicit CachedTokenSetRatio(
const Sentence1& s1) -> CachedTokenSetRatio<char_type<Sentence1>>;
440template <
typename InputIt1>
441CachedTokenSetRatio(InputIt1 first1, InputIt1 last1) -> CachedTokenSetRatio<iter_value_t<InputIt1>>;
463template <
typename Sentence1,
typename Sentence2>
466template <
typename InputIt1,
typename InputIt2>
468 double score_cutoff = 0);
471template <
typename CharT1>
472struct CachedPartialTokenSetRatio {
473 template <
typename InputIt1>
474 CachedPartialTokenSetRatio(InputIt1 first1, InputIt1 last1)
475 : s1(first1, last1), tokens_s1(detail::sorted_split(std::begin(s1), std::end(s1)))
478 template <
typename Sentence1>
479 explicit CachedPartialTokenSetRatio(
const Sentence1& s1_)
480 : CachedPartialTokenSetRatio(detail::to_begin(s1_), detail::to_end(s1_))
483 template <
typename InputIt2>
484 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
485 double score_hint = 0.0)
const;
487 template <
typename Sentence2>
488 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
491 std::vector<CharT1> s1;
492 detail::SplittedSentenceView<typename std::vector<CharT1>::iterator> tokens_s1;
495#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
496template <
typename Sentence1>
497explicit CachedPartialTokenSetRatio(
const Sentence1& s1) -> CachedPartialTokenSetRatio<char_type<Sentence1>>;
499template <
typename InputIt1>
500CachedPartialTokenSetRatio(InputIt1 first1,
501 InputIt1 last1) -> CachedPartialTokenSetRatio<iter_value_t<InputIt1>>;
523template <
typename Sentence1,
typename Sentence2>
524double token_ratio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
526template <
typename InputIt1,
typename InputIt2>
527double token_ratio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
double score_cutoff = 0);
530template <
typename CharT1>
531struct CachedTokenRatio {
532 template <
typename InputIt1>
533 CachedTokenRatio(InputIt1 first1, InputIt1 last1)
535 s1_tokens(detail::sorted_split(std::begin(s1), std::end(s1))),
536 s1_sorted(s1_tokens.join()),
537 cached_ratio_s1_sorted(s1_sorted)
540 template <
typename Sentence1>
541 explicit CachedTokenRatio(
const Sentence1& s1_)
542 : CachedTokenRatio(detail::to_begin(s1_), detail::to_end(s1_))
545 template <
typename InputIt2>
546 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
547 double score_hint = 0.0)
const;
549 template <
typename Sentence2>
550 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
553 std::vector<CharT1> s1;
554 detail::SplittedSentenceView<typename std::vector<CharT1>::iterator> s1_tokens;
555 std::vector<CharT1> s1_sorted;
556 CachedRatio<CharT1> cached_ratio_s1_sorted;
559#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
560template <
typename Sentence1>
561explicit CachedTokenRatio(
const Sentence1& s1) -> CachedTokenRatio<char_type<Sentence1>>;
563template <
typename InputIt1>
564CachedTokenRatio(InputIt1 first1, InputIt1 last1) -> CachedTokenRatio<iter_value_t<InputIt1>>;
587template <
typename Sentence1,
typename Sentence2>
588double partial_token_ratio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
590template <
typename InputIt1,
typename InputIt2>
591double partial_token_ratio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
592 double score_cutoff = 0);
595template <
typename CharT1>
596struct CachedPartialTokenRatio {
597 template <
typename InputIt1>
598 CachedPartialTokenRatio(InputIt1 first1, InputIt1 last1)
600 tokens_s1(detail::sorted_split(std::begin(s1), std::end(s1))),
601 s1_sorted(tokens_s1.join())
604 template <
typename Sentence1>
605 explicit CachedPartialTokenRatio(
const Sentence1& s1_)
606 : CachedPartialTokenRatio(detail::to_begin(s1_), detail::to_end(s1_))
609 template <
typename InputIt2>
610 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
611 double score_hint = 0.0)
const;
613 template <
typename Sentence2>
614 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
617 std::vector<CharT1> s1;
618 detail::SplittedSentenceView<typename std::vector<CharT1>::iterator> tokens_s1;
619 std::vector<CharT1> s1_sorted;
622#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
623template <
typename Sentence1>
624explicit CachedPartialTokenRatio(
const Sentence1& s1) -> CachedPartialTokenRatio<char_type<Sentence1>>;
626template <
typename InputIt1>
627CachedPartialTokenRatio(InputIt1 first1, InputIt1 last1) -> CachedPartialTokenRatio<iter_value_t<InputIt1>>;
651template <
typename Sentence1,
typename Sentence2>
652double WRatio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
654template <
typename InputIt1,
typename InputIt2>
655double WRatio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
double score_cutoff = 0);
658template <
typename CharT1>
660 template <
typename InputIt1>
661 explicit CachedWRatio(InputIt1 first1, InputIt1 last1);
663 template <
typename Sentence1>
664 CachedWRatio(
const Sentence1& s1_) : CachedWRatio(detail::to_begin(s1_), detail::to_end(s1_))
667 template <
typename InputIt2>
668 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
669 double score_hint = 0.0)
const;
671 template <
typename Sentence2>
672 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
677 std::vector<CharT1> s1;
678 CachedPartialRatio<CharT1> cached_partial_ratio;
679 detail::SplittedSentenceView<typename std::vector<CharT1>::iterator> tokens_s1;
680 std::vector<CharT1> s1_sorted;
681 rapidfuzz::detail::BlockPatternMatchVector blockmap_s1_sorted;
684#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
685template <
typename Sentence1>
686explicit CachedWRatio(
const Sentence1& s1) -> CachedWRatio<char_type<Sentence1>>;
688template <
typename InputIt1>
689CachedWRatio(InputIt1 first1, InputIt1 last1) -> CachedWRatio<iter_value_t<InputIt1>>;
713template <
typename Sentence1,
typename Sentence2>
714double QRatio(
const Sentence1& s1,
const Sentence2& s2,
double score_cutoff = 0);
716template <
typename InputIt1,
typename InputIt2>
717double QRatio(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
double score_cutoff = 0);
720namespace experimental {
724 MultiQRatio(
size_t count) : scorer(count)
727 size_t result_count()
const
729 return scorer.result_count();
732 template <
typename Sentence1>
733 void insert(
const Sentence1& s1_)
735 insert(detail::to_begin(s1_), detail::to_end(s1_));
738 template <
typename InputIt1>
739 void insert(InputIt1 first1, InputIt1 last1)
741 scorer.insert(first1, last1);
742 str_lens.push_back(
static_cast<size_t>(std::distance(first1, last1)));
745 template <
typename InputIt2>
746 void similarity(
double* scores,
size_t score_count, InputIt2 first2, InputIt2 last2,
747 double score_cutoff = 0.0)
const
749 similarity(scores, score_count, detail::make_range(first2, last2), score_cutoff);
752 template <
typename Sentence2>
753 void similarity(
double* scores,
size_t score_count,
const Sentence2& s2,
double score_cutoff = 0)
const
755 auto s2_ = detail::make_range(s2);
757 for (
size_t i = 0; i < str_lens.size(); ++i)
763 scorer.similarity(scores, score_count, s2, score_cutoff);
765 for (
size_t i = 0; i < str_lens.size(); ++i)
766 if (str_lens[i] == 0) scores[i] = 0;
770 std::vector<size_t> str_lens;
771 MultiRatio<MaxLen> scorer;
776template <
typename CharT1>
778 template <
typename InputIt1>
779 CachedQRatio(InputIt1 first1, InputIt1 last1) : s1(first1, last1), cached_ratio(first1, last1)
782 template <
typename Sentence1>
783 explicit CachedQRatio(
const Sentence1& s1_) : CachedQRatio(detail::to_begin(s1_), detail::to_end(s1_))
786 template <
typename InputIt2>
787 double similarity(InputIt2 first2, InputIt2 last2,
double score_cutoff = 0.0,
788 double score_hint = 0.0)
const;
790 template <
typename Sentence2>
791 double similarity(
const Sentence2& s2,
double score_cutoff = 0.0,
double score_hint = 0.0)
const;
794 std::vector<CharT1> s1;
795 CachedRatio<CharT1> cached_ratio;
798#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
799template <
typename Sentence1>
800explicit CachedQRatio(
const Sentence1& s1) -> CachedQRatio<char_type<Sentence1>>;
802template <
typename InputIt1>
803CachedQRatio(InputIt1 first1, InputIt1 last1) -> CachedQRatio<iter_value_t<InputIt1>>;
811#include <rapidfuzz/fuzz_impl.hpp>
double WRatio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Calculates a weighted ratio based on the other ratio algorithms.
Definition fuzz_impl.hpp:825
double ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
calculates a simple ratio between two strings
Definition fuzz_impl.hpp:29
double QRatio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Calculates a quick ratio between two strings using fuzz.ratio.
Definition fuzz_impl.hpp:905
double partial_token_set_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Compares the words in the strings based on unique and common words between them using fuzz::partial_r...
Definition fuzz_impl.hpp:491
double token_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Helper method that returns the maximum of fuzz::token_set_ratio and fuzz::token_sort_ratio (faster th...
Definition fuzz_impl.hpp:567
double partial_token_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Helper method that returns the maximum of fuzz::partial_token_set_ratio and fuzz::partial_token_sort_...
Definition fuzz_impl.hpp:733
double token_set_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Compares the words in the strings based on unique and common words between them using fuzz::ratio.
Definition fuzz_impl.hpp:433
double partial_token_sort_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Sorts the words in the strings and calculates the fuzz::partial_ratio between them.
Definition fuzz_impl.hpp:345
double token_sort_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
Sorts the words in the strings and calculates the fuzz::ratio between them.
Definition fuzz_impl.hpp:307
double partial_ratio(const Sentence1 &s1, const Sentence2 &s2, double score_cutoff=0)
calculates the fuzz::ratio of the optimal string alignment
Definition fuzz_impl.hpp:245