/WordSuggestion

A simple Swift implementation of word suggestion using n-gram probabilistic model

Primary LanguageSwiftMIT LicenseMIT

WordSuggestion

Version License Platform

Swift implementation for N-Gram word prediction. Modified from the Objective-C version WordSuggestion. Replace CoreData database store with Realm.

Usage

Setup

    // Setup realm
    let realm: Realm = {
        var config = Realm.Configuration()
        let realmName = "WordPredictor_default"
        config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(realmName).realm")
        config.objectTypes = [NGram1.self, NGram2.self, NGram3.self, NGram4.self]
        try? FileManager.default.createDirectory(at: config.fileURL!.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)

        return try! Realm(configuration: config)
    }()
    
    // Only needs load at the first time app launch
    if wordPredictor.needLoadNgramData {
        wordPredictor.load { error in
            print(error)
        }
    }

Predict

    func textViewDidChange(_ textView: UITextView) {
        wordPredictor.suggestWords(for: textView.text) { suggestions in
            // …            
        }
    }

For more detail please check the Example app demo.

Install

pod 'WordSuggestion'

Demo

Demo

License

MIT License