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/>.
23 #include "ccan/darray/darray.h"
24 #include "ccan/stringmap/stringmap.h"
25 #include "ccan/tal/tal.h"
26 #include "ccan/tal/str/str.h"
29 typedef darray(struct strgrp_grp *) darray_grp;
30 typedef darray(struct strgrp_item *) darray_item;
32 typedef stringmap(struct strgrp_grp *) stringmap_grp;
35 struct strgrp_grp *grp;
39 typedef darray(struct grp_score *) darray_score;
46 struct grp_score *scores;
50 const struct strgrp *ctx;
61 struct strgrp_grp_iter {
62 const struct strgrp_grp *grp;
73 static inline int cmi(int i, int j) {
78 lcs(const char *const a, const char *const b) {
79 const int lb = strlen(b);
80 const int lbp1 = lb + 1;
81 int16_t *const lookup = calloc(ROWS * lbp1, sizeof(int16_t));
86 for (ia = (strlen(a) - 1); ia >= 0; ia--) {
87 const char iav = a[ia];
88 for (ib = lb - 1; ib >= 0; ib--) {
89 const char ibv = b[ib];
90 const int ial = (ia + 1) & 1; // ia last
91 const int iac = ia & 1; // ia current
92 const int ibl = ib + 1; // ib last
93 // don't need separate "ib current" as it's just ib
95 lookup[cmi(iac, ib)] = 1 + lookup[cmi(ial, ibl)];
97 const int16_t valb = lookup[cmi(ial, ib)];
98 const int16_t vabl = lookup[cmi(iac, ibl)];
99 lookup[cmi(iac, ib)] = (valb > vabl) ? valb : vabl;
103 int16_t result = lookup[0];
111 nlcs(const char *const a, const char *const b) {
112 const double lcss = lcs(a, b);
113 return 2 * lcss / (strlen(a) + strlen(b));
117 should_grp_score(const struct strgrp *const ctx,
118 const struct strgrp_grp *const grp, const char *const str) {
119 const size_t strl = strlen(str);
120 const size_t keyl = grp->key_len;
121 double sr = strl / keyl;
125 return ctx->threshold <= sr;
129 grp_score(const struct strgrp_grp *const grp, const char *const str) {
130 return nlcs(grp->key, str);
133 static struct strgrp_item *
134 new_item(tal_t *const tctx, const char *const str, void *const data) {
135 struct strgrp_item *i = talz(tctx, struct strgrp_item);
139 i->key = tal_strdup(i, str);
145 add_item(struct strgrp_grp *const ctx, const char *const str,
147 struct strgrp_item *i = new_item(ctx, str, data);
151 darray_push(ctx->items, i);
157 free_grp(struct strgrp_grp *grp) {
158 darray_free(grp->items);
161 static struct strgrp_grp *
162 new_grp(tal_t *const tctx, const char *const str, void *const data) {
163 struct strgrp_grp *b = talz(tctx, struct strgrp_grp);
167 b->key = tal_strdup(b, str);
168 b->key_len = strlen(str);
170 darray_init(b->items);
171 tal_add_destructor(b, free_grp);
172 if (!add_item(b, str, data)) {
178 static struct strgrp_grp *
179 add_grp(struct strgrp *const ctx, const char *const str,
181 struct strgrp_grp *b = new_grp(ctx, str, data);
185 darray_push(ctx->grps, b);
188 if (!tal_resize(&ctx->scores, ctx->n_grps)) {
192 ctx->scores = tal_arr(ctx, struct grp_score, ctx->n_grps);
201 strgrp_new(const double threshold) {
202 struct strgrp *ctx = talz(NULL, struct strgrp);
203 ctx->threshold = threshold;
204 stringmap_init(ctx->known, NULL);
205 darray_init(ctx->grps);
210 cache(struct strgrp *const ctx, struct strgrp_grp *const grp,
211 const char *const str) {
212 *(stringmap_enter(ctx->known, str)) = grp;
215 static struct strgrp_grp *
216 grp_for(struct strgrp *const ctx, const char *const str) {
221 struct strgrp_grp **const grp = stringmap_lookup(ctx->known, str);
227 #pragma omp parallel for schedule(dynamic)
228 for (i = 0; i < ctx->n_grps; i++) {
229 ctx->scores[i].grp = darray_item(ctx->grps, i);
230 const bool ss = should_grp_score(ctx, ctx->scores[i].grp, str);
231 ctx->scores[i].score = ss ? grp_score(ctx->scores[i].grp, str) : 0;
233 struct grp_score *max = NULL;
234 for (i = 0; i < ctx->n_grps; i++) {
235 if (!max || ctx->scores[i].score > max->score) {
236 max = &(ctx->scores[i]);
239 return (max && max->score >= ctx->threshold) ? max->grp : NULL;
242 const struct strgrp_grp *
243 strgrp_grp_for(struct strgrp *const ctx, const char *const str) {
244 return grp_for(ctx, str);
247 const struct strgrp_grp *
248 strgrp_add(struct strgrp *const ctx, const char *const str,
250 bool inserted = false;
251 struct strgrp_grp *pick = grp_for(ctx, str);
253 inserted = add_item(pick, str, data);
255 pick = add_grp(ctx, str, data);
256 inserted = (NULL != pick);
259 assert(NULL != pick);
260 cache(ctx, pick, str);
266 strgrp_iter_new(struct strgrp *const ctx) {
267 struct strgrp_iter *iter = talz(ctx, struct strgrp_iter);
276 const struct strgrp_grp *
277 strgrp_iter_next(struct strgrp_iter *const iter) {
278 return (iter->ctx->n_grps == iter->i) ?
279 NULL : darray_item(iter->ctx->grps, iter->i++);
283 strgrp_iter_free(struct strgrp_iter *const iter) {
287 struct strgrp_grp_iter *
288 strgrp_grp_iter_new(const struct strgrp_grp *const grp) {
289 struct strgrp_grp_iter *iter = talz(grp, struct strgrp_grp_iter);
298 const struct strgrp_item *
299 strgrp_grp_iter_next(struct strgrp_grp_iter *const iter) {
300 return (iter->grp->n_items == iter->i) ?
301 NULL : darray_item(iter->grp->items, iter->i++);
305 strgrp_grp_iter_free(struct strgrp_grp_iter *iter) {
310 strgrp_grp_key(const struct strgrp_grp *const grp) {
315 strgrp_item_key(const struct strgrp_item *const item) {
320 strgrp_item_value(const struct strgrp_item *const item) {
325 strgrp_free(struct strgrp *const ctx) {
326 darray_free(ctx->grps);
327 stringmap_free(ctx->known);
332 strgrp_free_cb(struct strgrp *const ctx, void (*cb)(void *data)) {
333 struct strgrp_grp **grp;
334 struct strgrp_item **item;
335 darray_foreach(grp, ctx->grps) {
336 darray_foreach(item, (*grp)->items) {
346 print_item(const struct strgrp_item *item) {
347 printf("\t%s\n", item->key);
351 print_grp(const struct strgrp_grp *const grp) {
352 struct strgrp_item **item;
353 printf("%s:\n", grp->key);
354 darray_foreach(item, grp->items) {
361 strgrp_print(const struct strgrp *const ctx) {
362 struct strgrp_grp **grp;
363 darray_foreach(grp, ctx->grps) {