我如何检查一个词是否在字典里?

时间:2022-11-12 21:22:00

I'm loading strings from a preloaded core data database which happen to be all CAPITAL.

我正在从一个预加载的core data数据库中加载字符串,而这个数据库恰好是所有的资本。

I want to check if each word is in the English dictionary, and if so try to lowercase it like a regular sentence, if not keep it upper case (for abbreviations and such).

我想检查每个单词是否在英语字典中,如果是,试着用小写来表示,就像一个普通的句子一样,如果不是大写(缩写等)。

Is there a way to do this with iOS 4?

iOS 4有办法做到这一点吗?

2 个解决方案

#1


6  

Implementation for Noah's answer:

实现对诺亚的回答:

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

NSString string = @"lalala";
if ([self isDictionaryWord:string]) string = [string lowercaseString];

#2


0  

As of iOS 3.2, you can use the UITextChecker class to access the system dictionary. Use its -rangeOfMisspelledWordInString:range:startingAt:wrap:language: method: if it returns a range whose location is NSNotFound, all of the words in the string you provided are in the dictionary or have already been “learned” by the text checker.

从iOS 3.2开始,您可以使用UITextChecker类来访问系统字典。使用它的-rangeOfMisspelledWordInString:range:startingAt:wrap:language: method:如果返回一个位置为NSNotFound的范围,则提供的字符串中的所有单词都在字典中,或者已经被文本检查器“学会”。

#1


6  

Implementation for Noah's answer:

实现对诺亚的回答:

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

NSString string = @"lalala";
if ([self isDictionaryWord:string]) string = [string lowercaseString];

#2


0  

As of iOS 3.2, you can use the UITextChecker class to access the system dictionary. Use its -rangeOfMisspelledWordInString:range:startingAt:wrap:language: method: if it returns a range whose location is NSNotFound, all of the words in the string you provided are in the dictionary or have already been “learned” by the text checker.

从iOS 3.2开始,您可以使用UITextChecker类来访问系统字典。使用它的-rangeOfMisspelledWordInString:range:startingAt:wrap:language: method:如果返回一个位置为NSNotFound的范围,则提供的字符串中的所有单词都在字典中,或者已经被文本检查器“学会”。