|
| 1 | +/* |
| 2 | + * fzy fuzzy matching algorithm |
| 3 | + * |
| 4 | + * Copyright (c) 2014 John Hawthorn |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <ctype.h> |
| 26 | +#include <string.h> |
| 27 | +#include <strings.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <float.h> |
| 30 | +#include <math.h> |
| 31 | +#include <stdlib.h> |
| 32 | + |
| 33 | +#include "fzy-match.h" |
| 34 | + |
| 35 | +#define SCORE_GAP_LEADING -0.005 |
| 36 | +#define SCORE_GAP_TRAILING -0.005 |
| 37 | +#define SCORE_GAP_INNER -0.01 |
| 38 | +#define SCORE_MATCH_CONSECUTIVE 1.0 |
| 39 | +#define SCORE_MATCH_SLASH 0.9 |
| 40 | +#define SCORE_MATCH_WORD 0.8 |
| 41 | +#define SCORE_MATCH_CAPITAL 0.7 |
| 42 | +#define SCORE_MATCH_DOT 0.6 |
| 43 | + |
| 44 | +#include "fzy-bonus.h" |
| 45 | + |
| 46 | +char *strcasechr(const char *s, char c) { |
| 47 | + const char accept[3] = {c, toupper(c), 0}; |
| 48 | + return strpbrk(s, accept); |
| 49 | +} |
| 50 | + |
| 51 | +int has_match(const char *needle, const char *haystack) { |
| 52 | + while (*needle) { |
| 53 | + char nch = *needle++; |
| 54 | + |
| 55 | + if (!(haystack = strcasechr(haystack, nch))) { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + haystack++; |
| 59 | + } |
| 60 | + return 1; |
| 61 | +} |
| 62 | + |
| 63 | +#define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 64 | + |
| 65 | +struct match_struct { |
| 66 | + int needle_len; |
| 67 | + int haystack_len; |
| 68 | + |
| 69 | + char lower_needle[MATCH_MAX_LEN]; |
| 70 | + char lower_haystack[MATCH_MAX_LEN]; |
| 71 | + |
| 72 | + score_t match_bonus[MATCH_MAX_LEN]; |
| 73 | +}; |
| 74 | + |
| 75 | +static void precompute_bonus(const char *haystack, score_t *match_bonus) { |
| 76 | + /* Which positions are beginning of words */ |
| 77 | + char last_ch = '/'; |
| 78 | + for (int i = 0; haystack[i]; i++) { |
| 79 | + char ch = haystack[i]; |
| 80 | + match_bonus[i] = COMPUTE_BONUS(last_ch, ch); |
| 81 | + last_ch = ch; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +static void setup_match_struct(struct match_struct *match, const char *needle, const char *haystack) { |
| 86 | + match->needle_len = strlen(needle); |
| 87 | + match->haystack_len = strlen(haystack); |
| 88 | + |
| 89 | + if (match->haystack_len > MATCH_MAX_LEN || match->needle_len > match->haystack_len) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + for (int i = 0; i < match->needle_len; i++) |
| 94 | + match->lower_needle[i] = tolower(needle[i]); |
| 95 | + |
| 96 | + for (int i = 0; i < match->haystack_len; i++) |
| 97 | + match->lower_haystack[i] = tolower(haystack[i]); |
| 98 | + |
| 99 | + precompute_bonus(haystack, match->match_bonus); |
| 100 | +} |
| 101 | + |
| 102 | +static inline void match_row(const struct match_struct *match, int row, score_t *curr_D, score_t *curr_M, const score_t *last_D, const score_t *last_M) { |
| 103 | + int n = match->needle_len; |
| 104 | + int m = match->haystack_len; |
| 105 | + int i = row; |
| 106 | + |
| 107 | + const char *lower_needle = match->lower_needle; |
| 108 | + const char *lower_haystack = match->lower_haystack; |
| 109 | + const score_t *match_bonus = match->match_bonus; |
| 110 | + |
| 111 | + score_t prev_score = SCORE_MIN; |
| 112 | + score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER; |
| 113 | + |
| 114 | + /* These will not be used with this value, but not all compilers see it */ |
| 115 | + score_t prev_M = SCORE_MIN, prev_D = SCORE_MIN; |
| 116 | + |
| 117 | + for (int j = 0; j < m; j++) { |
| 118 | + if (lower_needle[i] == lower_haystack[j]) { |
| 119 | + score_t score = SCORE_MIN; |
| 120 | + if (!i) { |
| 121 | + score = (j * SCORE_GAP_LEADING) + match_bonus[j]; |
| 122 | + } else if (j) { /* i > 0 && j > 0*/ |
| 123 | + score = max( |
| 124 | + prev_M + match_bonus[j], |
| 125 | + |
| 126 | + /* consecutive match, doesn't stack with match_bonus */ |
| 127 | + prev_D + SCORE_MATCH_CONSECUTIVE); |
| 128 | + } |
| 129 | + prev_D = last_D[j]; |
| 130 | + prev_M = last_M[j]; |
| 131 | + curr_D[j] = score; |
| 132 | + curr_M[j] = prev_score = max(score, prev_score + gap_score); |
| 133 | + } else { |
| 134 | + prev_D = last_D[j]; |
| 135 | + prev_M = last_M[j]; |
| 136 | + curr_D[j] = SCORE_MIN; |
| 137 | + curr_M[j] = prev_score = prev_score + gap_score; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +score_t match(const char *needle, const char *haystack) { |
| 143 | + if (!*needle) |
| 144 | + return SCORE_MIN; |
| 145 | + |
| 146 | + struct match_struct match; |
| 147 | + setup_match_struct(&match, needle, haystack); |
| 148 | + |
| 149 | + int n = match.needle_len; |
| 150 | + int m = match.haystack_len; |
| 151 | + |
| 152 | + if (m > MATCH_MAX_LEN || n > m) { |
| 153 | + /* |
| 154 | + * Unreasonably large candidate: return no score |
| 155 | + * If it is a valid match it will still be returned, it will |
| 156 | + * just be ranked below any reasonably sized candidates |
| 157 | + */ |
| 158 | + return SCORE_MIN; |
| 159 | + } else if (n == m) { |
| 160 | + /* Since this method can only be called with a haystack which |
| 161 | + * matches needle. If the lengths of the strings are equal the |
| 162 | + * strings themselves must also be equal (ignoring case). |
| 163 | + */ |
| 164 | + return SCORE_MAX; |
| 165 | + } |
| 166 | + |
| 167 | + /* |
| 168 | + * D[][] Stores the best score for this position ending with a match. |
| 169 | + * M[][] Stores the best possible score at this position. |
| 170 | + */ |
| 171 | + score_t D[MATCH_MAX_LEN], M[MATCH_MAX_LEN]; |
| 172 | + |
| 173 | + for (int i = 0; i < n; i++) { |
| 174 | + match_row(&match, i, D, M, D, M); |
| 175 | + } |
| 176 | + |
| 177 | + return M[m - 1]; |
| 178 | +} |
| 179 | + |
| 180 | +score_t match_positions(const char *needle, const char *haystack, size_t *positions) { |
| 181 | + if (!*needle) |
| 182 | + return SCORE_MIN; |
| 183 | + |
| 184 | + struct match_struct match; |
| 185 | + setup_match_struct(&match, needle, haystack); |
| 186 | + |
| 187 | + int n = match.needle_len; |
| 188 | + int m = match.haystack_len; |
| 189 | + |
| 190 | + if (m > MATCH_MAX_LEN || n > m) { |
| 191 | + /* |
| 192 | + * Unreasonably large candidate: return no score |
| 193 | + * If it is a valid match it will still be returned, it will |
| 194 | + * just be ranked below any reasonably sized candidates |
| 195 | + */ |
| 196 | + return SCORE_MIN; |
| 197 | + } else if (n == m) { |
| 198 | + /* Since this method can only be called with a haystack which |
| 199 | + * matches needle. If the lengths of the strings are equal the |
| 200 | + * strings themselves must also be equal (ignoring case). |
| 201 | + */ |
| 202 | + if (positions) |
| 203 | + for (int i = 0; i < n; i++) |
| 204 | + positions[i] = i; |
| 205 | + return SCORE_MAX; |
| 206 | + } |
| 207 | + |
| 208 | + /* |
| 209 | + * D[][] Stores the best score for this position ending with a match. |
| 210 | + * M[][] Stores the best possible score at this position. |
| 211 | + */ |
| 212 | + score_t (*D)[MATCH_MAX_LEN], (*M)[MATCH_MAX_LEN]; |
| 213 | + M = malloc(sizeof(score_t) * MATCH_MAX_LEN * n); |
| 214 | + D = malloc(sizeof(score_t) * MATCH_MAX_LEN * n); |
| 215 | + |
| 216 | + match_row(&match, 0, D[0], M[0], D[0], M[0]); |
| 217 | + for (int i = 1; i < n; i++) { |
| 218 | + match_row(&match, i, D[i], M[i], D[i - 1], M[i - 1]); |
| 219 | + } |
| 220 | + |
| 221 | + /* backtrace to find the positions of optimal matching */ |
| 222 | + if (positions) { |
| 223 | + int match_required = 0; |
| 224 | + for (int i = n - 1, j = m - 1; i >= 0; i--) { |
| 225 | + for (; j >= 0; j--) { |
| 226 | + /* |
| 227 | + * There may be multiple paths which result in |
| 228 | + * the optimal weight. |
| 229 | + * |
| 230 | + * For simplicity, we will pick the first one |
| 231 | + * we encounter, the latest in the candidate |
| 232 | + * string. |
| 233 | + */ |
| 234 | + if (D[i][j] != SCORE_MIN && |
| 235 | + (match_required || D[i][j] == M[i][j])) { |
| 236 | + /* If this score was determined using |
| 237 | + * SCORE_MATCH_CONSECUTIVE, the |
| 238 | + * previous character MUST be a match |
| 239 | + */ |
| 240 | + match_required = |
| 241 | + i && j && |
| 242 | + M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE; |
| 243 | + positions[i] = j--; |
| 244 | + break; |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + score_t result = M[n - 1][m - 1]; |
| 251 | + |
| 252 | + free(M); |
| 253 | + free(D); |
| 254 | + |
| 255 | + return result; |
| 256 | +} |
0 commit comments