-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzy_search_db.mm
86 lines (72 loc) · 2.21 KB
/
fuzzy_search_db.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
//
// sqlite_store.m
// dsptest1
//
// Created by Lieven Govaerts on 23/01/13.
//
//
#import "fuzzy_search_db.h"
#import "fuzzy_search.h"
@implementation fuzzy_search_db
-(id)init_db:(NSString *)db_name
{
if (self = [super init])
{
int rc;
rc = sqlite3_open([db_name UTF8String], &db);
if(rc)
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
}
return self;
}
-(void)dealloc
{
if (db)
sqlite3_close(db);
[super dealloc];
}
-(NSArray*) str_to_ngrams:(NSString *)str
{
NSMutableArray *results = [[NSMutableArray alloc] init];
str = [str lowercaseString];
int ngramlen = (int)[str length];
ngramlen = ngramlen < 3 ? ngramlen : 3;
for (int i = 0;i <= (int)[str length] -3; i++)
{
NSString *ngram3 = [str substringWithRange:NSMakeRange(i, ngramlen)];
[results addObject:ngram3];
}
return results;
}
static const char* top_movie_query = "select movies.name, count(*) as match from ngrams,movies where " \
"ngram in ('@@') "\
"and movies.id=ngrams.movie group by movie order by match desc limit 10;";
-(NSArray*)find_best_matching_movies:(NSString *)movie
topk:(int)topk
{
NSArray *ngrams = [self str_to_ngrams:movie];
NSString *query = [[NSString alloc] initWithUTF8String:top_movie_query];
NSString *values = [ngrams componentsJoinedByString:@"','"];
query = [query stringByReplacingOccurrencesOfString:@"@@"
withString:values];
sqlite3_stmt *find_movie_stmt;
int rc = sqlite3_prepare_v2(db, [query UTF8String], [query length],
&find_movie_stmt, NULL);
if (rc)
return nil;
NSMutableArray *toplist = [[NSMutableArray alloc] init];
for (int i=0; i<topk; i++)
{
if (sqlite3_step(find_movie_stmt) != SQLITE_ROW)
break;
char *name = (char *)sqlite3_column_text(find_movie_stmt, 0);
// double score = sqlite3_column_double(find_movie_stmt, 1);
[toplist addObject:[NSString stringWithFormat:@"%s", name]];
}
return toplist;
}
@end