iPhone objective-c:检测'真实'字样

时间:2011-07-28 15:01:48

标签: iphone objective-c dictionary word spell-checking

我需要一个(快速和肮脏)解决方案来基本检测某个NSString是否是一个“真实”单词,也就是说,如果它在字典中。所以基本上,一个非常简单的拼写检查器。有谁知道有任何方法可以做到这一点?基本上我要么需要一个包含英语词典中所有单词的文件(我搜索过但无效),或者是一种与iPhone拼写检查服务接口的方法。当然我想以与OSX上的NSSpellChecker类似的方式与iPhone进行拼接拼写检查服务,这样我的应用程序就能与其他语言一起使用,但此时我会采取我能得到的。

最后,这里有一些伪代码可以更好地说明我的需求:

-(BOOL)isDictionaryWord:(NSString*)word;  //returns TRUE when word=@"greetings". returns FALSE when word=@"slkfjsdkl";

2 个答案:

答案 0 :(得分:21)

请改用UITextChecker。下面的代码可能不完美,但应该给你一个好主意。

-(BOOL)isDictionaryWord:(NSString*)word {
    UITextChecker *checker = [[UITextChecker alloc] init];
    NSLocale *currentLocale = [NSLocale currentLocale];
    NSString *currentLanguage = [currentLocale objectForKey:NSLocaleLanguageCode];
    NSRange searchRange = NSMakeRange(0, [word length]);

    NSRange misspelledRange = [checker rangeOfMisspelledWordInString:word range:searchRange startingAt:0 wrap:NO language:currentLanguage];
    return misspelledRange.location == NSNotFound;
}

答案 1 :(得分:3)

您可以使UITextChecker准确工作,而无需添加新词典。

我使用两个步骤,因为我需要第一步快速(但不准确)。您可能只需要第二步,即准确检查。请注意,这使用了UITextChecker的completionsForPartialWordRange函数,这就是为什么它比MisspelledWord函数更准确。

//第一步:我快速查看一下字母组合是否通过了拼写检查。这不是那么准确,但它非常快,所以我可以快速排除许多字母组合(蛮力方法)。

UITextChecker *checker;
NSString *wordToCheck = @"whatever"; // The combination of letters you wish to check

// Set the range to the length of the word
NSRange range = NSMakeRange(0, wordToCheck.length - 1);

NSRange misspelledRange = [checker rangeOfMisspelledWordInString:wordToCheck range: range startingAt:0 wrap:NO language: @"en_US"];
BOOL isRealWord = misspelledRange.location == NSNotFound;

// Call step two, to confirm that this is a real word
if (isRealWord) {
    isRealWord = [self isRealWordOK:wordToCheck];
}
return isRealWord; // if true then we found a real word, if not move to next combination of letters

//第二步:额外检查以确保单词真的是一个真正的单词。如果我们有一个真实的单词,则返回true。

-(BOOL)isRealWordOK:(NSString *)wordToCheck {

    // we dont want to use any words that the lexicon has learned.
    if ([UITextChecker hasLearnedWord:wordToCheck]) {
        return NO;
    }

    // now we are going to use the word completion function to see if this word really exists, by removing the final letter and then asking auto complete to complete the word, then look through all the results and if its not found then its not a real word. Note the auto complete is very acurate unlike the spell checker.
    NSRange range = NSMakeRange(0, wordToCheck.length - 1);
    NSArray *guesses = [checker completionsForPartialWordRange:range inString:wordToCheck language:@"en_US"];

    // confirm that the word is found in the auto-complete list
    for (NSString *guess in guesses) {

        if ([guess isEqualToString:wordToCheck]) {
            // we found the word in the auto complete list so it's real :-)
            return YES;
        }
    }

    // if we get to here then it's not a real word :-(
    NSLog(@"Word not found in second dictionary check:%@",wordToCheck);
    return NO;

}