iPhone Objective-C:检测“真实”的目标单词

发布于 2024-11-26 19:54:10 字数 430 浏览 1 评论 0原文

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

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

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

I need a (quick and dirty) solution to basically detect if a certain NSString is a 'real' word, that is, if it's in the dictionary. So basically, a very simplistic spell checker. Does anyone know of any way to do this? Basically I either need a file containing all words in the English dictionary (which I've searched for, but to no avail), or a way to interface with the iPhones spell checking service. Of course I would like to interface with the iPhones spell check service in a similar way to NSSpellChecker on OSX so my app will work with other languages, but at this point I'll take what I can get.

Lastly, here's some pseudo-code to better illustrate my needs:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-12-03 19:54:10

请改用 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;
}

Use UITextChecker instead. The code below might not be perfect but should give you a good idea.

-(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;
}
回心转意 2024-12-03 19:54:10

您可以使 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;

}

You can make UITextChecker work accurately without needing to add a new dictionary.

I use a two-step process because I need the first step to be fast (but not accurate). You may only need step two which is the accurate check. Note this makes use of the UITextChecker's completionsForPartialWordRange function which is why it's more accurate than the MisspelledWord function.

//Step one: I quickly check to see if a combination of letters passes the spell check. This is not that accurate but it's very fast so I can quickly exclude lots of letter combinations (brute force approach).

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

// Step Two: Extra check to make sure the word is really a real word. returns true if we have a real word.

-(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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文