3 Copyright (C) 2014 Andrew Jeffery <andrew@aj.id.au>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "ccan/darray/darray.h"
27 #include "ccan/stringmap/stringmap.h"
28 #include "ccan/tal/tal.h"
29 #include "ccan/tal/str/str.h"
33 #define CHAR_N_VALUES (1 << CHAR_BIT)
35 typedef darray(struct strgrp_grp *) darray_grp;
36 typedef darray(struct strgrp_item *) darray_item;
38 typedef stringmap(struct strgrp_grp *) stringmap_grp;
41 struct strgrp_grp *grp;
45 typedef darray(struct grp_score *) darray_score;
52 struct grp_score *scores;
53 int16_t pop[CHAR_N_VALUES];
57 const struct strgrp *ctx;
66 int16_t pop[CHAR_N_VALUES];
69 struct strgrp_grp_iter {
70 const struct strgrp_grp *grp;
80 /* String vector cosine similarity[1]
82 * [1] http://blog.nishtahir.com/2015/09/19/fuzzy-string-matching-using-cosine-similarity/
86 strpopcnt(const char *const str, int16_t pop[CHAR_N_VALUES]) {
88 memset(pop, 0, CHAR_N_VALUES * sizeof(*pop));
89 for(c = str; *c; c++) {
91 pop[(unsigned char)*c]++;
96 strcossim(const int16_t ref[CHAR_N_VALUES], const int16_t key[CHAR_N_VALUES]) {
101 for (i = 0; i < CHAR_N_VALUES; i++) {
102 saibi += ref[i] * key[i];
103 sai2 += ref[i] * ref[i];
104 sbi2 += key[i] * key[i];
106 return saibi / (sqrt(sai2) * sqrt(sbi2));
109 /* Low-cost filter functions */
112 should_grp_score_cos(const struct strgrp *const ctx,
113 struct strgrp_grp *const grp, const char *const str) {
114 return ctx->threshold <= strcossim(ctx->pop, grp->pop);
118 should_grp_score_len(const struct strgrp *const ctx,
119 const struct strgrp_grp *const grp, const char *const str) {
120 const size_t strl = strlen(str);
121 const size_t keyl = grp->key_len;
122 double sr = strl / keyl;
126 return ctx->threshold <= sr;
129 /* Scoring - Longest Common Subsequence[2]
131 * [2] https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
135 static inline int cmi(int i, int j) {
139 static inline int16_t
140 lcs(const char *const a, const char *const b) {
141 const int lb = strlen(b);
142 const int lbp1 = lb + 1;
143 int16_t *const lookup = calloc(ROWS * lbp1, sizeof(int16_t));
148 for (ia = (strlen(a) - 1); ia >= 0; ia--) {
149 const char iav = a[ia];
150 const int ial = (ia + 1) & 1; // ia last
151 for (ib = lb - 1; ib >= 0; ib--) {
152 const char ibv = b[ib];
153 const int iac = ia & 1; // ia current
154 const int ibl = ib + 1; // ib last
155 // don't need separate "ib current" as it's just ib
157 lookup[cmi(iac, ib)] = 1 + lookup[cmi(ial, ibl)];
159 const int16_t valb = lookup[cmi(ial, ib)];
160 const int16_t vabl = lookup[cmi(iac, ibl)];
161 lookup[cmi(iac, ib)] = (valb > vabl) ? valb : vabl;
165 int16_t result = lookup[0];
173 nlcs(const char *const a, const char *const b) {
174 const double lcss = lcs(a, b);
175 return 2 * lcss / (strlen(a) + strlen(b));
179 grp_score(const struct strgrp_grp *const grp, const char *const str) {
180 return nlcs(grp->key, str);
183 /* Structure management */
185 static struct strgrp_item *
186 new_item(tal_t *const tctx, const char *const str, void *const data) {
187 struct strgrp_item *i = talz(tctx, struct strgrp_item);
191 i->key = tal_strdup(i, str);
197 add_item(struct strgrp_grp *const ctx, const char *const str,
199 struct strgrp_item *i = new_item(ctx, str, data);
203 darray_push(ctx->items, i);
209 free_grp(struct strgrp_grp *grp) {
210 darray_free(grp->items);
213 static struct strgrp_grp *
214 new_grp(tal_t *const tctx, const char *const str, void *const data) {
215 struct strgrp_grp *b = talz(tctx, struct strgrp_grp);
219 b->key = tal_strdup(b, str);
220 b->key_len = strlen(str);
222 darray_init(b->items);
223 tal_add_destructor(b, free_grp);
224 if (!add_item(b, str, data)) {
230 static struct strgrp_grp *
231 add_grp(struct strgrp *const ctx, const char *const str,
233 struct strgrp_grp *b = new_grp(ctx, str, data);
237 memcpy(b->pop, ctx->pop, sizeof(ctx->pop));
238 darray_push(ctx->grps, b);
241 if (!tal_resize(&ctx->scores, ctx->n_grps)) {
245 ctx->scores = tal_arr(ctx, struct grp_score, ctx->n_grps);
254 strgrp_new(const double threshold) {
255 struct strgrp *ctx = talz(NULL, struct strgrp);
256 ctx->threshold = threshold;
257 stringmap_init(ctx->known, NULL);
258 // n threads compare strings
259 darray_init(ctx->grps);
264 cache(struct strgrp *const ctx, struct strgrp_grp *const grp,
265 const char *const str) {
266 *(stringmap_enter(ctx->known, str)) = grp;
269 static struct strgrp_grp *
270 grp_for(struct strgrp *const ctx, const char *const str) {
271 // Ensure ctx->pop is always populated. Returning null here indicates a new
272 // group should be created, at which point add_grp() copies ctx->pop into
273 // the new group's struct.
274 strpopcnt(str, ctx->pop);
279 struct strgrp_grp **const grp = stringmap_lookup(ctx->known, str);
285 // Keep ccanlint happy in reduced feature mode
287 #pragma omp parallel for schedule(dynamic)
289 for (i = 0; i < ctx->n_grps; i++) {
290 struct strgrp_grp *grp = darray_item(ctx->grps, i);
291 ctx->scores[i].grp = grp;
292 ctx->scores[i].score = 0;
293 if (should_grp_score_len(ctx, grp, str)) {
294 if (should_grp_score_cos(ctx, grp, str)) {
295 ctx->scores[i].score = grp_score(grp, str);
299 struct grp_score *max = NULL;
300 for (i = 0; i < ctx->n_grps; i++) {
301 if (!max || ctx->scores[i].score > max->score) {
302 max = &(ctx->scores[i]);
305 return (max && max->score >= ctx->threshold) ? max->grp : NULL;
308 const struct strgrp_grp *
309 strgrp_grp_for(struct strgrp *const ctx, const char *const str) {
310 return grp_for(ctx, str);
313 const struct strgrp_grp *
314 strgrp_add(struct strgrp *const ctx, const char *const str,
316 bool inserted = false;
317 // grp_for() populates the ctx->pop memory. add_grp() copies this memory
318 // into the strgrp_grp that it creates. It's assumed the ctx->pop memory
319 // has not been modified between the grp_for() and add_grp() calls.
320 struct strgrp_grp *pick = grp_for(ctx, str);
322 inserted = add_item(pick, str, data);
324 pick = add_grp(ctx, str, data);
325 inserted = (NULL != pick);
328 assert(NULL != pick);
329 cache(ctx, pick, str);
335 strgrp_iter_new(struct strgrp *const ctx) {
336 struct strgrp_iter *iter = talz(ctx, struct strgrp_iter);
345 const struct strgrp_grp *
346 strgrp_iter_next(struct strgrp_iter *const iter) {
347 return (iter->ctx->n_grps == iter->i) ?
348 NULL : darray_item(iter->ctx->grps, iter->i++);
352 strgrp_iter_free(struct strgrp_iter *const iter) {
356 struct strgrp_grp_iter *
357 strgrp_grp_iter_new(const struct strgrp_grp *const grp) {
358 struct strgrp_grp_iter *iter = talz(grp, struct strgrp_grp_iter);
367 const struct strgrp_item *
368 strgrp_grp_iter_next(struct strgrp_grp_iter *const iter) {
369 return (iter->grp->n_items == iter->i) ?
370 NULL : darray_item(iter->grp->items, iter->i++);
374 strgrp_grp_iter_free(struct strgrp_grp_iter *iter) {
379 strgrp_grp_key(const struct strgrp_grp *const grp) {
384 strgrp_item_key(const struct strgrp_item *const item) {
389 strgrp_item_value(const struct strgrp_item *const item) {
394 strgrp_free(struct strgrp *const ctx) {
395 darray_free(ctx->grps);
396 stringmap_free(ctx->known);
401 strgrp_free_cb(struct strgrp *const ctx, void (*cb)(void *data)) {
402 struct strgrp_grp **grp;
403 struct strgrp_item **item;
404 darray_foreach(grp, ctx->grps) {
405 darray_foreach(item, (*grp)->items) {
413 print_item(const struct strgrp_item *item) {
414 printf("\t%s\n", item->key);
418 print_grp(const struct strgrp_grp *const grp) {
419 struct strgrp_item **item;
420 printf("%s:\n", grp->key);
421 darray_foreach(item, grp->items) {
428 strgrp_print(const struct strgrp *const ctx) {
429 struct strgrp_grp **grp;
430 darray_foreach(grp, ctx->grps) {