Natural Language

作者 dpearson26998d90fd121a26無授權條款1.1K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫2 個月前更新

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 translation to iOS/macOS/visionOS apps.

AI 產生的概覽

指導 iOS/macOS/visionOS 開發者使用 Apple 的 NaturalLanguage 與 Translation 框架進行文字分析與翻譯。

功能
此技能為 Apple 的 NaturalLanguage 框架提供參考指南與 Swift 程式碼範例,涵蓋斷詞、語言辨識、詞性標註、命名實體辨識、情感分析,以及詞/句嵌入。它也涵蓋 Translation 框架的應用程式內翻譯,包括系統翻譯覆蓋層、程式化翻譯與批次翻譯,以及語言可用性檢查。內容包含常見錯誤、審查清單,以及一個提供延伸模式的參考檔案。
適用情境
在為 iOS、macOS 或 visionOS 應用程式加入裝置端文字分析或應用程式內翻譯時使用此技能。它適合已有文字、需要斷詞、標註、情感、嵌入或翻譯能力的開發者。不適用於 OCR、語音轉文字、UI 在地化或生成式摘要。
執行需求
需要 Apple 平台開發環境(iOS/macOS/visionOS)、Swift,以及 NaturalLanguage 與 Translation 框架。NaturalLanguage 不需要特殊權限;Translation 功能的系統呈現需要 iOS 17.4+/macOS 14.4+,程式化工作階段需要 iOS 18+/macOS 15+。不附帶指令碼,僅為說明文件,另有一個參考 markdown 檔案。

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

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.

swift
import NaturalLanguageimport Translation

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.

swift
import NaturalLanguage
func tokenizeWords(in text: String) -> [String] {    let tokenizer = NLTokenizer(unit: .word)    tokenizer.string = text
    let range = text.startIndex..<text.endIndex    return tokenizer.tokens(for: range).map { String(text[$0]) }}

Token Units

UnitDescription
.wordIndividual words
.sentenceSentences
.paragraphParagraphs
.documentEntire document

Enumerating with Attributes

Use enumerateTokens(in:using:) to detect numeric or emoji tokens.

swift
let tokenizer = NLTokenizer(unit: .word)tokenizer.string = text
tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { range, attributes in    if attributes.contains(.numeric) {        print("Number: \(text[range])")    }    return true // continue enumeration}

Language Identification

Detect the dominant language of a string with NLLanguageRecognizer.

swift
func detectLanguage(for text: String) -> NLLanguage? {    NLLanguageRecognizer.dominantLanguage(for: text)}
// Multiple hypotheses with confidence scoresfunc languageHypotheses(for text: String, max: Int = 5) -> [NLLanguage: Double] {    let recognizer = NLLanguageRecognizer()    recognizer.processString(text)    return recognizer.languageHypotheses(withMaximum: max)}

Constrain the recognizer to expected languages for better accuracy on short text.

swift
let recognizer = NLLanguageRecognizer()recognizer.languageConstraints = [.english, .french, .spanish]recognizer.processString(text)let detected = recognizer.dominantLanguage

Part-of-Speech Tagging

Identify nouns, verbs, adjectives, and other lexical classes with NLTagger.

swift
func tagPartsOfSpeech(in text: String) -> [(String, NLTag)] {    let tagger = NLTagger(tagSchemes: [.lexicalClass])    tagger.string = text
    var results: [(String, NLTag)] = []    let range = text.startIndex..<text.endIndex    let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace]
    tagger.enumerateTags(in: range, unit: .word, scheme: .lexicalClass, options: options) { tag, tokenRange in        if let tag {            results.append((String(text[tokenRange]), tag))        }        return true    }    return results}

Common Tag Schemes

SchemeOutput
.lexicalClassPart of speech (noun, verb, adjective)
.nameTypeNamed entity type (person, place, organization)
.nameTypeOrLexicalClassCombined NER + POS
.lemmaBase form of a word
.languagePer-token language
.sentimentScoreSentiment polarity score

Named Entity Recognition

Extract people, places, and organizations.

swift
func extractEntities(from text: String) -> [(String, NLTag)] {    let tagger = NLTagger(tagSchemes: [.nameType])    tagger.string = text
    var entities: [(String, NLTag)] = []    let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
    tagger.enumerateTags(        in: text.startIndex..<text.endIndex,        unit: .word,        scheme: .nameType,        options: options    ) { tag, tokenRange in        if let tag, tag != .other {            entities.append((String(text[tokenRange]), tag))        }        return true    }    return entities}// NLTag values: .personalName, .placeName, .organizationName

Sentiment Analysis

Score text sentiment from -1.0 (negative) to +1.0 (positive).

swift
func sentimentScore(for text: String) -> Double? {    let tagger = NLTagger(tagSchemes: [.sentimentScore])    tagger.string = text
    let (tag, _) = tagger.tag(        at: text.startIndex,        unit: .paragraph,        scheme: .sentimentScore    )    return tag.flatMap { Double($0.rawValue) }}

Text Embeddings

Measure semantic similarity between words or sentences with NLEmbedding.

swift
func wordSimilarity(_ word1: String, _ word2: String) -> Double? {    guard let embedding = NLEmbedding.wordEmbedding(for: .english) else { return nil }    return embedding.distance(between: word1, and: word2, distanceType: .cosine)}
func findSimilarWords(to word: String, count: Int = 5) -> [(String, Double)] {    guard let embedding = NLEmbedding.wordEmbedding(for: .english) else { return [] }    return embedding.neighbors(for: word, maximumCount: count, distanceType: .cosine)}

Sentence embeddings compare entire sentences.

swift
func sentenceSimilarity(_ s1: String, _ s2: String) -> Double? {    guard let embedding = NLEmbedding.sentenceEmbedding(for: .english) else { return nil }    return embedding.distance(between: s1, and: s2, distanceType: .cosine)}

Translation

System Translation Overlay

Show the built-in translation UI with .translationPresentation().

swift
import SwiftUIimport Translation
struct TranslatableView: View {    @State private var showTranslation = false    let text = "Hello, how are you?"
    var body: some View {        Button { showTranslation = true } label: {            Text(text)        }        .buttonStyle(.plain)        .translationPresentation(            isPresented: $showTranslation,            text: text        )    }}

Programmatic Translation

Use .translationTask() for programmatic translations within a view context.

swift
struct TranslatingView: View {    @State private var translatedText = ""    @State private var translationErrorMessage: String?    @State private var configuration: TranslationSession.Configuration?
    var body: some View {        VStack {            Text(translatedText)            Button("Translate") {                configuration = .init(source: Locale.Language(identifier: "en"),                                      target: Locale.Language(identifier: "es"))            }        }        .translationTask(configuration) { session in            do {                let response = try await session.translate("Hello, world!")                await MainActor.run {                    translatedText = response.targetText                    translationErrorMessage = nil                }            } catch {                let message = error.localizedDescription                await MainActor.run {                    translationErrorMessage = message                }            }        }    }}

Batch Translation

Translate multiple strings in a single session.

swift
.translationTask(configuration) { session in    do {        let requests = texts.enumerated().map { index, text in            TranslationSession.Request(sourceText: text,                                       clientIdentifier: "\(index)")        }        let responses = try await session.translations(from: requests)        for response in responses {            print("\(response.sourceText) -> \(response.targetText)")        }    } catch {        // Handle cancellation, unsupported languages, or download refusal.    }}

Checking Language Availability

swift
let availability = LanguageAvailability()let status = await availability.status(    from: Locale.Language(identifier: "en"),    to: Locale.Language(identifier: "ja"))switch status {case .installed: break    // Ready to translate offlinecase .supported: break    // Needs downloadcase .unsupported: break  // Language pair not available}

Common Mistakes

DON'T: Share NLTagger/NLTokenizer across threads

These classes are not thread-safe and will produce incorrect results or crash.

swift
// WRONGlet sharedTagger = NLTagger(tagSchemes: [.lexicalClass])DispatchQueue.concurrentPerform(iterations: 10) { _ in    sharedTagger.string = someText  // Data race}
// CORRECTawait withTaskGroup(of: Void.self) { group in    for _ in 0..<10 {        group.addTask {            let tagger = NLTagger(tagSchemes: [.lexicalClass])            tagger.string = someText            // process...        }    }}

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.

swift
// WRONG: Trying to do NER with raw Core MLlet coreMLModel = try MLModel(contentsOf: modelURL)
// CORRECT: Use NLTagger for built-in NERlet tagger = NLTagger(tagSchemes: [.nameType])
// Or load a custom Core ML model via NLModellet nlModel = try NLModel(mlModel: coreMLModel)tagger.setModels([nlModel], forTagScheme: .nameType)

DON'T: Assume embeddings exist for all languages

Not all languages have word or sentence embeddings available on device.

swift
// WRONG: Force unwraplet embedding = NLEmbedding.wordEmbedding(for: .japanese)!
// CORRECT: Handle nilguard let embedding = NLEmbedding.wordEmbedding(for: .japanese) else {    // Embedding not available for this language    return}

DON'T: Create a new tagger per token

Creating and configuring a tagger is expensive. Reuse it for the same text.

swift
// WRONG: New tagger per wordfor word in words {    let tagger = NLTagger(tagSchemes: [.lexicalClass])    tagger.string = word}
// CORRECT: Set string once, enumeratelet tagger = NLTagger(tagSchemes: [.lexicalClass])tagger.string = fullTexttagger.enumerateTags(in: fullText.startIndex..<fullText.endIndex,                     unit: .word, scheme: .lexicalClass, options: []) { tag, range in    return true}

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.

swift
// WRONG: Detect language of a single wordlet lang = NLLanguageRecognizer.dominantLanguage(for: "chat")  // French or English?
// CORRECT: Provide contextlet recognizer = NLLanguageRecognizer()recognizer.languageHints = [.english: 0.8, .french: 0.2]recognizer.processString("chat")

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

來源與署名

來源:dpearson2699/swift-ios-skills位於skills/natural-language提交8d90fd1

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架

更多來自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指導實作、審查與改進 iOS、iPadOS、watchOS 與 CarPlay 上的 WidgetKit 小工具與控制項。

Software Development1.1K2 個月前更新

Weatherkit

dpearson2699

指導 iOS 開發者使用 WeatherService 取得 WeatherKit 預報、警報與署名資訊。

Software Development1.1K2 個月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分類1.1K2 個月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分類1.1K2 個月前更新

Tabletopkit

dpearson2699

指導使用 TabletopKit 在 visionOS 上打造多人空間桌遊,涵蓋棋具、座位、動作與 RealityKit 算繪。

Software Development1.1K2 個月前更新

Swiftui Webkit

dpearson2699

指導在 iOS 26 及更新版本的 SwiftUI App 中使用 WebKit for SwiftUI 嵌入與控制網頁內容。

Software Development1.1K2 個月前更新