使用UITextChecker进行IOS自定义键盘建议

时间:2017-09-11 10:21:21

标签: ios objective-c custom-keyboard

到目前为止,我到达了这里。我有一个接收字符串的函数,并向我展示了我的自定义键盘的一些建议。

- (void)suggestionsForCustomKeyboard:(NSString *)word {

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);

}

最后两行是两种类型的print语句。一个用于猜测发生错误拼写的单词,另一个用于字典单词。 arrPredectiveText数组按字典顺序包含单词。

请给出一些想法或方法来解决ios自定义键盘的建议和下一个词预测机制,就像其他应用程序一样。任何帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分: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 ...块,您将在块完成中获得有关建议字符串数组的好建议。