Using Uilexicon to Implement Autocorrect in iOS 8 Keyboard Extension

Want to implement prediction and autocorrection in ios 8 custom keyboard application

Apple doesn't provide an API for their QuickType keyboard, autocorrect and word prediction services. If you want, you need to implement them yourself.

How to use autocorrection and shortcut list in iOS8 custom keyboard?

Implementing the lexicon would look pretty much like this:

  1. Use requestSupplementaryLexiconWithCompletion() to get the lexicon upon launch once.
  2. Each type text is inputted add it to a NSString (tracking the current word)
  3. When user presses space (end of curent word) check the string against the lexicon
  4. If it's a match count the number of characters and delete that number of characters
  5. Input the suggestion suggested by the lexicon
  6. Clear the string and start again

Additionally you could also use UITextChecker to offer more advanced auto-correct features.

Code (in Objective-C, this may not be 100% accurate I wrote in SO while on the bus but it should do):

UILexicon *lexicon;
NSString *currentString;

-(void)viewDidLoad {
[self requestSupplementaryLexiconWithCompletion:^(UILexicon *receivedLexicon) {
self.lexicon = receivedLexicon;
}];
}

-(IBAction)myTypingAction:(UIButton *)sender {
[documentProxy insertText:sender.title];
[currentString stringByAppendingString:sender.title];
}

-(IBAction)space {
[documentProxy insertText:@" "];
for (UILexiconEntry *lexiconEntry in lexicon.entries) {
if (lexiconEntry.userInput isEqualToString:currentString) {
for (int i = 0; currentString.length >=i ; i++) {
[documentProxy deleteTextBackwards];
}
[documentProxy insertText:lexiconEntry.documentText];
currentString = @"";
}
}
}

Feel free to comment if you have any more questions.

Source: Personal experience with iOS 8 keyboards and UILexicon

How to use auto suggestion view for custom key-board extension ios8?

Finlay i done it..! i put suggestion using sqlite static database in and get first three suggested work using like query like following code:

NSString *precedingContext = self.textDocumentProxy.documentContextBeforeInput; //here i get enter word string.

__block NSString *lastWord = nil;

[precedingContext enumerateSubstringsInRange:NSMakeRange(0, [precedingContext length]) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
lastWord = substring;
*stop = YES;
}];
NSLog(@"==%@",lastWord); // here i get last word from full of enterd string

NSString *str_query = [NSString stringWithFormat:@"select * from suggestion where value LIKE '%@%%' limit 3",lastWord];
NSMutableArray *suggestion = [[DataManager initDB] RETRIVE_Playlist:str_query];

NSLog(@"arry %@",suggestion); i get value in to array using like query
if(suggestion.count>0)
{

if(suggestion.count==1)
{
[self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal];

}
else if(suggestion.count==2)
{
[self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal];
[self.ObjKeyLayout.secondButton setTitle:[suggestion objectAtIndex:1] forState:UIControlStateNormal];
}
else
{

[self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal];
[self.ObjKeyLayout.secondButton setTitle:[suggestion objectAtIndex:1] forState:UIControlStateNormal];
[self.ObjKeyLayout.thirdButton setTitle:[suggestion objectAtIndex:2] forState:UIControlStateNormal];
}

}
else
{

[self.ObjKeyLayout.FirstButton setTitle:@"" forState:UIControlStateNormal];
[self.ObjKeyLayout.secondButton setTitle:@"" forState:UIControlStateNormal];
[self.ObjKeyLayout.thirdButton setTitle:@"" forState:UIControlStateNormal];
}

and i got it my keyboard output is:

Sample Image

Autocorrection and Suggestion for custom keyboard

I use four different systems for my keyboard:

  1. I have a list of the top 30,000 or so words, ranked in order of the most used to the least used. You can pay for lists, I just got a free one of about 42,000 and edited it down a lot.

  2. guessesForWordRange is provided by Apple. It will guess words that are close to what you have typed. It does a fairly good job, but I had to filter out some things. The top guess sometimes has quotation marks around it, but other then that it works great.

  3. completionsForPartialWordRange is also provided by Apple. It will return completed words, but in alphabetical order, not ranked by usage. Not much good on it's own, but is a great supplement to 1 and 2. (if this worked correctly #1 wouldn't be needed)

  4. Special cases. Mainly for contractions. When someone types didnt, I wanted it to auto choose didn't. So I have almost all contractions specifically programmed in.

So my word suggestions and autocorrection aren't perfect, but it does a decent job.

Hope this helps.

iOS Custom Keyboard, Suggestions from txt file

You have to implement your own autocorrection system. UILexicon will only give you shortcuts the user has set up, words that they have added to the iOS Dictionary, and names of contacts. It has no awareness of any words that you yourself provide, whether in a txt file or in any other form.

If you want to use the TOMSSuggestionBar, it appears from the sample code that the onus is on you to convert your txt file into a core data model, and indicate to the suggestion bar how it is to interpret the contents of that model. You may also want to implement the data source protocol to get more fine grained control over the suggestions.

Autocorrection and next word prediction are not solved problems; I suggest you do your own research and find the solution that is best suited to your goals.

Add QuickType (Predictive) to custom keyboard in iOS 8

No. There is no way to do this as of iOS8 BETA 5. Not sure why this got down votes and close votes...



Related Topics



Leave a reply



Submit