natural-language
Tokenize, tag, and analyze natural language text using Apple's NaturalLanguage framework and translate between languages with the Translation framework. Use when adding language identification, sentiment analysis, named entity recognition, part-of-speech tagging, text embeddings, or in-app translati
By dpearson2699 · 3,214 installs
npx skills add dpearson2699/swift-ios-skills --skill natural-language
Source repository · Upstream listing
NaturalLanguage + Translation
Analyze natural language text for tokenization, part of speech tagging, named
entity recognition, sentiment analysis, language identification, and word/sentence
embeddings. Translate text between languages with the Translation framework.
This skill covers two related frameworks: NaturalLanguage ( NLTokenizer , NLTagger , NLEmbedding ) for on device text analysis, and Translation ( TranslationSession , LanguageAvailability ) for language translation.
Scope boundary: Use this skill after you already have text. It owns
tokenization, language identification, POS/NER tagging, sentiment, embeddings,
custom NLModel classifiers/taggers, and in app translation. Hand off OCR to
vision framework , speech to text to speech recognition , UI strings and
locale formatting to ios localization , and generative summarization or Apple
Intelligence workflows to apple on device ai .
Contents
[Setup]( setup)
[Tokenization]( tokenization)
[Language Identification]( language identification)
[Part of Speech Tagging]( part of speech tagging)
[Named Entity Recognition]( named entity recognition)
[Sentiment Analysis]( sentiment analysis)
[Text Embeddings]( text embeddings)
[Translation]( translation)
[Common Mistakes]( common mistakes)
[Review Checklist]( review checklist)
[References]( references)
Setup
Import NaturalLanguage for text analysis and Translation for language
translation. No special entitlements or capabilities are required for
NaturalLanguage. Translation has split availability: system translation
presentation is iOS 17.4+ / macOS 14.4+, while TranslationSession ,
.translationTask() , LanguageAvailability , and batch translation require
iOS 18+ / macOS 15+.
Direct TranslationSession(installedSource:target:) is the non UI option, but
only when the source and target languages are already installed on device.
NaturalLanguage classes ( NLTokenizer , NLTagger ) are not thread safe .
Use each instance from one thread or dispatch queue at a time.
Tokenization
Segment text into words, sentences, or paragraphs with NLTokenizer .
Token Units
Unit Description
.word Individual words
.sentence Sentences
.paragraph Paragraphs
.document Entire document
Enumerating with Attributes
Use enumerateTokens(in:using:) to detect numeric or emoji tokens.
Language Identification
Detect the dominant language of a string with NLLanguageRecognizer .
Constrain the recognizer to expected languages for better accuracy on short text.
Part of Speech Tagging
Identify nouns, verbs, adjectives, and other lexical classes with NLTagger .
Common Tag Schemes
Scheme Output
.lexicalClass Part of speech (noun, verb, adjective)
.nameType Named entity type (person, place, organization)
.nameTypeOrLexicalClass Combined NER + POS
.lemma Base form of a word
.language Per token language
.sentimentScore Sentiment polarity score
Named Entity Recognition
Extract people, places, and organizations.
Sentiment Analysis
Score text sentiment from 1.0 (negative) to +1.0 (positive).
Text Embeddings
Measure semantic similarity between words or sentences with NLEmbedding .
Sentence embeddings compare entire sentences.
Translation
System Translation Overlay
Show the built in translation UI with .translationPresentation() .
Programmatic Translation
Use .translationTask() for programmatic translations within a view context.
Batch Translation
Translate multiple strings in a single session.
Checking Language Availability
Common Mistakes
DON'T: Share NLTagger/NLTokenizer across threads
These classes are not thread safe and will produce incorrect results or crash.
DON'T: Confuse NaturalLanguage with Core ML
NaturalLanguage provides built in linguistic analysis. Use Core ML for custom
trained models. They complement each other via NLModel .
DON'T: Assume embeddings exist for all languages
Not all languages have word or sentence embeddings available on device.
DON'T: Create a new tagger per token
Creating and configuring a tagger is expensive. Reuse it for the same text.
DON'T: Ignore language hints for short text
Language detection on short strings (under ~20 characters) is unreliable.
Set constraints or hints to improve accuracy.
Review Checklist
[ ] NLTokenizer and NLTagger instances used from a single thread
[ ] Tagger created once per text, not per token
[ ] Language detection uses constraints/hints for short text
[ ] NLEmbedding availability checked before use (returns nil if unavailable)
[ ] Translation LanguageAvailability checked before attempting translation
[ ] .translationTask() used within a SwiftUI view hierarchy
[ ] Batch translation uses clientIdentifier to match responses to requests
[ ] Sentiment scores handled as optional (may return nil for unsupported languages)
[ ] .joinNames option used with NER to keep multi word names together
[ ] Custom ML models loaded via NLModel , not raw Core ML
References
Extended patterns (custom models, contextual embeddings, gazetteers): [references/translation patterns.md](references/translation patterns.md)
[Natural Language framework](https://sosumi.ai/documentation/naturallanguage)
[NLTokenizer](https://sosumi.ai/documentation/naturallanguage/nltokenizer)
[NLTagger](https://sosumi.ai/documentation/naturallanguage/nltagger)
[NLEmbedding](https://sosumi.ai/documentation/naturallanguage/nlembedding)
[NLLanguageRecognizer](https://sosumi.ai/documentation/naturallanguage/nllanguagerecognizer)
[Translation framework](https://sosumi.ai/documentation/translation)
[TranslationSession](https://sosumi.ai/documentation/translation/translationsession)
[TranslationSession.Strategy](https://sosumi.ai/documentation/translation/translationsession/strategy)
[LanguageAvailability](https://sosumi.ai/documentation/translation/languageavailability)