Skip to content

Wavesonics/PlatformSpellCheckerKt

Repository files navigation

Platform Spell Checker Kt

CI Maven Central License

KMP badge-jvm badge-android badge-ios

This Kotlin Multiplatform library wraps the OS's native spell checker on each supported platform. It then provides a Kotlin friendly API.

The big advantage here is that nothing needs to be bundled with your program. Especially dictionaries. Most likely, what ever the user's language is, they will already have the proper dictionary on their device for the system level Spell Checker.

Note: Each platform can and will give different results for the same word. This library does not attempt to correct these results.

Dependency

implementation("com.darkrockstudios:platform-spellcheckerkt:1.0.0")

Usage

Create a Spell Checker

// Android's PlatformSpellCheckerFactory requires a context, thus you will need to construct it
// in platform-specific code and pass it down into common code.
val factory = PlatformSpellCheckerFactory(context)
val factory = PlatformSpellCheckerFactory()

// Use the user's current/system language
val checkerDefault = factory.createSpellChecker()

// Or request a specific locale (validated). Country is optional.
val checkerUkUa = factory.createSpellChecker(SpLocale.UK_UA)

// Check before you create! Creating a Spell Checker for a locale that isn't supported will throw an exception
if (factory.hasLanguage(SpLocale.EN_GB)) {
    val gbChecker = factory.createSpellChecker(SpLocale.EN_GB)
}

// You can also create a checker manually
val checker = PlatformSpellChecker(SpLocale.EN_US)

Use a Spell Checker

// Check a single word for spelling errors
val result = checker.checkWord("mispelledWord")
if (isMisspelled(result)) {
	result.suggestions.forEach { println(it) }
}

// Sentence level correct is also supported
val misspellings = checker.checkMultiword("This is a sentence with a mispelled word.")
misspellings.forEach { println("${it.misspelledWord} at ${it.startIndex} with length ${it.length}") }

Utilities

val factory = PlatformSpellCheckerFactory()

// Is a spell checker available on this platform at runtime?
val available = factory.isAvailable()

// What locale does the system currently use?
val systemLocale: SpLocale = factory.currentSystemLocale()

// A best-effort list of available Spell Checker locales on this device
val locales: List<SpLocale> = factory.availableLocales()

Supported Platforms

Platform Supported Implementation Details
Android Wraps TextServicesManager's SpellCheckerSession with full featured support.
iOS Wraps UITextChecker for full featured support.
JVM - Windows Wraps the Win32 API COM Object for ISpellCheckerFactory for full featured support.
JVM - Linux Linux has no native spell checker API, so this library looks to see if hunspell is installed, and uses it, otherwise it reports Spell Checking is not supported. Single word spell checking is a first class citizen, but sentence level correct is really just decomposing the sentence into individual single word checks, so is not as robust as other platforms.
JVM - macOS ⚠️ Wraps NSSpellChecker for partial support.
Known issues:
• There is an issue with JNA calling into certain platform APIs, so sentence level correct is hacked together right now.
• MacOS has a bit of a weird spell checker, the words it considers "correct" are not always what I would expect, but who am I to question the wisdom of Tim Apple.
K/N - Winx86 Support is certainly possible, but there aren't any examples I've found for binding COM objects in K/N, so would take some exploration.
WASM-JS Can't work I don't think because there is no standard browser spell checker API as far as I have found.

About

A Kotlin Multiplatform wrapper around different platform's native spell checker systems.

Resources

License

Stars

Watchers

Forks

Packages

No packages published