Natural Language Processing Implementation in Mobile Applications
NLP on mobile is not one task but several fundamentally different ones: text classification, entity extraction (NER), sentiment analysis, summarization, machine translation. Each solved with different tools with different resource requirements.
Platform NLP APIs
Start with what's already on device — free on resources and works offline.
iOS — NaturalLanguage framework:
import NaturalLanguage
// Language identification
let recognizer = NLLanguageRecognizer()
recognizer.processString("Hello, how are you?")
let language = recognizer.dominantLanguage // .english
// Tokenization
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = text
tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { range, _ in
print(String(text[range]))
return true
}
// Sentiment analysis
let tagger = NLTagger(tagSchemes: [.sentimentScore])
tagger.string = text
let (sentiment, _) = tagger.tag(at: text.startIndex,
unit: .paragraph,
scheme: .sentimentScore)
let score = Double(sentiment?.rawValue ?? "0") ?? 0.0
// score: -1.0 (negative) ... +1.0 (positive)
Android — ML Kit Text APIs:
EntityExtraction (ML Kit) finds addresses, phones, dates, tracking numbers, money — offline, model downloads once (~8 MB). LanguageIdentification — analog of NLLanguageRecognizer. SmartReply model — covered in separate service.
Text Classification on TFLite
Platform APIs can't do domain classification — won't say "food review" or "tech support request". Need own model.
Typical pipeline for mobile NLP classifier:
- Training: BERT-tiny or MobileBERT (8 MB vs 100 MB full BERT) on PyTorch/TF
- Conversion:
torch.onnx.export()→ ONNX →onnxruntime-mobile, ortf2tflite→.tflite - Quantization: int8 via TFLite Converter gives ~4x compression with minimal accuracy loss
class TextClassifier(context: Context) {
private val interpreter: Interpreter
private val tokenizer: BertTokenizer
init {
val modelBuffer = loadModelFile(context, "bert_tiny_classifier.tflite")
interpreter = Interpreter(modelBuffer, Interpreter.Options().apply {
addDelegate(NnApiDelegate()) // Android Neural Networks API
})
tokenizer = BertTokenizer.fromAssets(context, "vocab.txt")
}
fun classify(text: String): ClassificationResult {
val tokens = tokenizer.encode(text, maxLength = 128, truncate = true)
val inputIds = Array(1) { tokens.inputIds.toIntArray() }
val attentionMask = Array(1) { tokens.attentionMask.toIntArray() }
val output = Array(1) { FloatArray(NUM_LABELS) }
interpreter.runForMultipleInputsOutputs(
arrayOf(inputIds, attentionMask),
mapOf(0 to output)
)
return output[0].argmax().let { ClassificationResult(label = LABELS[it], confidence = output[0][it]) }
}
}
NNAPI delegate on Android 8.1+ speeds inference via DSP/NPU. On Pixel 7+ — up to 10x acceleration. On budget devices NNAPI can be slower than CPU — test on real devices.
Named Entity Recognition
NER — extract named entities from text (persons, organizations, locations, dates). Applications: auto-create calendar events from messages, pre-fill forms from CV, parse receipts.
ML Kit EntityExtraction covers standard entities without training. For custom domains — own NER model based on BiLSTM+CRF or BERT. Model size: BiLSTM — 5–15 MB, DistilBERT-NER — ~60 MB in fp16.
On iOS NL framework returns NLTag with types: .personalName, .placeName, .organizationName. Works offline.
Summarization and Translation
On-device summarization requires heavy models (BART, T5 minimum 60–100 MB). For mobile — either extractive summarization (select key sentences without generation, via TF-IDF + MMR, ~100 KB logic) or cloud API (OpenAI, YandexGPT).
Machine translation: ML Kit Translation supports 59 languages, models download on demand (~30 MB per language pair). On iOS — MLTranslation via Apple Intelligence (iOS 18+) or cloud APIs.
User Input Processing: Cleaning and Normalization
Often overlooked step — text pre-processing before NLP model. Typos, slang, emojis, mixed keyboard layout — all reduce accuracy. Apache Lucene Analyzers on Android or NLTokenizer with custom rules on iOS help normalize text.
Implementation Process
Define NLP tasks and quality requirements. Choose approach: platform APIs, ML Kit or custom model. For custom: prepare data, train, quantize, convert. Integrate with NNAPI/Metal delegate for acceleration. Test on language diversity (dialects, input errors).
Timeline Guidelines
Integrate platform NLP APIs (sentiment, tokenization, NER) — 3–5 days. Custom classifier with training and mobile optimization — 3–6 weeks.







