2017-09-11 82 views
0

在這裏,我到目前爲止。我有一個函數接收一個字符串並向我顯示一些關於我的自定義鍵盤的建議。iOS自定義鍵盤建議與UITextChecker

- (空)suggestionsForCustomKeyboard:(的NSString *)字{

NSArray *arrPredectiveText; 
UITextChecker *checker = [[UITextChecker alloc] init]; 
NSLocale *currentLocale = [NSLocale currentLocale]; 
NSString *currentLanguage = [currentLocale objectForKey:NSLocaleLanguageCode]; 
NSRange searchRange = NSMakeRange(0, word.length); 
arrPredectiveText = [checker completionsForPartialWordRange:searchRange inString:word language:currentLanguage]; 

NSArray *guesses = [NSArray array]; 

if ([currentString length] >= 1) { 

    UITextChecker *textChecker = [[UITextChecker alloc] init]; 
    NSRange misspelledRange = [textChecker 
           rangeOfMisspelledWordInString:currentString 
           range:NSMakeRange(0, [currentString length]) 
           startingAt:0 
           wrap:NO 
           language:@"en_US"]; 


    if (misspelledRange.location != NSNotFound) { 

     guesses = [textChecker guessesForWordRange:misspelledRange 
              inString:currentString 
              language:@"en_US"]; 
     NSLog(@"First guess: %@", [guesses firstObject]); 

    } else { 
     NSLog(@"Textchecker Not found"); 
     autocorrectedText = @""; 
    } 

    if (arrPredectiveText.count >= 2){ 
     suggestionOne = [arrPredectiveText objectAtIndex:0]; 
     suggestionTwo = [arrPredectiveText objectAtIndex:1]; 
    } 
    else if (arrPredectiveText.count == 1 && guesses.count >= 1) { 
     suggestionOne = [arrPredectiveText firstObject]; 
     suggestionTwo = [guesses firstObject]; 
    }else if (arrPredectiveText.count == 0 && guesses.count > 0){ 
     suggestionOne = [guesses firstObject]; 
     if (guesses.count > 1) { 
      suggestionTwo = [guesses objectAtIndex:1]; 
     } 
    } 

    NSLog(@"Textchecker all guess: %@", guesses); 
    NSLog(@"Prediction: %@",arrPredectiveText); 

}

最後兩行是兩種類型的打印語句。一個是拼寫錯誤發生時的猜測詞,另一個是詞典詞。 arrPredectiveText數組按字典順序包含單詞。

請給出一些想法或方法來解決像其他應用程序的ios自定義鍵盤的建議和下一個詞預測機制。任何幫助或建議將不勝感激。

回答

0

回答我自己的問題,我發現了一些更好的建議方式。這可能有助於其他人。

//建議改進

+ (void)getSuggestionsFor:(NSString *)word WithCompletion:(void(^)(NSArray *))completion 
{ 
    // NSLog(@"Current String %@",word); 
    NSString *prefix; 
    if ([word length] > 1) { 
     prefix = [word substringToIndex:word.length - 1]; 
    }else{ 
     prefix = [word substringToIndex:word.length]; 
    } 
    // Won't get suggestions for correct words, so we are scrambling the words 
    // NSLog(@"Prefix String %@",prefix); 
    NSString *scrambledWord = [NSString stringWithFormat:@"%@%@",word, [self getRandomCharAsNSString]]; 
    // NSLog(@"scrambledWord String %@",scrambledWord); 
    UITextChecker *checker = [[UITextChecker alloc] init]; 
    NSRange checkRange = NSMakeRange(0, scrambledWord.length); 
    NSRange misspelledRange = [checker rangeOfMisspelledWordInString:scrambledWord range:checkRange startingAt:checkRange.location wrap:YES language:@"en_US"]; 

    NSArray *arrGuessed = [checker guessesForWordRange:misspelledRange inString:scrambledWord language:@"en_US"]; 
    // NSLog(@"Guess Arr ===== %@",arrGuessed); 
    // Filter the result based on the word 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",word]; 
    NSArray *arrayfiltered = [arrGuessed filteredArrayUsingPredicate:predicate]; 
    // NSLog(@"1 arrayfiltered character %@",arrayfiltered); 
    if(arrayfiltered.count == 0) 
    { 
     // Filter the result based on the prefix 
     NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",prefix]; 
     arrayfiltered = [arrGuessed filteredArrayUsingPredicate:newPredicate]; 
     //  NSLog(@"2 arrayfiltered character %@",arrayfiltered); 
    } 
    if (arrayfiltered.count == 0) { 

     NSArray *arrPredectiveText; 
     UITextChecker *checker = [[UITextChecker alloc] init]; 
     NSLocale *currentLocale = [NSLocale currentLocale]; 
     NSString *currentLanguage = [currentLocale objectForKey:NSLocaleLanguageCode]; 
     NSRange searchRange = NSMakeRange(0, word.length); 
     arrPredectiveText = [checker completionsForPartialWordRange:searchRange inString:word language:currentLanguage]; 
     //  NSLog(@"3 arrayfiltered character %@",arrPredectiveText); 

     if (arrPredectiveText.count == 0 && arrGuessed.count != 0) { 
      completion(arrGuessed); 
     }else{ 
      completion(arrPredectiveText); 
     } 
    }else{ 
     completion(arrayfiltered); 
    } 
} 

+ (NSString *)getRandomCharAsNSString{ 
    NSString *str = [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a']; 
    // NSLog(@"Random character %@",str); 
    return str; 
} 

通過調用這個getSuggestionsFor ...塊與輸入字符串你會得到很好的建議,分組完成的建議字符串數組。