1

I'm building custom keyboard extension and want to implement autocompletion, like Apple does. As I see method completionsForPartialWordRangereturns list of words sorted alphabetically. How can I get results sorted by usage?

enter image description here

Community
  • 1
  • 1
landonandrey
  • 1,271
  • 1
  • 16
  • 26

2 Answers2

2

The docs for completionsForPartialWordRange:inString:language: say:

The strings in the array are in the order they should be presented to the user—that is, more probable completions come first in the array.

However, the results are very clearly sorted in alphabetical order, and it's not true that "more probable completions come first in the array." The below was tested with iOS 9:

NSString *partialWord = @"th";
UITextChecker *textChecker = [[UITextChecker alloc] init];
NSArray *completions = [textChecker completionsForPartialWordRange:NSMakeRange(0, partialWord.length) inString:partialWord language:@"en"];

iOS word completions for "th":

thalami,
thalamic,
thalamus,
thalassic,
thalidomide,
thallium,
...
the,
...

So, the results will need to be sorted again after obtaining the word completions.

The OS X NSSpellChecker version of this method does not have the same problem:

NSString *partialWord = @"th";

NSArray *completions = [[NSSpellChecker sharedSpellChecker] completionsForPartialWordRange:NSMakeRange(0, partialWord.length) inString:partialWord language:@"en" inSpellDocumentWithTag:0];

List of complete words from the spell checker dictionary in the order they should be presented to the user.

Mac OS X word completions for "th":

the,
this,
that,
they,
thanks,
there,
that's,
...

Filing a radar bug report would be a good idea, so that the behavior will hopefully be fixed in a later version of iOS. I've reported this as rdar://24226582 if you'd like to duplicate.

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • i tried to do advanced search for the rdar 24226582 on apple bug reporter but it says no results found :( am i doing something wrong? – sudoExclaimationExclaimation Feb 02 '16 at 00:52
  • @PranoyC Apple rdars are private and unsearchable, so you won't be able to find it, but you should make your own and say "duplicate of rdar://24226582" somewhere in the description. Then copy/paste it to [OpenRadar](https://openradar.appspot.com/) (as I should have done). – pkamb Feb 02 '16 at 00:54
1

Swift 4.0

 func autoSuggest(_ word: String) -> [String]? {
    let textChecker = UITextChecker()
    let availableLangueages = UITextChecker.availableLanguages
    let preferredLanguage = (availableLangueages.count > 0 ? availableLangueages[0] : "en-US");

    let completions = textChecker.completions(forPartialWordRange: NSRange(0..<word.utf8.count), in: word, language: preferredLanguage)

    return completions
   }
Yogendra Singh
  • 2,063
  • 25
  • 20