आप एक नया शब्दकोश जोड़ने की आवश्यकता के बिना UITextChecker को सटीक रूप से काम कर सकते हैं।
मैं दो-चरणीय प्रक्रिया का उपयोग करता हूं क्योंकि मुझे तेज़ होने के पहले चरण की आवश्यकता है (लेकिन सटीक नहीं)। आपको केवल चरण दो की आवश्यकता हो सकती है जो सटीक जांच है। ध्यान दें कि यह UITextChecker के पूर्ण होने के लिए ForPartialWordRange फ़ंक्शन का उपयोग करता है, यही कारण है कि यह MisspelledWord फ़ंक्शन से अधिक सटीक है।
// चरण एक: मैं जल्दी से यह देखने के लिए जांच करता हूं कि पत्रों का संयोजन वर्तनी जांच पास करता है या नहीं। यह सटीक नहीं है लेकिन यह बहुत तेज़ है इसलिए मैं जल्दी से बहुत से अक्षर संयोजन (ब्रूट फोर्स दृष्टिकोण) को बाहर कर सकता हूं।
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
// चरण दो: यह सुनिश्चित करने के लिए अतिरिक्त जांच वास्तव में एक वास्तविक शब्द है। अगर हमारे पास असली शब्द है तो सच हो जाता है।
-(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;
}
क्यों न केवल एन एनएसएसपील चेकर का उपयोग करें? – Goz
@ वुल्फगैंग: एनएसओब्जेक्ट एनएसएसटींग की तरह .... – brain
@ वुल्फगैंग: एनएस प्रीफिक्स्ड क्लास आईएसओ पर उपलब्ध हैं, जैसे एनएसओब्जेक्ट, एनएसएनंबर, एनएसएसटींग, एनएसपी्रेडिकेट, एनएसएफटेड कंट्रोलर, ... – rckoenes