-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzy_search.mm
241 lines (194 loc) · 6.74 KB
/
fuzzy_search.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// fuzzy_search.c
// dsptest1
//
// Created by Lieven Govaerts on 20/01/13.
//
//
#import "fuzzy_search.h"
@implementation weighted_str_t
@end
@implementation fuzzy_search
-(id)init
{
if (self = [super init])
{
ngrams = [NSMutableDictionary dictionary];
}
return self;
}
-(void)dealloc
{
[ngrams dealloc];
[super dealloc];
}
-(NSArray*) str_to_ngrams:(NSString *)str
{
NSMutableArray *results = [[NSMutableArray alloc] init];
for (int i = 0;i <= [str length] -3; i++)
{
NSString *ngram3 = [str substringWithRange:NSMakeRange(i, 3)];
[results addObject:ngram3];
}
return results;
}
-(void) ordered_insert:(NSMutableArray*)strings
wstr:(weighted_str_t *)str
{
int index = 0;
if (!str)
return;
for(index = 0; index < [strings count]; index++)
{
weighted_str_t *ws = [strings objectAtIndex:index];
if (str->similarity > ws->similarity)
break;
if ((str->similarity == ws->similarity) &&
(str->weight > ws->weight))
break;
}
[strings insertObject:str atIndex:index];
}
-(void)load_string:(NSString *)str
weight:(int)weight
{
/* Add all ngrams of the string in the ngrams dictionary */
NSArray *str_ngrams = [self str_to_ngrams:str];
for (NSString *ng in str_ngrams)
{
weighted_str_t *wstr = [[weighted_str_t alloc] init];
wstr->str = str; wstr->strid = (long)str;
wstr->weight = weight;
wstr->ngram = ng;
NSMutableArray *strings = [ngrams objectForKey:ng];
if (strings == nil)
{
strings = [[NSMutableArray alloc] init];
[ngrams setObject:strings forKey:ng];
}
/* Ordered insert */
[self ordered_insert:strings wstr:wstr];
}
}
static const int top_k = 1;
-(int)required_freq_treshold:(NSArray*)top_list
{
if ([top_list count] < top_k)
return 1;
weighted_str_t *last = [top_list objectAtIndex:[top_list count]-1];
return last->similarity;
}
-(void)push_next_elements_to_heap:(NSArray *)str_ngrams
pop_ngrams:(NSMutableArray *)pop_ngrams
minimum:(weighted_str_t *)minimum
heap:(NSMutableArray *)heap
{
NSArray *ngrams_to_pop = pop_ngrams ? pop_ngrams : str_ngrams;
/* Insert the top element on each list to the heap */
for (NSString *ng in ngrams_to_pop)
{
NSMutableArray *ar = [ngrams objectForKey:ng];
weighted_str_t *wstr;
if (ar == nil || [ar count] == 0)
continue; /* no more strings for this ngram */
if (minimum)
{
/* find elements to skip and remove them from the ngram stack */
int elems_to_skip;
for (elems_to_skip = 0; elems_to_skip < [ar count]; elems_to_skip++)
{
wstr = [ar objectAtIndex:0];
if (wstr->weight <= minimum->weight)
break;
}
for (int j=0;j < elems_to_skip; j++)
[ar removeObjectAtIndex:0];
}
/* Remove the element from the stack and return to the caller. */
if ([ar count] > 0)
{
wstr = [ar objectAtIndex:0];
printf(" %s:%s\n", [ng UTF8String], [wstr->str UTF8String]);
wstr = [ar objectAtIndex:0];
[self ordered_insert:heap wstr:wstr];
[ar removeObjectAtIndex:0];
}
}
/* All ngrams replenished. */
if (pop_ngrams)
[pop_ngrams removeAllObjects];
}
-(weighted_str_t*)find_string:(NSString *)fs
{
NSArray *str_ngrams = [self str_to_ngrams:fs];
NSMutableArray *top_list = [[NSMutableArray alloc] init];
NSMutableArray *string_heap = [[NSMutableArray alloc] init];
/* Setup the initial heap with one element from each matching ngram. */
[self push_next_elements_to_heap:str_ngrams
pop_ngrams:NULL
minimum:NULL
heap:string_heap];
int freq_treshold = 1;
/* TODO: take care of same ngram multiple times in string */
while ([string_heap count] > 0 && freq_treshold < [str_ngrams count])
{
/* Take the top element, and count the number of appearances on the heap */
int p = 1;
weighted_str_t *top = [string_heap objectAtIndex:(NSUInteger)0];
NSMutableArray *top_ngrams = [[NSMutableArray alloc] init];
[top_ngrams addObject:top->ngram];
printf("heap:");
for (weighted_str_t *s in string_heap)
printf("%s(%d),", [s->str UTF8String], s->weight);
printf("\n");
/* Pop all elements equal to the top element */
for (int i = 1; i < [string_heap count]; i++)
{
weighted_str_t *s = [string_heap objectAtIndex:(NSUInteger)i];
if (s->strid!= top->strid)
break;
[top_ngrams addObject:s->ngram];
p++;
}
for (int i = 0; i<p; i++)
[string_heap removeObjectAtIndex:(NSUInteger)0];
/* */
if (p >= freq_treshold)
{
top->similarity = p;
[self ordered_insert:top_list wstr:top];
if ([top_list count] > top_k)
[top_list removeObjectAtIndex:[top_list count] - 1];
freq_treshold = [self required_freq_treshold:top_list];
/* From each matching ngram list that has element top, pop it and then
add the next element of that ngram list to the heap. */
[self push_next_elements_to_heap:str_ngrams
pop_ngrams:top_ngrams
minimum:NULL
heap:string_heap];
} else
{
/* Pop additional freq_treshold - p - 1 elements from heap */
int c = freq_treshold - p - 1;
for (int i = 0; i < c; i++) {
if ([string_heap count] == 0)
break;
weighted_str_t *top = [string_heap objectAtIndex:(NSUInteger)0];
[top_ngrams addObject:top->ngram];
[string_heap removeObjectAtIndex:(NSUInteger)0];
}
if ([string_heap count] == 0)
break; /* early exit, not enough unique ngrams left */
top = [string_heap objectAtIndex:(NSUInteger)0];
[self push_next_elements_to_heap:str_ngrams
pop_ngrams:top_ngrams
minimum:top
heap:string_heap];
}
}
if ([top_list count] == 0)
return NULL;
weighted_str_t *wstr = [top_list objectAtIndex:(NSUInteger)0];
return wstr;
}
@end