Browse files

added basic suffix support

Signed-off-by: Rifat Nabi <to.rifat@gmail.com>
  • Loading branch information...
1 parent 71d6708 commit 6bad417deb03efd82f9816d4816ab58acc58d6b1 @torifat committed Jul 4, 2012
Showing with 47 additions and 7 deletions.
  1. +2 −0 Database.h
  2. +17 −4 Database.m
  3. +28 −3 Suggestion.m
View
2 Database.h
@@ -9,12 +9,14 @@
@interface Database : NSObject {
NSMutableDictionary* _db;
+ NSMutableDictionary* _suffix;
}
+ (void)allocateSharedInstance;
+ (void)deallocateSharedInstance;
+ (Database *)sharedInstance;
- (NSArray*)find:(NSString*)term;
+- (NSString*)banglaForSuffix:(NSString*)suffix;
@end
View
21 Database.m
@@ -21,6 +21,7 @@ - (id)init {
[RegexParser allocateSharedInstance];
_db = [[NSMutableDictionary alloc] initWithCapacity:0];
+ _suffix = [[NSMutableDictionary alloc] initWithCapacity:0];
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
@@ -76,6 +77,8 @@ - (id)init {
[self loadTableWithName:@"Y" fromDatabase:sqliteDb];
[self loadTableWithName:@"Z" fromDatabase:sqliteDb];
+ [self loadSuffixTableFromDatabase:sqliteDb];
+
[sqliteDb close];
[loopPool release];
@@ -86,6 +89,7 @@ - (id)init {
- (void)dealloc {
[_db release];
+ [_suffix release];
[super dealloc];
}
@@ -108,11 +112,8 @@ - (void)loadTableWithName:(NSString*)name fromDatabase:(FMDatabase*)sqliteDb {
NSMutableArray* items = [[NSMutableArray alloc] init];
FMResultSet *results = [sqliteDb executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", name]];
- int count = 0;
while([results next]) {
- NSString* val = [results stringForColumnIndex:0];
- [items addObject:val];
- count++;
+ [items addObject:[results stringForColumn:@"Words"]];
}
/*
@@ -127,6 +128,14 @@ - (void)loadTableWithName:(NSString*)name fromDatabase:(FMDatabase*)sqliteDb {
[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
@@ -259,4 +268,8 @@ - (NSArray*)find:(NSString*)term {
return [suggestions allObjects];
}
+- (NSString*)banglaForSuffix:(NSString*)suffix {
+ return [_suffix objectForKey:suffix];
+}
+
@end
View
31 Suggestion.m
@@ -58,20 +58,21 @@ - (NSMutableArray*)getList:(NSString*)term {
return _suggestions;
}
+ // Saving humanity by reducing a few CPU cycles
[_suggestions addObjectsFromArray:[[CacheManager sharedInstance] arrayForKey:term]];
if (_suggestions && [_suggestions count] > 0) {
return _suggestions;
}
- // Suggestion form AutoCorrect
+ // Suggestions form AutoCorrect
NSString* autoCorrect = [[AutoCorrect sharedInstance] find:term];
if (autoCorrect) {
[_suggestions addObject:autoCorrect];
}
- // Suggestion from Dictionary
+ // Suggestions from Dictionary
NSArray* dicList = [[Database sharedInstance] find:term];
- // Suggestion from Default Parser
+ // Suggestions from Default Parser
NSString* paresedString = [[AvroParser sharedInstance] parse:term];
if (dicList) {
// Sort dicList based on edit distance
@@ -93,6 +94,30 @@ - (NSMutableArray*)getList:(NSString*)term {
}
}
+ // Suggestions with Suffix
+ int i;
+ for (i = [term length]-1; i > 0; --i) {
+ NSLog(@"Suffix English: %@", [[term substringFromIndex:i] lowercaseString]);
+ NSString* suffix = [[Database sharedInstance] banglaForSuffix:[[term substringFromIndex:i] lowercaseString]];
+ NSLog(@"Suffix Bangla: %@", suffix);
+ if (suffix) {
+ NSString* base = [term substringToIndex:i];
+ NSLog(@"Suffix: %@", base);
+ NSArray* cached = [[CacheManager sharedInstance] arrayForKey:base];
+ // This should always exist, so it's just a safety check
+ if (cached) {
+ for (NSString *item in cached) {
+ NSLog(@"Item: %@", item);
+ // Skip AutoCorrect English Entry
+ if ([base isEqualToString:item]) {
+ continue;
+ }
+ [_suggestions addObject:[NSString stringWithFormat:@"%@%@", item, suffix]];
+ }
+ }
+ }
+ }
+
if ([_suggestions containsObject:paresedString] == NO) {
[_suggestions addObject:paresedString];
}

0 comments on commit 6bad417

Please sign in to comment.