Android O スマートテキスト選択 ー TextClassifier、TextClassification、TextSelectionの動作検証


https://developer.android.com/preview/api-overview.html?hl=ja#a11y

```text
スマート テキスト選択
Android O では、互換性のある端末で、ユーザーがより有意義にテキストを利用できるようになります。
ユーザーが、エンティティの中の単語、つまり、住所やレストラン名など、認識されているフォーマット
の単語を長押しすると、システムによってエンティティ全体が選択されます。ユーザーには、
フローティング ツールバーが表示され、このツールバーは、選択されているテキスト エンティティを扱う
アプリを含むことができます。
例えば、システムが住所を認識した場合、マップ アプリへとユーザーを誘導します。

システムが認識するエンティティには、住所、URL、電話番号、メールアドレスなどが含まれます。
詳細については、TextClassifier をご覧ください。
```

https://japan.cnet.com/article/35101335/
![image.png](https://qiita-image-store.s3.amazonaws.com/0/38382/11f72252-aeae-ad94-202e-da82db43d69f.png)

内部的にTextClassifierというものが使われているようなので、TextClassifier
の動作についてエミュレーターで簡単な動作検証を行いました。

https://developer.android.com/reference/android/view/textclassifier/TextClassifier.html
https://developer.android.com/reference/android/view/textclassifier/TextClassification.html
https://developer.android.com/reference/android/view/textclassifier/TextSelection.html

```java
package com.example.user.textclassificationtext;

import android.app.Activity;
import android.content.Context;
import android.content.Entity;
import android.os.Handler;
import android.os.LocaleList;
import android.os.Bundle;
import android.util.Log;
import android.view.textclassifier.TextClassification;
import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextClassifier;
import android.view.textclassifier.TextSelection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends Activity {
    public static final String TAG = "TextClassificationText";
    private Handler handler= new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextClassificationManager tcm = (TextClassificationManager)getSystemService(TextClassificationManager.class);
        final TextClassifier tc = tcm.getTextClassifier();

        final String phoneNumber = "090-1111-1111";
        final String url = "https://www.google.co.jp/";
        final String email = "testes@gmail.com";
        final String address = "Google Inc, 12396 Grant St, Thornton, CO 80241 USA";
        final String address2 = "Mr. John Brown, 2352 E. Evans Ave., Denver Colo 80210-3310, USA";
        final String address3 = "東京都新宿区西新宿２丁目８−１";
        final String other = "This is an apple";
        final String other2 = "Please call at " + phoneNumber;
        final List<String> texts = new ArrayList<>(Arrays.asList(phoneNumber, url, email, address, address2, address3, other, other2));

        handler.post(new Runnable() {
            @Override
            public void run() {
                // TextClassification#classifyText
                for (String text : texts) {
                    TextClassification result = tc.classifyText(text, 0, text.length(), LocaleList.getDefault());
                    int entCount = result.getEntityCount();
                    Log.d(TAG, "********************************");
                    Log.d(TAG, "Text : " + text);
                    Log.d(TAG, "EntityCount : " + String.valueOf(entCount));
                    for (int i = 0; i < entCount; i++) {
                        String ent = result.getEntity(i);
                        Log.d(TAG, "Entity : " + ent + ", Precision : " + result.getConfidenceScore(ent) + ", Classified Text : " + result.getText());
                    }
                }
                Log.d(TAG, " ");
                // TextSelection.suggestSelection
                for (String text : texts) {
                    TextSelection result = tc.suggestSelection(text, 0, text.length(), LocaleList.getDefault());
                    int entCount = result.getEntityCount();
                    Log.d(TAG, "********************************");
                    Log.d(TAG, "Text : " + text);
                    Log.d(TAG, "EntityCount : " + String.valueOf(entCount));
                    for (int i = 0; i < entCount; i++) {
                        String ent = result.getEntity(i);
                        Log.d(TAG, "Selected Text : " + text.substring(result.getSelectionStartIndex(), result.getSelectionEndIndex()));
                        Log.d(TAG, "Entity : " + ent + ", Precision : " + result.getConfidenceScore(ent));
                    }
                }
            }
        });
    }
}
```
TextClassification#classifyText

```text
08-05 12:43:49.397: D/TextClassificationText(3957): run()
08-05 12:43:49.417: D/TextClassificationText(3957): ********************************
08-05 12:43:49.417: D/TextClassificationText(3957): Text : 090-1111-1111
08-05 12:43:49.417: D/TextClassificationText(3957): EntityCount : 3
08-05 12:43:49.418: D/TextClassificationText(3957): Entity : phone, Precision : 0.98711896, Classified Text : 090-1111-1111
08-05 12:43:49.418: D/TextClassificationText(3957): Entity : other, Precision : 0.011957903, Classified Text : 090-1111-1111
08-05 12:43:49.419: D/TextClassificationText(3957): Entity : address, Precision : 9.231136E-4, Classified Text : 090-1111-1111
08-05 12:43:49.432: D/TextClassificationText(3957): ********************************
08-05 12:43:49.432: D/TextClassificationText(3957): Text : https://www.google.co.jp/
08-05 12:43:49.432: D/TextClassificationText(3957): EntityCount : 1
08-05 12:43:49.432: D/TextClassificationText(3957): Entity : url, Precision : 1.0, Classified Text : https://www.google.co.jp/
08-05 12:43:49.451: D/TextClassificationText(3957): ********************************
08-05 12:43:49.451: D/TextClassificationText(3957): Text : testes@gmail.com
08-05 12:43:49.451: D/TextClassificationText(3957): EntityCount : 1
08-05 12:43:49.451: D/TextClassificationText(3957): Entity : email, Precision : 1.0, Classified Text : testes@gmail.com
08-05 12:43:49.464: D/TextClassificationText(3957): ********************************
08-05 12:43:49.464: D/TextClassificationText(3957): Text : Google Inc, 12396 Grant St, Thornton, CO 80241 USA
08-05 12:43:49.464: D/TextClassificationText(3957): EntityCount : 2
08-05 12:43:49.464: D/TextClassificationText(3957): Entity : address, Precision : 0.6283806, Classified Text : Google Inc, 12396 Grant St, Thornton, CO 80241 USA
08-05 12:43:49.464: D/TextClassificationText(3957): Entity : other, Precision : 0.37161946, Classified Text : Google Inc, 12396 Grant St, Thornton, CO 80241 USA
08-05 12:43:49.466: D/TextClassificationText(3957): ********************************
08-05 12:43:49.467: D/TextClassificationText(3957): Text : Mr. John Brown, 2352 E. Evans Ave., Denver Colo 80210-3310, USA
08-05 12:43:49.467: D/TextClassificationText(3957): EntityCount : 2
08-05 12:43:49.467: D/TextClassificationText(3957): Entity : address, Precision : 0.6001622, Classified Text : Mr. John Brown, 2352 E. Evans Ave., Denver Colo 80210-3310, USA
08-05 12:43:49.467: D/TextClassificationText(3957): Entity : other, Precision : 0.39983782, Classified Text : Mr. John Brown, 2352 E. Evans Ave., Denver Colo 80210-3310, USA
08-05 12:43:49.468: D/TextClassificationText(3957): ********************************
08-05 12:43:49.468: D/TextClassificationText(3957): Text : 東京都新宿区西新宿２丁目８−１
08-05 12:43:49.468: D/TextClassificationText(3957): EntityCount : 0
08-05 12:43:49.469: D/TextClassificationText(3957): ********************************
08-05 12:43:49.469: D/TextClassificationText(3957): Text : This is an apple
08-05 12:43:49.469: D/TextClassificationText(3957): EntityCount : 2
08-05 12:43:49.469: D/TextClassificationText(3957): Entity : other, Precision : 0.99918395, Classified Text : This is an apple
08-05 12:43:49.469: D/TextClassificationText(3957): Entity : address, Precision : 8.160865E-4, Classified Text : This is an apple
08-05 12:43:49.470: D/TextClassificationText(3957): ********************************
08-05 12:43:49.471: D/TextClassificationText(3957): Text : Please call at 090-1111-1111
08-05 12:43:49.471: D/TextClassificationText(3957): EntityCount : 2
08-05 12:43:49.471: D/TextClassificationText(3957): Entity : other, Precision : 0.99637246, Classified Text : Please call at 090-1111-1111
08-05 12:43:49.471: D/TextClassificationText(3957): Entity : address, Precision : 0.003627508, Classified Text : Please call at 090-1111-1111

```
TextSelection.suggestSelection

```text
08-05 12:43:49.471: D/TextClassificationText(3957):   
08-05 12:43:49.473: D/TextClassificationText(3957): ********************************
08-05 12:43:49.473: D/TextClassificationText(3957): Text : 090-1111-1111
08-05 12:43:49.473: D/TextClassificationText(3957): EntityCount : 3
08-05 12:43:49.473: D/TextClassificationText(3957): Telected Text : 090-1111-1111
08-05 12:43:49.473: D/TextClassificationText(3957): Entity : phone, Precision : 0.98711896
08-05 12:43:49.473: D/TextClassificationText(3957): Telected Text : 090-1111-1111
08-05 12:43:49.473: D/TextClassificationText(3957): Entity : other, Precision : 0.011957903
08-05 12:43:49.473: D/TextClassificationText(3957): Telected Text : 090-1111-1111
08-05 12:43:49.473: D/TextClassificationText(3957): Entity : address, Precision : 9.231136E-4
08-05 12:43:49.474: D/TextClassificationText(3957): ********************************
08-05 12:43:49.474: D/TextClassificationText(3957): Text : https://www.google.co.jp/
08-05 12:43:49.474: D/TextClassificationText(3957): EntityCount : 0
08-05 12:43:49.474: D/TextClassificationText(3957): ********************************
08-05 12:43:49.475: D/TextClassificationText(3957): Text : testes@gmail.com
08-05 12:43:49.475: D/TextClassificationText(3957): EntityCount : 1
08-05 12:43:49.475: D/TextClassificationText(3957): Telected Text : testes@gmail.com
08-05 12:43:49.475: D/TextClassificationText(3957): Entity : email, Precision : 1.0
08-05 12:43:49.477: D/TextClassificationText(3957): ********************************
08-05 12:43:49.477: D/TextClassificationText(3957): Text : Google Inc, 12396 Grant St, Thornton, CO 80241 USA
08-05 12:43:49.477: D/TextClassificationText(3957): EntityCount : 0
08-05 12:43:49.479: D/TextClassificationText(3957): ********************************
08-05 12:43:49.479: D/TextClassificationText(3957): Text : Mr. John Brown, 2352 E. Evans Ave., Denver Colo 80210-3310, USA
08-05 12:43:49.479: D/TextClassificationText(3957): EntityCount : 0
08-05 12:43:49.480: D/TextClassificationText(3957): ********************************
08-05 12:43:49.480: D/TextClassificationText(3957): Text : 東京都新宿区西新宿２丁目８−１
08-05 12:43:49.480: D/TextClassificationText(3957): EntityCount : 0
08-05 12:43:49.481: D/TextClassificationText(3957): ********************************
08-05 12:43:49.481: D/TextClassificationText(3957): Text : This is an apple
08-05 12:43:49.481: D/TextClassificationText(3957): EntityCount : 0
08-05 12:43:49.481: D/TextClassificationText(3957): ********************************
08-05 12:43:49.481: D/TextClassificationText(3957): Text : Please call at 090-1111-1111
08-05 12:43:49.481: D/TextClassificationText(3957): EntityCount : 0
```