Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone objective-c: detecting a 'real' word

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";
like image 774
Chris Avatar asked Jul 28 '11 15:07

Chris


1 Answers

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;
}
like image 80
brain Avatar answered Oct 28 '22 00:10

brain