PredictionKeyboard is a high-performance, intelligent next-word prediction framework for iOS custom keyboards. Built on Realm for fast, persistent prediction scoring, it provides context-aware word suggestions and completions to enhance typing experiences in custom keyboard extensions.
- ๐ Fast Prediction Engine: Powered by Realm database for instant lookups and scoring
- ๐ง Context-Aware Suggestions: N-gram based predictions (1-gram to 3-gram) for intelligent next-word suggestions
- โ๏ธ Word Completion: Real-time autocorrect and word completion as users type
- ๐ฆ Pre-trained Database: Includes a 600MB+ prediction database with millions of word sequences
- ๐ฏ High Accuracy: Scored predictions based on real-world language patterns
- โก๏ธ Optimized Performance: Concurrent prediction queue for non-blocking UI
- ๐ Privacy-First: All predictions run locally on-device, no network requests
- ๐ฑ iOS 13+: Compatible with modern iOS versions and keyboard extensions
- ๐ง Easy Integration: Simple API for custom keyboard implementations
- iOS 13.0+
- Xcode 12.0+
- Swift 5.0+ or Objective-C
- CocoaPods 1.10+
Add the following line to your Podfile:
pod 'PredictionKeyboard'Then run:
pod installSwift:
import PredictionKeyboardObjective-C:
#import <PredictionKeyboard/PredictionKeyboard.h>Swift:
class KeyboardViewController: UIInputViewController {
let predictor = PredictionKeyboardManager()
override func viewDidLoad() {
super.viewDidLoad()
// Initialize the prediction database (runs once on first launch)
predictor.initializePredictionDatabase { success, error in
if success {
print("โ
Prediction database ready")
} else {
print("โ Database initialization failed: \(error?.localizedDescription ?? "Unknown error")")
}
}
}
}Objective-C:
@interface KeyboardViewController ()
@property (nonatomic, strong) PredictionKeyboardManager *predictor;
@end
@implementation KeyboardViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.predictor = [[PredictionKeyboardManager alloc] init];
// Initialize the prediction database
[self.predictor initializePredictionDatabase:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"โ
Prediction database ready");
} else {
NSLog(@"โ Database initialization failed: %@", error.localizedDescription);
}
}];
}
@endSwift:
func textDidChange() {
let currentText = textDocumentProxy.documentContextBeforeInput ?? ""
predictor.getPrediction(currentText) { suggestions, textColor in
// Update your suggestion bar with the predictions
self.updateSuggestionBar(suggestions: suggestions, color: textColor)
}
}Objective-C:
- (void)textDidChange:(id<UITextInput>)textInput {
NSString *currentText = self.textDocumentProxy.documentContextBeforeInput ?: @"";
[self.predictor getPrediction:currentText completion:^(NSArray<NSString *> *suggestions, UIColor *textColor) {
// Update your suggestion bar with the predictions
[self updateSuggestionBarWithSuggestions:suggestions color:textColor];
}];
}To share data between your main app and keyboard extension, use App Groups:
Swift:
let predictor = PredictionKeyboardManager(appGroup: "group.com.yourcompany.yourapp")Objective-C:
PredictionKeyboardManager *predictor = [[PredictionKeyboardManager alloc] initWithAppGroup:@"group.com.yourcompany.yourapp"];The framework uses configurable constants defined in PredictionConstants.h:
// Maximum number of suggestions to return
#define MAX_SUGGESTIONS 3
// Minimum score threshold for predictions
#define MIN_PREDICTION_SCORE 100
#define MIN_SCORE_FOR_NGRAM 5
// Realm database configuration
#define REALM_SCHEMA_VERSION 1
#define REALM_DB_NAME @"predictiondb.realm"PredictionKeyboard uses a two-phase prediction strategy:
- Analyzes the last 1-3 words for context
- Queries n-gram database (trigrams โ bigrams โ unigrams)
- Returns scored predictions based on language patterns
- Suggestions appear in blue to indicate next-word mode
- Extracts the current incomplete word
- Performs prefix matching against word database
- Returns autocomplete suggestions sorted by frequency
- Suggestions appear in black to indicate completion mode
The prediction database includes two main tables:
PredictionTable: N-gram sequences (e.g., "the", "the quick", "the quick brown") with prediction scoresPredictionWord: Individual words with frequency scores for completion
- Database Size: ~600MB (millions of word sequences)
- First Launch: Database extraction takes ~5-10 seconds (one-time operation)
- Prediction Speed: <10ms average per query (measured on iPhone 12)
- Memory Footprint: ~50MB RAM during active use
The framework includes comprehensive unit tests:
# Run tests
xcodebuild test -scheme PredictionKeyboard -destination 'platform=iOS Simulator,name=iPhone 15'Test coverage includes:
- Database extraction and caching
- Next-word prediction performance
- Word completion accuracy
- Concurrent prediction requests
- Edge cases and error handling
// Initialize with default app storage
- (instancetype)init;
// Initialize with App Group for keyboard extension
- (instancetype)initWithAppGroup:(NSString *)appGroupID;// Initialize the prediction database (call once on first launch)
- (void)initializePredictionDatabase:(nullable void(^)(BOOL success, NSError *_Nullable error))completion;
// Get predictions for the current text
- (void)getPrediction:(NSString *)syntax
completion:(void(^)(NSArray<NSString *> *suggestions, UIColor *textColor))completion;Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Clone the repository
git clone https://github.com/carloskekwa/Custom-Keyboard-Prediction.git
cd Custom-Keyboard-Prediction
# Install dependencies
pod install
# Open the workspace
open PredictionKeyboard.xcworkspacePredictionKeyboard is available under the MIT license. See the LICENSE file for more info.
Carlos Kekwa
- Email: [email protected]
- GitHub: @carloskekwa
- Built with Realm for high-performance local storage
- Inspired by modern keyboard prediction systems
- Prediction database trained on public domain text corpora
Check out the Example/ directory for a complete implementation of a custom keyboard using PredictionKeyboard.
A: The bundled database is approximately 600MB and includes millions of word sequences for accurate predictions.
A: No! All predictions run completely on-device. Your typing data never leaves the device.
A: Simply update the CocoaPod to get the latest database. You can also train your own database using Realm.
A: Yes! The framework is compatible with both UIKit and SwiftUI keyboard implementations.
A: Currently optimized for English. Multi-language support is planned for future releases.
- Multi-language support (Spanish, French, German, etc.)
- User dictionary learning and personalization
- Emoji predictions
- Cloud sync for prediction history
- Reduced database size with compression
- SwiftUI example implementation
- ๐ Prediction Accuracy: ~85% in real-world typing scenarios
- โก๏ธ Average Response Time: <10ms
- ๐พ Memory Usage: ~50MB during active use
- ๐ฆ Framework Size: ~3MB (excluding database)
Made with โค๏ธ for the iOS developer community