2011-05-30 97 views
0

我试图做一个应用程序,基本上可以从一个框,由用户输入的文本,并将其转换成相当于该文本到另一个文本框。 当我说相同的时候,我的意思是另一种措辞的措辞,这些将是我自己的话,并没有在某个其他数据库已经完成某处。iPhone SDK:如何去翻译风格的应用程序

我可以在一个小规模上做这个事情,就像所有给定可能性的switch语句一样,但是对于整个语言来说这具有实用性问题。

我怎么会去这样做呢?我会在SQLite数据库中数据库中的所有单词和它们的等价物吗?我将如何将其整合到应用程序中?当涉及到编程时,我的知识非常有限,包括整个数据库的值,我只是习惯于列出单独的值,并没有真正意识到能够集成很多值的短代码。 是否有可能将SQL数据库的所有值集成到一个Switch或if语句中,它可以将用户的文本条目分离为它所包含的单独词汇,并显示它们在数据库中列出的等价物?

感谢

回答

4

从你的“这是我自己的话,而不是可在一个已经被地方做了一些其他的数据库”的评论,是不是正确的假设你的字对字的替换是一种有效的方法你想达到什么目的? (?没有单独的语言或先例,什么是正确的)

如果是这样,我想你会做这样的事情:

1)从输入字段走串, 2)适用componentsSeparatedByString:给它把它分解成一个数组,然后, 3)取数组中的每一项,搜索你的sqlite数据库中的数据,返回相应的单词,并将其附加到一个字符串(和一个空格)中...... 4)当你'用数组完成,输出结果字符串到'输出'框。你可以从一个大的excel文件开始,然后用一些sqlite管理器(我使用firefox插件),将它导入到一个.sqlite文件中,然后将它添加到你的项目中。你有没有想过如何处理'找不到'的单词?

这是未经测试的代码,只是为了给你的东西入手:

-(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 *[email protected]""; 
     //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]); 
    } 
} 
+0

好的,好吧,我一直试图实现这个代码一个小时左右,并在事情的日志方面,我不断得到“错误获得结果,也许没有找到字”的消息,我只能认为我的问题与我所做的实际数据库文件一样,我可以在这里排除故障的是什么? – Sam 2011-05-31 12:40:00

+0

是否将'dictionary'更改为您在sqlite文件中使用的实际表名?和'普通'到您使用的列的正确名称? – 2011-05-31 13:38:12

+0

是的,它仍然返回错误信息... – Sam 2011-05-31 14:15:57

2

机器翻译是一个极其复杂的问题,不能将通过实施字的字词典的翻译根本解决。维基百科对文献中现存的不同方法有一个体面的概述;但是请注意,它们都不是微不足道的。

Machine translation

+0

是的,也许实际的语言,但我的应用程序,这是合适的,因为它更像措辞不同的方式相同字。 – Sam 2011-05-31 08:33:00