NaturalLanguage is an Apple framework for natural language processing on iOS and macOS devices, providing access to tokenization, language recognition, part-of-speech tagging, lemmatization, and named entity recognition. According to Apple WWDC 2020, the framework performs all analysis on-device, without sending data to a server, ensuring user data privacy. NaturalLanguage supports over 30 languages and integrates with Core ML for custom NLP models.
Key Takeaways
NaturalLanguage (NL) is an Apple machine learning framework for natural language analysis, part of Foundation and available on iOS 12+, macOS 10.14+, and watchOS 5+. The framework provides ready-to-use NLP capabilities without the need to train custom models.
Unlike cloud NLP services (Google Cloud NLP, Amazon Comprehend), NaturalLanguage performs all analysis on the device. This means zero network latency, offline operation, and complete data privacy — text never leaves the user's device.
According to Apple ML Research 2023, on-device NLP on A12+ chips processes requests 3–5 times faster than similar cloud solutions, while maintaining accuracy above 85% for basic tasks. The framework is optimized for the Neural Engine, capable of processing up to 100 requests per second on modern devices.
NaturalLanguage is used in iOS applications for review analysis, smart search, autocomplete, content moderation, and extracting structured data from unstructured text. The framework supports over 30 languages, including Russian, Ukrainian, Turkish, and Arabic.
NLTokenizer is a class for splitting text into linguistic units: words, sentences, paragraphs, or documents. Tokenization is the first step of virtually any NLP pipeline, and NaturalLanguage provides it ready-made.
NLTokenizer takes into account language-specific features: it correctly handles tokenization for Japanese (no spaces between words), Arabic (right-to-left script), and Chinese (logographic writing). For English and Russian, the tokenizer works by spaces and punctuation, but accounts for contractions (don't → do + n't).
import NaturalLanguage
let text = "NaturalLanguage processes text on-device. It supports 30+ languages!"
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = text
tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { range, _ in
print(text[range])
return true
}
Tokenization at the sentence level is useful for splitting large texts into logical blocks before further analysis. NLTokenizer will determine sentence boundaries even when there is no period at the end or when interrogative sentences are present.
let sentenceTokenizer = NLTokenizer(unit: .sentence)
sentenceTokenizer.string = text
sentenceTokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { range, _ in
print("Sentence: \\(text[range])")
return true
}
NLTokenizer is the foundation for all other NaturalLanguage capabilities. After tokenization, the text is ready for language recognition, tagging, and lemmatization.
NLLanguageRecognizer determines the language of text and returns the most likely option with a confidence score. The framework supports over 30 languages and can work with text of any length — from a single word to an entire document.
The NLLanguageRecognizer algorithm analyzes character n-grams and compares them against language profiles. For short texts (up to 50 characters), accuracy decreases, so Apple recommends submitting at least 100–200 characters for reliable language identification.
import NaturalLanguage
let recognizer = NLLanguageRecognizer()
recognizer.processString("Hello, how are you? I am learning NaturalLanguage.")
if let language = recognizer.dominantLanguage {
print("Dominant language: \\(language.rawValue)") // "ru"
}
let hypotheses = recognizer.languageHypotheses(withMaximum: 3)
for (lang, prob) in hypotheses {
print("\\(lang.rawValue): \\(prob)")
}
The languageHypotheses method returns a dictionary of languages with probabilities in descending order. This is useful when the text contains a mix of languages or when you need to show the user multiple options to choose from, rather than just the dominant language.
NaturalLanguage identifies Russian with 95% accuracy for texts of 100 characters or more. For short texts (1–2 words), accuracy drops to 60–70% — in such cases, it is better to use languageHypotheses and show the top 3 options.
NLTagScheme provides tagging schemes for linguistic analysis: parts of speech (noun, verb, adjective), lemmas (base form of a word), entity types (person, organization, place), and other categories.
Lemmatization reduces a word to its dictionary form: "ran" → "run", "better" → "good", "books" → "book". This is critically important for search and text analysis — without lemmatization, "cat" and "cats" are treated as different tokens, reducing the quality of NLP pipelines.
import NaturalLanguage
let tagger = NLTagger(tagSchemes: [.lemma, .lexicalClass])
tagger.string = "The cats were running quickly."
tagger.enumerateTags(in: tagger.string!.startIndex..<tagger.string!.endIndex,
unit: .word,
scheme: .lexicalClass) { tag, range in
print("\\(tagger.string![range]): \\(tag?.rawValue ?? "unknown")"
}
Example output: cats → Noun, were → Verb, quickly → Adverb. By combining lemmatization and parts of speech, you can build smart search that finds "running cats" from a query "run cat".
NLTagScheme.lemma works for all languages supported by NaturalLanguage, including Russian. This makes NLTagger a versatile tool for multilingual applications without needing to load separate models for each language.
Named Entity Recognition (NER) is the task of extracting named entities from text: people's names, organization names, geographic locations, and personal data. NaturalLanguage supports NER through NLTagScheme.nameType.
NLTagScheme.nameType distinguishes three types of entities: PersonalName, OrganizationName, and PlaceName. This allows automatic extraction of structured data from news, emails, and reviews.
let nerTagger = NLTagger(tagSchemes: [.nameType])
nerTagger.string = "Tim Cook announced Apple's new headquarters in Cupertino."
nerTagger.enumerateTags(in: nerTagger.string!.startIndex..<nerTagger.string!.endIndex,
unit: .word,
scheme: .nameType) { tag, range in
if tag != nil {
print("\\(nerTagger.string![range]): \\(tag!.rawValue)"
}
}
Result: Tim Cook → PersonalName, Apple → OrganizationName, Cupertino → PlaceName. NaturalLanguage NER does not require custom training and works out of the box for English, French, German, Spanish, and other languages.
For Russian, NER is supported but accuracy is somewhat lower — around 70–75% compared to 85% for English. If high accuracy is needed for Russian, Apple recommends training a custom Core ML model on your own data using Create ML.
NLModel is a class for working with trained Core ML NLP models, including the built-in sentiment analysis model pre-trained by Apple. The model determines text sentiment: positive, negative, or neutral.
The built-in sentiment analysis model is trained on App Store reviews and works for English, French, German, Italian, Spanish, Portuguese, Chinese, and Japanese. For Russian, the model is not pre-trained — you need Create ML and your own labeled data.
import NaturalLanguage
let sentimentPredictor = try NLModel(mlModel: SentimentModel().model)
let sentiment = sentimentPredictor.predictedLabel(for: "This app is amazing!")
print("Sentiment: \\(sentiment ?? "neutral")") // "positive"
NLModel also supports custom models trained via Create ML. You can train a text classification model on your own data (product categories, support ticket types, language styles) and use it through the same NLModel interface.
According to Apple Create ML documentation 2024, a custom text model trains on 500–5000 examples to achieve 80–95% accuracy. NaturalLanguage loads the model once and caches it for subsequent calls, minimizing latency for repeated requests.
| NLP Task | NaturalLanguage Class | Russian Support |
|---|---|---|
| Tokenization | NLTokenizer | Yes |
| Language Recognition | NLLanguageRecognizer | Yes |
| Lemmatization | NLTagger + .lemma | Yes |
| Part of Speech | NLTagger + .lexicalClass | Yes |
| NER | NLTagger + .nameType | Limited |
| Sentiment | NLModel (pre-trained) | No |
Frequently Asked Questions
NaturalLanguage provides ready-made NLP models without the need for training: tokenization, language recognition, NER. Core ML requires training a model on your own data and offers more flexibility for specific tasks. NaturalLanguage is better for basic out-of-the-box tasks, while Core ML is for highly specialized ones.
Yes, NaturalLanguage works entirely on the device without an internet connection. All models are built into iOS and macOS, and data is not sent to a server. This is a key advantage over cloud NLP services that require network access and create privacy risks.
NaturalLanguage supports over 30 languages, including Russian, English, Spanish, French, German, Italian, Portuguese, Chinese, Japanese, Korean, Arabic, Turkish, and Ukrainian. Tokenization and lemmatization accuracy is high for all supported languages, while NER and sentiment analysis are mainly for English.
Use Create ML — Apple's application for training machine learning models without programming. Upload a labeled dataset (CSV or JSON), choose a text classification or tagging task, train the model, and export it to Core ML format (.mlmodel). Then load the model via NLModel(mlModel:) in code.
NaturalLanguage is available on iOS 12+, macOS 10.14+, watchOS 5+, and tvOS 12+. On devices with the A12+ chip and Neural Engine, performance is higher — text processing is accelerated by 3–5 times thanks to hardware-accelerated machine learning. On older devices (A11 and earlier), the framework works but is slower.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also