Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created June 18, 2014 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/8a7618288c9f37db459c to your computer and use it in GitHub Desktop.
Save dongyuwei/8a7618288c9f37db459c to your computer and use it in GitHub Desktop.
test NSSpellChecker, show suggestion list for an uncorrect word.
#import <AppKit/NSSpellChecker.h> //import AppKit framework
void testSpellCheck(){
NSSpellChecker* checker = [NSSpellChecker sharedSpellChecker];
NSString * string = @"helo";
NSRange range = NSMakeRange(0, [string length]);
NSArray *corrected = [checker guessesForWordRange:range inString:string language:@"en" inSpellDocumentWithTag:0];
NSLog(@"NSSpellChecker suggestion:%@",corrected);
}
@dongyuwei
Copy link
Author

void testCleanupGoogleWords(){
    NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath: @"/Users/ilfe/code/NumberInput_IMKit_Sample/object-c/hallelujahIM/dictionary/google_333333_words.json"];
    [inputStream  open];
    NSMutableDictionary* wordsWithFrequency  = [[NSJSONSerialization JSONObjectWithStream:inputStream
                                                            options:nil
                                                              error:nil] mutableCopy];
    [inputStream close];

    NSArray* words = [wordsWithFrequency allKeys];
    NSSpellChecker* checker = [NSSpellChecker sharedSpellChecker];
    for(NSString* word in words){
        NSRange range = NSMakeRange(0, [word length]);
        NSString *corrected = [checker correctionForWordRange:range inString:word language:@"en" inSpellDocumentWithTag:0];
        if(corrected){
            [wordsWithFrequency removeObjectForKey:word];
        }
       [word release];
       [corrected release];
    }

    NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:@"/tmp/correct.json"
                                                                     append:NO];
    [outputStream open];
    [NSJSONSerialization writeJSONObject:wordsWithFrequency
                                                      toStream:outputStream
                                                       options:nil
                                                         error:nil];
    [outputStream close];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment