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:
Ask for the top guesses with confidence scores
recognizer.languageHypotheses(withMaximum: 5)Give it hints if you already have a suspicion
recognizer.languageHints = [.danish: 0.2, .norwegian: 0.8]
Practical uses:
- Feed the detected language into UITextChecker for correct spell-checking
- Pass it to AVSpeechSynthesizer so text is spoken with the right accent
- Auto-select the correct keyboard or dictionary
- Route customer-support messages to the right team
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:
Tags:
Year: