NaturalLanguage + Translation
Analyze natural language text for tokenization, part-of-speech tagging, named entity recognition, sentiment analysis, language identification, and embeddings using NaturalLanguage. Translate text with Translation. Targets Swift 6.3 / iOS 26+.
Contents
- Framework Scope and Boundaries
- Core Capabilities Matrix
- Thread Safety and Performance
- Translation Availability
- Route by Task
- Common Mistakes
- Review Checklist
- References
Framework Scope and Boundaries
NaturalLanguage: On-device text analysis (NLTokenizer,NLTagger,NLLanguageRecognizer,NLEmbedding,NLModel). Requires no special entitlements.Translation: In-app UI and programmatic language translation (TranslationSession,LanguageAvailability).- Boundaries: Route OCR and image text recognition to
vision-framework; route audio speech-to-text tospeech-recognition; route generative LLM features toapple-on-device-ai; route static localization toios-localization.
Core Capabilities Matrix
| Capability | Primary Class | Key Output |
|---|---|---|
| Tokenization | NLTokenizer |
Substrings segmented by word, sentence, or paragraph |
| Language ID | NLLanguageRecognizer |
NLLanguage (e.g. .english, .vietnamese) with confidence |
| Part of Speech | NLTagger (.lexicalClass) |
NLTag (.noun, .verb, .adjective, .pronoun) |
| Named Entities | NLTagger (.nameType) |
Personal names, place names, organization names |
| Sentiment | NLTagger (.sentimentScore) |
Continuous score between -1.0 and +1.0 |
| Semantic Distance | NLEmbedding |
Cosine distance and neighbor queries for words/sentences |
| System Translation | .translationPresentation() |
Native system modal translation sheet |
| Batch Translation | TranslationSession |
Async batch translated strings |
Thread Safety and Performance
[!IMPORTANT]
NLTokenizerandNLTaggerare not thread-safe. Do not share instances across concurrent tasks or queues. Create instances on demand or isolate them within a serial actor.NLEmbeddinginstances are read-only and thread-safe once loaded into memory.
Translation Availability
.translationPresentation(isPresented:text:): iOS 17.4+, macOS 14.4+, visionOS 1.1+TranslationSession,.translationTask(), andLanguageAvailability: iOS 18.0+, macOS 15.0+, visionOS 2.0+- Offline programmatic translation via
TranslationSession(installedSource:target:)requires the target language packs to be already installed on device.
Route by Task
- For word/sentence tokenization, language recognition, and emoji/numeric detection, read Tokenization and Language ID.
- For POS tagging, entity extraction, sentiment analysis, and embeddings, read Tagging and Embeddings.
- For custom Core ML text classifiers and taggers with
NLModel, read Custom NLModel Classifiers. - For SwiftUI translation sheets, batch translation, and language pack availability, read Translation Patterns.
Common Mistakes
- Sharing an
NLTokenizerorNLTaggeracross concurrent threads or Tasks without isolation. - Passing empty strings to
NLEmbeddingor checking similarity without unwrapping optionals. - Using
TranslationSessionoffline when language packs are not installed on device. - Performing synchronous NLP tagging or sentence embedding on the main thread during UI scrolling.
- Misinterpreting sentiment scores: scores range from -1.0 to 1.0 (0.0 is neutral, nil indicates untagged).
Review Checklist
-
NLTokenizerandNLTaggerused from a single thread or isolated in an actor - Availability guards applied for
TranslationSession(iOS 18+) vs presentation sheet (iOS 17.4+) - Language availability verified with
LanguageAvailability.status()before programmatic translation - Long text analysis dispatched off
@MainActorto background tasks -
NLEmbedding.wordEmbedding(for:)checked for nil availability in the target language - Tag options include
.omitWhitespaceand.omitPunctuationwhere appropriate
References
- NaturalLanguage text analysis patterns
- Translation framework patterns and SwiftUI views
- NaturalLanguage documentation
- Translation documentation
- NLTokenizer
- NLTagger
- NLEmbedding