2011-07-28 2 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:NSObject、NSNumber、NSString、NSPredicate、NSFetchedControllerなどのNSプレフィックスクラスが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

私はあなたのコードスピネッツに従っていますが、 "あなたの声、白、悪、恋人"のような単語のいくつかは現実の単語ですが、UITextcheckerは無効な単語として表示しています。 –

関連する問題