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 double lstr = (double) strlen(str);
121 const double lkey = (double) grp->key_len;
122 const double lmin = (lstr > lkey) ? lkey : lstr;
123 const double s = sqrt((2 * lmin * lmin) / (1.0 * lstr * lstr + lkey * lkey));
124 return ctx->threshold <= s;
127 /* Scoring - Longest Common Subsequence[2]
129 * [2] https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
133 static inline int cmi(int i, int j) {
137 static inline int16_t
138 lcs(const char *const a, const char *const b) {
139 const int lb = strlen(b);
140 const int lbp1 = lb + 1;
141 int16_t *const lookup = calloc(ROWS * lbp1, sizeof(int16_t));
146 for (ia = (strlen(a) - 1); ia >= 0; ia--) {
147 const char iav = a[ia];
148 const int ial = (ia + 1) & 1; // ia last
149 for (ib = lb - 1; ib >= 0; ib--) {
150 const char ibv = b[ib];
151 const int iac = ia & 1; // ia current
152 const int ibl = ib + 1; // ib last
153 // don't need separate "ib current" as it's just ib
155 lookup[cmi(iac, ib)] = 1 + lookup[cmi(ial, ibl)];
157 const int16_t valb = lookup[cmi(ial, ib)];
158 const int16_t vabl = lookup[cmi(iac, ibl)];
159 lookup[cmi(iac, ib)] = (valb > vabl) ? valb : vabl;
163 int16_t result = lookup[0];
171 nlcs(const char *const a, const char *const b) {
172 const double lcss = lcs(a, b);
173 const double la = (double) strlen(a);
174 const double lb = (double) strlen(b);
175 const double s = sqrt((2 * lcss * lcss) / (la * la + lb * lb));
180 grp_score(const struct strgrp_grp *const grp, const char *const str) {
181 return nlcs(grp->key, str);
184 /* Structure management */
186 static struct strgrp_item *
187 new_item(tal_t *const tctx, const char *const str, void *const data) {
188 struct strgrp_item *i = talz(tctx, struct strgrp_item);
192 i->key = tal_strdup(i, str);
198 add_item(struct strgrp_grp *const ctx, const char *const str,
200 struct strgrp_item *i = new_item(ctx, str, data);
204 darray_push(ctx->items, i);
210 free_grp(struct strgrp_grp *grp) {
211 darray_free(grp->items);
214 static struct strgrp_grp *
215 new_grp(tal_t *const tctx, const char *const str, void *const data) {
216 struct strgrp_grp *b = talz(tctx, struct strgrp_grp);
220 b->key = tal_strdup(b, str);
221 b->key_len = strlen(str);
223 darray_init(b->items);
224 tal_add_destructor(b, free_grp);
225 if (!add_item(b, str, data)) {
231 static struct strgrp_grp *
232 add_grp(struct strgrp *const ctx, const char *const str,
234 struct strgrp_grp *b = new_grp(ctx, str, data);
238 memcpy(b->pop, ctx->pop, sizeof(ctx->pop));
239 darray_push(ctx->grps, b);
242 if (!tal_resize(&ctx->scores, ctx->n_grps)) {
246 ctx->scores = tal_arr(ctx, struct grp_score, ctx->n_grps);
255 strgrp_new(const double threshold) {
256 struct strgrp *ctx = talz(NULL, struct strgrp);
257 ctx->threshold = threshold;
258 stringmap_init(ctx->known, NULL);
259 // n threads compare strings
260 darray_init(ctx->grps);
265 cache(struct strgrp *const ctx, struct strgrp_grp *const grp,
266 const char *const str) {
267 *(stringmap_enter(ctx->known, str)) = grp;
270 static struct strgrp_grp *
271 grp_for(struct strgrp *const ctx, const char *const str) {
272 // Ensure ctx->pop is always populated. Returning null here indicates a new
273 // group should be created, at which point add_grp() copies ctx->pop into
274 // the new group's struct.
275 strpopcnt(str, ctx->pop);
280 struct strgrp_grp **const grp = stringmap_lookup(ctx->known, str);
286 // Keep ccanlint happy in reduced feature mode
288 #pragma omp parallel for schedule(dynamic)
290 for (i = 0; i < ctx->n_grps; i++) {
291 struct strgrp_grp *grp = darray_item(ctx->grps, i);
292 ctx->scores[i].grp = grp;
293 ctx->scores[i].score = 0;
294 if (should_grp_score_len(ctx, grp, str)) {
295 if (should_grp_score_cos(ctx, grp, str)) {
296 ctx->scores[i].score = grp_score(grp, str);
300 struct grp_score *max = NULL;
301 for (i = 0; i < ctx->n_grps; i++) {
302 if (!max || ctx->scores[i].score > max->score) {
303 max = &(ctx->scores[i]);
306 return (max && max->score >= ctx->threshold) ? max->grp : NULL;
309 const struct strgrp_grp *
310 strgrp_grp_for(struct strgrp *const ctx, const char *const str) {
311 return grp_for(ctx, str);
314 const struct strgrp_grp *
315 strgrp_add(struct strgrp *const ctx, const char *const str,
317 bool inserted = false;
318 // grp_for() populates the ctx->pop memory. add_grp() copies this memory
319 // into the strgrp_grp that it creates. It's assumed the ctx->pop memory
320 // has not been modified between the grp_for() and add_grp() calls.
321 struct strgrp_grp *pick = grp_for(ctx, str);
323 inserted = add_item(pick, str, data);
325 pick = add_grp(ctx, str, data);
326 inserted = (NULL != pick);
329 assert(NULL != pick);
330 cache(ctx, pick, str);
336 strgrp_iter_new(struct strgrp *const ctx) {
337 struct strgrp_iter *iter = talz(ctx, struct strgrp_iter);
346 const struct strgrp_grp *
347 strgrp_iter_next(struct strgrp_iter *const iter) {
348 return (iter->ctx->n_grps == iter->i) ?
349 NULL : darray_item(iter->ctx->grps, iter->i++);
353 strgrp_iter_free(struct strgrp_iter *const iter) {
357 struct strgrp_grp_iter *
358 strgrp_grp_iter_new(const struct strgrp_grp *const grp) {
359 struct strgrp_grp_iter *iter = talz(grp, struct strgrp_grp_iter);
368 const struct strgrp_item *
369 strgrp_grp_iter_next(struct strgrp_grp_iter *const iter) {
370 return (iter->grp->n_items == iter->i) ?
371 NULL : darray_item(iter->grp->items, iter->i++);
375 strgrp_grp_iter_free(struct strgrp_grp_iter *iter) {
380 strgrp_grp_key(const struct strgrp_grp *const grp) {
385 strgrp_item_key(const struct strgrp_item *const item) {
390 strgrp_item_value(const struct strgrp_item *const item) {
395 strgrp_free(struct strgrp *const ctx) {
396 darray_free(ctx->grps);
397 stringmap_free(ctx->known);
402 strgrp_free_cb(struct strgrp *const ctx, void (*cb)(void *data)) {
403 struct strgrp_grp **grp;
404 struct strgrp_item **item;
405 darray_foreach(grp, ctx->grps) {
406 darray_foreach(item, (*grp)->items) {
414 print_item(const struct strgrp_item *item) {
415 printf("\t%s\n", item->key);
419 print_grp(const struct strgrp_grp *const grp) {
420 struct strgrp_item **item;
421 printf("%s:\n", grp->key);
422 darray_foreach(item, grp->items) {
429 strgrp_print(const struct strgrp *const ctx) {
430 struct strgrp_grp **grp;
431 darray_foreach(grp, ctx->grps) {