forked from torifat/iAvro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Database.m
290 lines (259 loc) · 10.1 KB
/
Database.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// AvroKeyboard
//
// Created by Rifat Nabi on 6/28/12.
// Copyright (c) 2012 OmicronLab. All rights reserved.
//
#import "Database.h"
#import "FMDatabase.h"
#import "RegexParser.h"
#import "RegexKitLite.h"
static Database* sharedInstance = nil;
@implementation Database
+ (Database *)sharedInstance {
if (sharedInstance == nil) {
[[self alloc] init]; // assignment not done here, see allocWithZone
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (oneway void)release {
//do nothing
}
- (id)autorelease {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; // This is sooo not zero
}
- (id)init {
self = [super init];
if (self) {
_db = [[NSMutableDictionary alloc] initWithCapacity:0];
_suffix = [[NSMutableDictionary alloc] initWithCapacity:0];
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"db3"];
FMDatabase *sqliteDb = [FMDatabase databaseWithPath:filePath];
[sqliteDb open];
[self loadTableWithName:@"A" fromDatabase:sqliteDb];
[self loadTableWithName:@"AA" fromDatabase:sqliteDb];
[self loadTableWithName:@"B" fromDatabase:sqliteDb];
[self loadTableWithName:@"BH" fromDatabase:sqliteDb];
[self loadTableWithName:@"C" fromDatabase:sqliteDb];
[self loadTableWithName:@"CH" fromDatabase:sqliteDb];
[self loadTableWithName:@"D" fromDatabase:sqliteDb];
[self loadTableWithName:@"Dd" fromDatabase:sqliteDb];
[self loadTableWithName:@"Ddh" fromDatabase:sqliteDb];
[self loadTableWithName:@"Dh" fromDatabase:sqliteDb];
[self loadTableWithName:@"E" fromDatabase:sqliteDb];
[self loadTableWithName:@"G" fromDatabase:sqliteDb];
[self loadTableWithName:@"Gh" fromDatabase:sqliteDb];
[self loadTableWithName:@"H" fromDatabase:sqliteDb];
[self loadTableWithName:@"I" fromDatabase:sqliteDb];
[self loadTableWithName:@"II" fromDatabase:sqliteDb];
[self loadTableWithName:@"J" fromDatabase:sqliteDb];
[self loadTableWithName:@"JH" fromDatabase:sqliteDb];
[self loadTableWithName:@"K" fromDatabase:sqliteDb];
[self loadTableWithName:@"KH" fromDatabase:sqliteDb];
[self loadTableWithName:@"Khandatta" fromDatabase:sqliteDb];
[self loadTableWithName:@"L" fromDatabase:sqliteDb];
[self loadTableWithName:@"M" fromDatabase:sqliteDb];
[self loadTableWithName:@"N" fromDatabase:sqliteDb];
[self loadTableWithName:@"NGA" fromDatabase:sqliteDb];
[self loadTableWithName:@"NN" fromDatabase:sqliteDb];
[self loadTableWithName:@"NYA" fromDatabase:sqliteDb];
[self loadTableWithName:@"O" fromDatabase:sqliteDb];
[self loadTableWithName:@"OI" fromDatabase:sqliteDb];
[self loadTableWithName:@"OU" fromDatabase:sqliteDb];
[self loadTableWithName:@"P" fromDatabase:sqliteDb];
[self loadTableWithName:@"PH" fromDatabase:sqliteDb];
[self loadTableWithName:@"R" fromDatabase:sqliteDb];
[self loadTableWithName:@"RR" fromDatabase:sqliteDb];
[self loadTableWithName:@"RRH" fromDatabase:sqliteDb];
[self loadTableWithName:@"RRI" fromDatabase:sqliteDb];
[self loadTableWithName:@"S" fromDatabase:sqliteDb];
[self loadTableWithName:@"SH" fromDatabase:sqliteDb];
[self loadTableWithName:@"SS" fromDatabase:sqliteDb];
[self loadTableWithName:@"T" fromDatabase:sqliteDb];
[self loadTableWithName:@"TH" fromDatabase:sqliteDb];
[self loadTableWithName:@"TT" fromDatabase:sqliteDb];
[self loadTableWithName:@"TTH" fromDatabase:sqliteDb];
[self loadTableWithName:@"U" fromDatabase:sqliteDb];
[self loadTableWithName:@"UU" fromDatabase:sqliteDb];
[self loadTableWithName:@"Y" fromDatabase:sqliteDb];
[self loadTableWithName:@"Z" fromDatabase:sqliteDb];
[self loadSuffixTableFromDatabase:sqliteDb];
[sqliteDb close];
[loopPool release];
}
return self;
}
- (void)dealloc {
[_db release];
[_suffix release];
[super dealloc];
}
- (void)loadTableWithName:(NSString*)name fromDatabase:(FMDatabase*)sqliteDb {
NSMutableArray* items = [[NSMutableArray alloc] init];
FMResultSet *results = [sqliteDb executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", name]];
while([results next]) {
[items addObject:[results stringForColumn:@"Words"]];
}
/*
NSLog(@"-----------------------------------------------------------------");
NSLog(@"%d items added to key %@", count, name);
NSLog(@"-----------------------------------------------------------------");
*/
[_db setObject:items forKey:[name lowercaseString]];
[results close];
[items release];
}
- (void)loadSuffixTableFromDatabase:(FMDatabase*)sqliteDb {
FMResultSet *results = [sqliteDb executeQuery:[NSString stringWithFormat:@"SELECT * FROM Suffix"]];
while([results next]) {
[_suffix setObject:[results stringForColumn:@"Bangla"] forKey:[results stringForColumn:@"English"]];
}
[results close];
}
- (NSArray*)find:(NSString*)term {
// Left Most Character
unichar lmc = [[term lowercaseString] characterAtIndex:0];
NSString* regex = [NSString stringWithFormat:@"^%@$", [[RegexParser sharedInstance] parse:term]];
NSMutableArray* tableList = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableSet* suggestions = [[NSMutableSet alloc] initWithCapacity:0];
switch (lmc) {
case 'a':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"a", @"aa", @"e", @"oi", @"o", @"nya", @"y", nil]];
break;
case 'b':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"b", @"bh", nil]];
break;
case 'c':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"c", @"ch", @"k", nil]];
break;
case 'd':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"d", @"dh", @"dd", @"ddh", nil]];
break;
case 'e':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"i", @"ii", @"e", @"y", nil]];
break;
case 'f':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"ph", nil]];
break;
case 'g':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"g", @"gh", @"j", nil]];
break;
case 'h':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"h", nil]];
break;
case 'i':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"i", @"ii", @"y", nil]];
break;
case 'j':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"j", @"jh", @"z", nil]];
break;
case 'k':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"k", @"kh", nil]];
break;
case 'l':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"l", nil]];
break;
case 'm':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"h", @"m", nil]];
break;
case 'n':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"n", @"nya", @"nga", @"nn", nil]];
break;
case 'o':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"a", @"u", @"uu", @"oi", @"o", @"ou", @"y", nil]];
break;
case 'p':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"p", @"ph", nil]];
break;
case 'q':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"k", nil]];
break;
case 'r':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"rri", @"h", @"r", @"rr", @"rrh", nil]];
break;
case 's':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"s", @"sh", @"ss", nil]];
break;
case 't':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"t", @"th", @"tt", @"tth", @"khandatta", nil]];
break;
case 'u':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"u", @"uu", @"y", nil]];
break;
case 'v':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"bh", nil]];
break;
case 'w':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"o", nil]];
break;
case 'x':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"e", @"k", nil]];
break;
case 'y':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"i", @"y", nil]];
break;
case 'z':
[tableList addObjectsFromArray:
[NSArray arrayWithObjects:@"h", @"j", @"jh", @"z", nil]];
break;
default:
break;
}
for (NSString* table in tableList) {
NSArray* tableData = [_db objectForKey:table];
for (NSString* tmpString in tableData) {
if ([tmpString isMatchedByRegex:regex]) {
[suggestions addObject:tmpString];
}
}
}
[tableList release];
[suggestions autorelease];
return [suggestions allObjects];
}
- (NSString*)banglaForSuffix:(NSString*)suffix {
return [_suffix objectForKey:suffix];
}
@end