iPhone SDK: How to go about a Translator style app
i'm trying to make an app that can basically take text from one box, as entered by the user, and convert it into an equivalent of that text into another text box. When I say equivalent, I mean another way of wording that same word, these would be my own words, and not available on some other database that's already been done somewhere.
I could do this on a small scale with something like a switch statement for all the given possibilities, but for a whole language this has practicality issues.
How would I go about doing this? Would I database all of the words and their equivalents in an SQLite database? And how would I integrate this into an app? I have very limited knowledge when it comes to programming that would include a whole database of values, i'm just used to listing separate values, and am not really aware of the short codes capable of integrating a lot of values. Is it possible to integrate all of the values of an SQL database into a Switch or if statement, that co开发者_开发问答uld separate a user's text entry, into the separate words it contains, and display their equivalents as listed in the database?
Thanks
From your "these would be my own words, and not available on some other database that's already been done somewhere" comment, is it correct to assume your word-for-word replacement is a valid approach to what you want to achieve? (no separate language or precedent to what is right?)
If so, I think you would do something like:
1) take the string from the input field, 2) apply componentsSeparatedByString: to it to have it broken into an array, then, 3) take each item in the array, search your sqlite DB for it, return the equivalent word, and append that to a string (and a space)... 4) when you're done with the array, output the resulting string to the 'output' box.
SQLite is pretty easy to work with, you can just start with a big excel file, then with some sqlite manager (I use a firefox plugin), import that into a .sqlite file, and add that to your project. Have you thought about how you would handle 'word not found'?
This is untested code, just to give you something to start with:
-(void) viewWillAppear:(BOOL)animated{
[self createEditableCopyOfDatabaseIfNeeded];
}
-(sqlite3 *) getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened");
} else {
NSLog(@"Error in opening database");
}
return newDBconnection;
}
-(void)translate{
//take input and break into an array
NSString *clearText = [[NSString alloc] init];
clearText=inputBox.text;
NSArray *words = [[NSArray alloc] init];
words= [clearText componentsSeparatedByString:@" "];
numOfWords=words.count;
NSString *newText=@"";
//open database
sqlite3 *db = [self getNewDBConnection];
//loop through array
for(i=0;i<numOfWords;i++){
sqlite3_stmt *resultStatement = nil;
NSString *res = [NSString stringWithFormat:@"select * from dictionary where plain='%@'",[[words objectAtIndex:i] stringByTrimmingCharactersInSet:whitespaceCharacterSet]];
if((sqlite3_prepare_v2(db, [res UTF8String], -1, &resultStatement, nil))!=SQLITE_OK){
NSLog(@"Error getting result, maybe word not found\n");
NSLog(@"error: %s", sqlite3_errmsg(db));
}
else{
if(sqlite3_step(resultStatement)==SQLITE_ROW){
//in the line below, 1 is the column number of the replacement word
NSString *add = [[NSString alloc] initWithUTF8String: (char*)sqlite3_column_text(resultStatement,1)]
newText=[newText stringByAppendingString:add];
[add release];
}
}
sqlite3_finalize(resultStatement);
}
//output result
outputBox.text=newText;
sqlite3_close(db);
}
-(void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
//NSLog(@"Creating editable copy of database");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
Machine translation is an extremely complex problem, and cannot be solved simply by implementing a word-by-word dictionary translation. Wikipedia has a decent overview of the different approaches extant in the literature; note, however, that none of them are trivial to implement.
Machine translation
精彩评论