NLLanguageRecognizer

New in iOS 12 and macOS 10.14, the Natural Language framework refines existing linguistic APIs and exposes new functionality to developers.

NLTagger is NSLinguisticTagger with a new attitude. NLTokenizer is a replacement for enumerateSubstrings(in:options:using:) (neé CFStringTokenizer). NLLanguageRecognizer offers an extension of the functionality previously exposted through the dominantLanguage in NSLinguisticTagger, with the ability to provide hints and get additional predictions.

nshipster.com/nllanguagerecognizer/

Apple’s Natural Language framework (iOS 12+) finally lets developers use the same powerful language-detection technology that powers Siri and system features.

The star for detecting language is NLLanguageRecognizer.

Basic usage is almost trivial:

let text = "Je peux manger du verre, ça ne me fait pas mal."
let recognizer = NLLanguageRecognizer()
recognizer.processString(text)
let language = recognizer.dominantLanguage?.rawValue   // "fr"

It is extremely accurate, often 95 %+ confident after only a few words, and supports dozens of languages.

You can also:

Practical uses:

NLLanguageRecognizer is fast, works fully on-device (no network), and needs zero setup. It is one of the easiest ways to make your app feel instantly smarter and more respectful of the user’s language.


Category:

Year: