Skip to content

Instantly share code, notes, and snippets.

@garyjohnson
Forked from atduskgreg/GABWordnet.h
Created September 30, 2015 15:04
Show Gist options
  • Save garyjohnson/5406d4e492900c892cb4 to your computer and use it in GitHub Desktop.
Save garyjohnson/5406d4e492900c892cb4 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