forked from JanX2/NSString-DamerauLevenshtein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevenshteinDistanceSearch.m
67 lines (47 loc) · 1.93 KB
/
LevenshteinDistanceSearch.m
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
#import <Foundation/Foundation.h>
#import "JXTrie.h"
#define DICTIONARY @"/usr/share/dict/words"
#define TARGET @"goober"
#define MAX_COST 1
// Apparently, recreating the trie from the raw word list is faster than archiving/unarchiving using NSKeyedArchiver.
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *target;
NSUInteger maxCost;
NSString *dictionary;
NSTimeInterval start;
NSTimeInterval duration;
if ([[[NSProcessInfo processInfo] arguments] count] < 3) {
fprintf(stderr, "usage: %s [<search string> <maximum distance>] [<dictionary path>]\n",
[[[NSProcessInfo processInfo] processName] UTF8String]);
target = TARGET;
maxCost = MAX_COST;
}
else {
target = [[[NSProcessInfo processInfo] arguments] objectAtIndex:1];
maxCost = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:2] integerValue];
}
if ([[[NSProcessInfo processInfo] arguments] count] > 3) {
dictionary = [[[NSProcessInfo processInfo] arguments] objectAtIndex:3];
}
else {
dictionary = DICTIONARY;
}
// Read dictionary file into a trie
JXTrie *trie;
start = [NSDate timeIntervalSinceReferenceDate];
NSString *wordListText = [NSString stringWithContentsOfFile:dictionary encoding:NSUTF8StringEncoding error:NULL];
trie = [JXTrie trieWithWordListString:wordListText];
duration = [NSDate timeIntervalSinceReferenceDate] - start;
//NSLog(@"\n\n%@", trie);
NSLog(@"Read %lu words into %lu nodes. ", (unsigned long)[trie count], (unsigned long)[trie nodeCount]);
NSLog(@"Creating the trie for \"%@\" took %.4f s. ", dictionary, (double)duration);
NSArray *results = nil;
start = [NSDate timeIntervalSinceReferenceDate];
results = [trie search:target maximumDistance:maxCost];
duration = [NSDate timeIntervalSinceReferenceDate] - start;
NSLog(@"\n%@", results);
NSLog(@"Search for \"%@\" took %.4f s. ", target, (double)duration);
[pool drain];
return 0;
}