Skip to content

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

License

Notifications You must be signed in to change notification settings

DimensionDev/WordSuggestion

 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 81.2%
  • Objective-C 14.8%
  • Ruby 4.0%