Overview

Android 8.1 introduces the TextClassfier API that uses machine learning techniques to help developers classify text:

TextClassificationManager tcm =
    context.getSystemService(TextClassificationManager.class);
TextClassifier classifier = tcm.getTextClassifier();
TextSelection selection = classifier.suggestSelection(...);
TextClassification classification = classifier.classifyText(...);

Developers may choose to set a custom text classifier:

tcm.setTextClassifier(customTextClassifier);

But if an app developer sets the text classifier to null, a system default text classifier is returned for getTextClassifier().

See: android.view.textclassifier.TextClassifierImpl

TextView and WebView use the TextClassifier for smart selection and smart text share features:

Figure 1. TEXTCLASSIFIER usage.

TextClassifier neural-net models

The Android Open Source Project (AOSP) features a number of neural network models for classifying text. Each model file is trained for a single language. You may choose to install any combination of models. The models are defined in:

external/libtextclassifier/Android.mk

Pre-installing language models on devices

You may specify a bundle of language models and install them on a device:

# -----------------------
# Smart Selection bundles
# -----------------------

include $(CLEAR_VARS)
LOCAL_MODULE           := textclassifier.smartselection.bundle1
LOCAL_REQUIRED_MODULES := textclassifier.smartselection.en.model
LOCAL_REQUIRED_MODULES += textclassifier.smartselection.es.model
LOCAL_REQUIRED_MODULES += textclassifier.smartselection.de.model
LOCAL_REQUIRED_MODULES += textclassifier.smartselection.fr.model
include $(BUILD_STATIC_LIBRARY)

For example, in device/google/marlin/device-common.mk

# TextClassifier smart selection model files
PRODUCT_PACKAGES += \
    textclassifier.smartselection.bundle1

Inspecting installed language modules

Use ADB to list the files in the directory:

$ adb shell ls -l /etc/textclassifier
-rw-r--r-- 1 root root ... textclassifier.smartselection.de.model
-rw-r--r-- 1 root root ... textclassifier.smartselection.en.model
-rw-r--r-- 1 root root ... textclassifier.smartselection.es.model
-rw-r--r-- 1 root root ... textclassifier.smartselection.fr.model

Model updates

Models can be updated either by having a new model included as part of a system image update, or dynamically via having a system component that would trigger an update through the System API ACTION_UPDATE_SMART_SELECTION intent. By broadcasting this System API intent, the framework is able to update the language model of the currently set language. The models themselves contain the supported language and a version number so the latest appropriate model is used.

So you don't need to preload models for all languages because they can be added later. If no model file is found for the specified language, text classification will return no-op values.

Compatibility Test Suite tests

The associated Android Compatibility Test Suite (CTS) tests can be found in:

cts/tests/tests/view/src/android/view/textclassifier/cts/TextClassificationManagerTest.java

cts/tests/tests/widget/src/android/widget/cts/TextViewTest.java