1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.textclassifier; 18 19 import android.app.RemoteAction; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.view.textclassifier.TextClassification; 23 import android.view.textclassifier.TextClassifier; 24 import android.view.textclassifier.TextLinks; 25 import androidx.core.util.Pair; 26 import com.google.android.textclassifier.AnnotatorModel; 27 import com.google.common.annotations.VisibleForTesting; 28 import java.util.ArrayList; 29 import java.util.List; 30 import javax.annotation.Nullable; 31 32 /** Utility class for inserting and retrieving data in TextClassifier request/response extras. */ 33 // TODO: Make this a TestApi for CTS testing. 34 public final class ExtrasUtils { 35 36 // Keys for response objects. 37 private static final String SERIALIZED_ENTITIES_DATA = "serialized-entities-data"; 38 private static final String ENTITIES_EXTRAS = "entities-extras"; 39 private static final String ACTION_INTENT = "action-intent"; 40 private static final String ACTIONS_INTENTS = "actions-intents"; 41 private static final String FOREIGN_LANGUAGE = "foreign-language"; 42 private static final String ENTITY_TYPE = "entity-type"; 43 private static final String SCORE = "score"; 44 private static final String MODEL_VERSION = "model-version"; 45 private static final String MODEL_NAME = "model-name"; 46 private static final String TEXT_LANGUAGES = "text-languages"; 47 private static final String ENTITIES = "entities"; 48 49 // Keys for request objects. 50 private static final String IS_SERIALIZED_ENTITY_DATA_ENABLED = 51 "is-serialized-entity-data-enabled"; 52 ExtrasUtils()53 private ExtrasUtils() {} 54 55 /** Bundles and returns foreign language detection information for TextClassifier responses. */ createForeignLanguageExtra(String language, float score, int modelVersion)56 static Bundle createForeignLanguageExtra(String language, float score, int modelVersion) { 57 final Bundle bundle = new Bundle(); 58 bundle.putString(ENTITY_TYPE, language); 59 bundle.putFloat(SCORE, score); 60 bundle.putInt(MODEL_VERSION, modelVersion); 61 bundle.putString(MODEL_NAME, "langId_v" + modelVersion); 62 return bundle; 63 } 64 65 /** 66 * Stores {@code extra} as foreign language information in TextClassifier response object's extras 67 * {@code container}. 68 * 69 * @see #getForeignLanguageExtra(TextClassification) 70 */ putForeignLanguageExtra(Bundle container, Bundle extra)71 static void putForeignLanguageExtra(Bundle container, Bundle extra) { 72 container.putParcelable(FOREIGN_LANGUAGE, extra); 73 } 74 75 /** 76 * Returns foreign language detection information contained in the TextClassification object. 77 * responses. 78 * 79 * @see #putForeignLanguageExtra(Bundle, Bundle) 80 */ 81 @Nullable 82 @VisibleForTesting getForeignLanguageExtra(@ullable TextClassification classification)83 public static Bundle getForeignLanguageExtra(@Nullable TextClassification classification) { 84 if (classification == null) { 85 return null; 86 } 87 return classification.getExtras().getBundle(FOREIGN_LANGUAGE); 88 } 89 90 /** 91 * @see #getTopLanguage(Intent) 92 */ putTopLanguageScores(Bundle container, EntityConfidence languageScores)93 static void putTopLanguageScores(Bundle container, EntityConfidence languageScores) { 94 final int maxSize = Math.min(3, languageScores.getEntities().size()); 95 final String[] languages = 96 languageScores.getEntities().subList(0, maxSize).toArray(new String[0]); 97 final float[] scores = new float[languages.length]; 98 for (int i = 0; i < languages.length; i++) { 99 scores[i] = languageScores.getConfidenceScore(languages[i]); 100 } 101 container.putStringArray(ENTITY_TYPE, languages); 102 container.putFloatArray(SCORE, scores); 103 } 104 105 /** See {@link #putTopLanguageScores(Bundle, EntityConfidence)}. */ 106 @Nullable getTopLanguage(@ullable Intent intent)107 static Pair<String, Float> getTopLanguage(@Nullable Intent intent) { 108 if (intent == null) { 109 return null; 110 } 111 final Bundle tcBundle = intent.getBundleExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER); 112 if (tcBundle == null) { 113 return null; 114 } 115 final Bundle textLanguagesExtra = tcBundle.getBundle(TEXT_LANGUAGES); 116 if (textLanguagesExtra == null) { 117 return null; 118 } 119 final String[] languages = textLanguagesExtra.getStringArray(ENTITY_TYPE); 120 final float[] scores = textLanguagesExtra.getFloatArray(SCORE); 121 if (languages == null 122 || scores == null 123 || languages.length == 0 124 || languages.length != scores.length) { 125 return null; 126 } 127 int highestScoringIndex = 0; 128 for (int i = 1; i < languages.length; i++) { 129 if (scores[highestScoringIndex] < scores[i]) { 130 highestScoringIndex = i; 131 } 132 } 133 return Pair.create(languages[highestScoringIndex], scores[highestScoringIndex]); 134 } 135 putTextLanguagesExtra(Bundle container, Bundle extra)136 public static void putTextLanguagesExtra(Bundle container, Bundle extra) { 137 container.putBundle(TEXT_LANGUAGES, extra); 138 } 139 140 /** 141 * Stores {@code actionsIntents} information in TextClassifier response object's extras {@code 142 * container}. 143 */ putActionsIntents(Bundle container, ArrayList<Intent> actionsIntents)144 static void putActionsIntents(Bundle container, ArrayList<Intent> actionsIntents) { 145 container.putParcelableArrayList(ACTIONS_INTENTS, actionsIntents); 146 } 147 148 /** 149 * Stores {@code actionIntent} information in TextClassifier response object's extras {@code 150 * container}. 151 */ putActionIntent(Bundle container, @Nullable Intent actionIntent)152 public static void putActionIntent(Bundle container, @Nullable Intent actionIntent) { 153 container.putParcelable(ACTION_INTENT, actionIntent); 154 } 155 156 /** Returns {@code actionIntent} information contained in a TextClassifier response object. */ 157 @Nullable getActionIntent(Bundle container)158 public static Intent getActionIntent(Bundle container) { 159 return container.getParcelable(ACTION_INTENT); 160 } 161 162 /** 163 * Stores serialized entity data information in TextClassifier response object's extras {@code 164 * container}. 165 */ putSerializedEntityData( Bundle container, @Nullable byte[] serializedEntityData)166 public static void putSerializedEntityData( 167 Bundle container, @Nullable byte[] serializedEntityData) { 168 container.putByteArray(SERIALIZED_ENTITIES_DATA, serializedEntityData); 169 } 170 171 /** Returns serialized entity data information contained in a TextClassifier response object. */ 172 @Nullable getSerializedEntityData(Bundle container)173 public static byte[] getSerializedEntityData(Bundle container) { 174 return container.getByteArray(SERIALIZED_ENTITIES_DATA); 175 } 176 177 /** 178 * Stores {@code entities} information in TextClassifier response object's extras {@code 179 * container}. 180 * 181 * @see {@link #getCopyText(Bundle)} 182 */ putEntitiesExtras(Bundle container, @Nullable Bundle entitiesExtras)183 public static void putEntitiesExtras(Bundle container, @Nullable Bundle entitiesExtras) { 184 container.putParcelable(ENTITIES_EXTRAS, entitiesExtras); 185 } 186 187 /** 188 * Returns {@code entities} information contained in a TextClassifier response object. 189 * 190 * @see {@link #putEntitiesExtras(Bundle, Bundle)} 191 */ 192 @Nullable getCopyText(Bundle container)193 public static String getCopyText(Bundle container) { 194 Bundle entitiesExtras = container.getParcelable(ENTITIES_EXTRAS); 195 if (entitiesExtras == null) { 196 return null; 197 } 198 return entitiesExtras.getString("text"); 199 } 200 201 /** Returns {@code actionIntents} information contained in the TextClassification object. */ 202 @Nullable getActionsIntents(@ullable TextClassification classification)203 public static ArrayList<Intent> getActionsIntents(@Nullable TextClassification classification) { 204 if (classification == null) { 205 return null; 206 } 207 return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS); 208 } 209 210 /** 211 * Returns the first action found in the {@code classification} object with an intent action 212 * string, {@code intentAction}. 213 */ 214 @Nullable 215 @VisibleForTesting findAction( @ullable TextClassification classification, @Nullable String intentAction)216 public static RemoteAction findAction( 217 @Nullable TextClassification classification, @Nullable String intentAction) { 218 if (classification == null || intentAction == null) { 219 return null; 220 } 221 final ArrayList<Intent> actionIntents = getActionsIntents(classification); 222 if (actionIntents != null) { 223 final int size = actionIntents.size(); 224 for (int i = 0; i < size; i++) { 225 final Intent intent = actionIntents.get(i); 226 if (intent != null && intentAction.equals(intent.getAction())) { 227 return classification.getActions().get(i); 228 } 229 } 230 } 231 return null; 232 } 233 234 /** Returns the first "translate" action found in the {@code classification} object. */ 235 @Nullable 236 @VisibleForTesting findTranslateAction(@ullable TextClassification classification)237 public static RemoteAction findTranslateAction(@Nullable TextClassification classification) { 238 return findAction(classification, Intent.ACTION_TRANSLATE); 239 } 240 241 /** Returns the entity type contained in the {@code extra}. */ 242 @Nullable 243 @VisibleForTesting getEntityType(@ullable Bundle extra)244 public static String getEntityType(@Nullable Bundle extra) { 245 if (extra == null) { 246 return null; 247 } 248 return extra.getString(ENTITY_TYPE); 249 } 250 251 /** Returns the score contained in the {@code extra}. */ 252 @VisibleForTesting getScore(Bundle extra)253 public static float getScore(Bundle extra) { 254 final int defaultValue = -1; 255 if (extra == null) { 256 return defaultValue; 257 } 258 return extra.getFloat(SCORE, defaultValue); 259 } 260 261 /** Returns the model name contained in the {@code extra}. */ 262 @Nullable getModelName(@ullable Bundle extra)263 public static String getModelName(@Nullable Bundle extra) { 264 if (extra == null) { 265 return null; 266 } 267 return extra.getString(MODEL_NAME); 268 } 269 270 /** Stores the entities from {@link AnnotatorModel.ClassificationResult} in {@code container}. */ putEntities( Bundle container, @Nullable AnnotatorModel.ClassificationResult[] classifications)271 public static void putEntities( 272 Bundle container, @Nullable AnnotatorModel.ClassificationResult[] classifications) { 273 if (classifications == null || classifications.length == 0) { 274 return; 275 } 276 ArrayList<Bundle> entitiesBundle = new ArrayList<>(); 277 for (AnnotatorModel.ClassificationResult classification : classifications) { 278 if (classification == null) { 279 continue; 280 } 281 Bundle entityBundle = new Bundle(); 282 entityBundle.putString(ENTITY_TYPE, classification.getCollection()); 283 entityBundle.putByteArray(SERIALIZED_ENTITIES_DATA, classification.getSerializedEntityData()); 284 entitiesBundle.add(entityBundle); 285 } 286 if (!entitiesBundle.isEmpty()) { 287 container.putParcelableArrayList(ENTITIES, entitiesBundle); 288 } 289 } 290 291 /** Returns a list of entities contained in the {@code extra}. */ 292 @Nullable 293 @VisibleForTesting getEntities(Bundle container)294 public static List<Bundle> getEntities(Bundle container) { 295 return container.getParcelableArrayList(ENTITIES); 296 } 297 298 /** Whether the annotator should populate serialized entity data into the result object. */ isSerializedEntityDataEnabled(TextLinks.Request request)299 public static boolean isSerializedEntityDataEnabled(TextLinks.Request request) { 300 return request.getExtras().getBoolean(IS_SERIALIZED_ENTITY_DATA_ENABLED); 301 } 302 303 /** 304 * To indicate whether the annotator should populate serialized entity data in the result object. 305 */ 306 @VisibleForTesting putIsSerializedEntityDataEnabled(Bundle bundle, boolean isEnabled)307 public static void putIsSerializedEntityDataEnabled(Bundle bundle, boolean isEnabled) { 308 bundle.putBoolean(IS_SERIALIZED_ENTITY_DATA_ENABLED, isEnabled); 309 } 310 } 311