2011-07-28 5 views
9

특정 NSString이 '실제'단어인지, 즉 사전에 있는지를 기본적으로 감지하는 데는 (빠르고 더러운) 솔루션이 필요합니다. 그래서 기본적으로 매우 단순한 맞춤법 검사기입니다. 누구든지이 일을 할 수있는 방법을 알고 있습니까? 기본적으로 영어 사전에있는 모든 단어가 들어있는 파일이 필요합니다 (검색 한 적이 있지만 아무 쓸모가 없습니다). 또는 iPhone 맞춤법 검사 서비스와 연결하는 방법이 필요합니다. 물론 OSX의 NSSpellChecker와 유사한 방식으로 iPhone 맞춤법 검사 서비스와 인터페이스하고 싶습니다. 그래서 내 앱이 다른 언어에서도 작동 할 수 있지만, 지금은 내가 얻을 수있는 것을 취할 것입니다.iPhone objective-c : '실제'단어 검색

마지막으로, 여기에 더 나은 내 요구를 설명하기 위해 일부 의사 코드입니다 : 대신

-(BOOL)isDictionaryWord:(NSString*)word; //returns TRUE when [email protected]"greetings". returns FALSE when [email protected]"slkfjsdkl"; 
+0

n NSSpellChecker를 사용하지 않는 이유는 무엇입니까? – Goz

+0

@Wolfgang : NSObject NSString과 같습니다. – brain

+0

@Wolfgang : NS 접두어를 사용하는 클래스는 NSObject, NSNumber, NSString, NSPredicate, NSFetchedController 등과 같이 iOS에서 사용할 수 있습니다. – rckoenes

답변

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

새 사전을 추가하지 않고도 UITextChecker가 정확하게 작동하도록 만들 수 있습니다.

빠른 (정확하지는 않지만) 첫 번째 단계가 필요하기 때문에 2 단계 프로세스를 사용합니다. 정확한 수표 인 2 단계 만 필요할 수 있습니다. 이것은 UITextChecker의 completionsForPartialWordRange 함수를 사용하므로 MisspelledWord 함수보다 정확합니다.

// 1 단계 : 문자 조합이 맞춤법 검사를 통과하는지 신속하게 확인합니다. 이것은 정확하지 않지만 속도가 매우 빠르므로 많은 문자 조합을 신속하게 제외 할 수 있습니다 (무차별 대입 방식).

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 

// 2 단계 : 단어가 실제 단어인지 확인하기위한 추가 검사. 실제 단어가 있으면 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; 

} 
+0

나는 코드 스핀 츠를 따라 갔지만 "thy, arouse, white, evil, lover"같은 단어는 실제 단어이지만 UITextchecker는 유효하지 않은 단어로 표시됩니다. –