Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created August 4, 2014 00:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atduskgreg/c30a5fb167f4c35d6f89 to your computer and use it in GitHub Desktop.
Save atduskgreg/c30a5fb167f4c35d6f89 to your computer and use it in GitHub Desktop.
Thin wrapper around wordnet sql database for getting definitions of words on iOS. Add the wordnet.sqlite3 file to your project and the sqlite3 dylib to the build phases. I downloaded the sqlite db from here: https://code.google.com/p/synonym/downloads
//
// GABWordnic.h
// WordnicSqliteTest
//
// Created by Greg Borenstein on 8/3/14.
// Copyright (c) 2014 Greg Borenstein. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <sqlite3.h>
@interface GABWordnet : NSObject{
sqlite3 *_database;
}
-(NSString*) definitionForWord:(NSString*) word;
@end
//
// GABWordnic.m
// WordnicSqliteTest
//
// Created by Greg Borenstein on 8/3/14.
// Copyright (c) 2014 Greg Borenstein. All rights reserved.
//
#import "GABWordnet.h"
@implementation GABWordnet
- (id)init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"wordnet30"
ofType:@"sqlite3"];
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
} else {
NSLog(@"Database open.");
}
}
return self;
}
-(NSString*) definitionForWord:(NSString*) word{
NSString* query = [[NSString alloc] initWithFormat:@"SELECT definition FROM synset where synsetid=(SELECT synsetid FROM sense WHERE wordid=(SELECT wordid FROM word WHERE lemma='%@'));", word];
NSString* result;
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char* defChars = (char *)sqlite3_column_text(statement, 0);
result = [[NSString alloc] initWithUTF8String:defChars];
}
sqlite3_finalize(statement);
}
return result;
}
- (void)dealloc {
sqlite3_close(_database);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment