Localex is a lightweight SwiftUI showcase app demonstrating Apple’s NaturalLanguage framework for classical Natural Language Processing (NLP) tasks such as tokenization, lemmatization, language identification, and part-of-speech tagging — all fully on-device.
- 🔤 Tokenization: Split text into words, sentences, and paragraphs
- 🌍 Language Identification: Detect the dominant language of text
- 🧬 Word Embeddings: Generate and inspect the vector representation of a word
- ⚡ On-device & Offline: No internet connection required
- 🎨 Minimal SwiftUI UI: Clean interface to visualize NLP results
The app has three tabs, each backed by its own view model:
TokenizerViewModelfor tokenizationRecognizerViewModelfor language detectionEmbeddingViewModelfor word vectors
This case demonstrates how a text is split into discrete word units.
- The view model creates
NLTokenizer(unit: .word). - When the user taps Tokenize,
inputTextis assigned totokenizer.string. enumerateTokens(in:)walks through the full string range.- For each token range, the app slices
inputText[range]and appends it totokens. - The UI renders each token as a separate
Textrow.
This case demonstrates how Apple estimates which language(s) a text belongs to and with what confidence.
- The view model creates
NLLanguageRecognizer(). - On Recognize,
processString(inputText)feeds the entire input into the recognizer. - The app calls
languageHypotheses(withMaximum: 10)to get up to 10 candidates. - Candidates are sorted descending by probability (
Doubleconfidence value from 0.0 to 1.0). - The UI shows each language code (
rawValue), a percentage label, and aProgressView.
This case demonstrates how a word is mapped to a high-dimensional numeric vector that represents semantic meaning.
- The input is split by spaces and only the first word is used.
- The app loads
NLEmbedding.wordEmbedding(for: .english). - It requests a vector with
vector(for: firstWord). - If successful, the resulting
[Double]is stored invector. - The UI shows the vector dimension (
vector.count) and each component value.