Similarity according to Sorensen-Dice coefficient
| 3753 | |
| 3754 | // Similarity according to Sorensen-Dice coefficient |
| 3755 | float String::similarity(const String &p_string) const { |
| 3756 | if (operator==(p_string)) { |
| 3757 | // Equal strings are totally similar |
| 3758 | return 1.0f; |
| 3759 | } |
| 3760 | if (length() < 2 || p_string.length() < 2) { |
| 3761 | // No way to calculate similarity without a single bigram |
| 3762 | return 0.0f; |
| 3763 | } |
| 3764 | |
| 3765 | const int src_size = length() - 1; |
| 3766 | const int tgt_size = p_string.length() - 1; |
| 3767 | |
| 3768 | const int sum = src_size + tgt_size; |
| 3769 | int inter = 0; |
| 3770 | for (int i = 0; i < src_size; i++) { |
| 3771 | const char32_t i0 = get(i); |
| 3772 | const char32_t i1 = get(i + 1); |
| 3773 | |
| 3774 | for (int j = 0; j < tgt_size; j++) { |
| 3775 | if (i0 == p_string.get(j) && i1 == p_string.get(j + 1)) { |
| 3776 | inter++; |
| 3777 | break; |
| 3778 | } |
| 3779 | } |
| 3780 | } |
| 3781 | |
| 3782 | return (2.0f * inter) / sum; |
| 3783 | } |
| 3784 | |
| 3785 | static bool _wildcard_match(const char32_t *p_pattern, const char32_t *p_string, bool p_case_sensitive) { |
| 3786 | switch (*p_pattern) { |
no test coverage detected