2011-07-28 14 views
9

मुझे मूल रूप से यह पता लगाने के लिए एक (त्वरित और गंदे) समाधान की आवश्यकता है कि कोई निश्चित एनएसएसटींग एक 'वास्तविक' शब्द है, यानी, यदि यह शब्दकोश में है। तो मूल रूप से, एक बहुत ही सरल वर्तनी परीक्षक। क्या किसी को ऐसा करने के किसी भी तरीके से पता है? असल में मुझे या तो अंग्रेजी शब्दकोश में सभी शब्दों वाले फ़ाइल की आवश्यकता होती है (जिसे मैंने खोजा है, लेकिन इसका कोई फायदा नहीं हुआ है), या iPhones वर्तनी जांच सेवा के साथ इंटरफ़ेस करने का एक तरीका है। बेशक मैं ओएसएक्स पर एनएसएसपील चेकर के समान तरीके से आईफ़ोन स्पेल चेक सेवा के साथ इंटरफ़ेस करना चाहता हूं, इसलिए मेरा ऐप अन्य भाषाओं के साथ काम करेगा, लेकिन इस बिंदु पर मैं जो प्राप्त कर सकता हूं वह ले जाऊंगा।आईफोन उद्देश्य-सी: एक 'असली' शब्द का पता लगाने

अन्त में, यहाँ बेहतर मेरी जरूरतों को वर्णन करने के कुछ छद्म कोड है:

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

क्यों न केवल एन एनएसएसपील चेकर का उपयोग करें? – Goz

+0

@ वुल्फगैंग: एनएसओब्जेक्ट एनएसएसटींग की तरह .... – brain

+0

@ वुल्फगैंग: एनएस प्रीफिक्स्ड क्लास आईएसओ पर उपलब्ध हैं, जैसे एनएसओब्जेक्ट, एनएसएनंबर, एनएसएसटींग, एनएसपी्रेडिकेट, एनएसएफटेड कंट्रोलर, ... – 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 को सटीक रूप से काम कर सकते हैं।

मैं दो-चरणीय प्रक्रिया का उपयोग करता हूं क्योंकि मुझे तेज़ होने के पहले चरण की आवश्यकता है (लेकिन सटीक नहीं)। आपको केवल चरण दो की आवश्यकता हो सकती है जो सटीक जांच है। ध्यान दें कि यह 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; 

} 
+0

मैंने आपके कोड स्पिनेट का पालन किया है, लेकिन "आपके, जागृत, सफेद, बुराई, प्रेमी" जैसे कुछ शब्द वास्तविक शब्द हैं लेकिन UITextchecker अमान्य शब्दों के रूप में दिखा रहा है। –