NLLanguageRecognizer
The language of a body of text.
Declaration
class NLLanguageRecognizerMentioned in
Overview
An NLLanguageRecognizer object automatically detects the language of a piece of text. It performs language identification by:
Identifying the dominant script of a piece of text. Some languages have a unique script (like Greek), but others share the same script (like English, French, and German, which all share the Latin script).
Identifying the language itself.
The identification obtained from an NLLanguageRecognizer object can be either a single most likely language, accessed through dominantLanguage, or a set of language candidates with probabilities, using languageHypotheses(withMaximum:). You can reset the recognizer to its initial state, to be reused for new analysis.
Use the convenience method, dominantLanguage(for:), to get the most likely language without creating an NLLanguageRecognizer.
Language identification is most reliable for longer passages of text. For very short inputs of only a few words, accuracy is lower, and for some text there may be no well-defined answer at all. For example, personal names are shared across languages and don’t belong definitively to any one of them. When you work with short or ambiguous text, inspect the full set of candidates with languageHypotheses(withMaximum:) rather than relying on a single dominantLanguage. When you have prior knowledge about the input, like a person’s preferred languages, bias the recognizer toward them with languageHints, or limit it to a known set with languageConstraints.