1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view.textclassifier;
import android.annotation.FloatRange;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.icu.util.ULocale;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.util.ArrayMap;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;
import java.util.Locale;
import java.util.Map;
/**
* Represents the result of language detection of a piece of text.
* <p>
* This contains a list of locales, each paired with a confidence score, sorted in decreasing
* order of those scores. E.g., for a given input text, the model may return
* {@code [<"en", 0.85>, <"fr", 0.15>]}. This sample result means the model reports that it is
* 85% likely that the entire text is in English and 15% likely that the entire text is in French,
* etc. It does not mean that 85% of the input is in English and 15% is in French.
*/
public final class TextLanguage implements Parcelable {
public static final @android.annotation.NonNull Creator<TextLanguage> CREATOR = new Creator<TextLanguage>() {
@Override
public TextLanguage createFromParcel(Parcel in) {
return readFromParcel(in);
}
@Override
public TextLanguage[] newArray(int size) {
return new TextLanguage[size];
}
};
static final TextLanguage EMPTY = new Builder().build();
@Nullable private final String mId;
private final EntityConfidence mEntityConfidence;
private final Bundle mBundle;
private TextLanguage(
@Nullable String id,
EntityConfidence entityConfidence,
Bundle bundle) {
mId = id;
mEntityConfidence = entityConfidence;
mBundle = bundle;
}
/**
* Returns the id, if one exists, for this object.
*/
@Nullable
public String getId() {
return mId;
}
/**
* Returns the number of possible locales for the processed text.
*/
@IntRange(from = 0)
public int getLocaleHypothesisCount() {
return mEntityConfidence.getEntities().size();
}
/**
* Returns the language locale at the specified index. Locales are ordered from high
* confidence to low confidence.
* <p>
* See {@link #getLocaleHypothesisCount()} for the number of locales available.
*
* @throws IndexOutOfBoundsException if the specified index is out of range.
*/
@NonNull
public ULocale getLocale(int index) {
return ULocale.forLanguageTag(mEntityConfidence.getEntities().get(index));
}
/**
* Returns the confidence score for the specified language locale. The value ranges from
* 0 (low confidence) to 1 (high confidence). 0 indicates that the locale was not found for
* the processed text.
*/
@FloatRange(from = 0.0, to = 1.0)
public float getConfidenceScore(@NonNull ULocale locale) {
return mEntityConfidence.getConfidenceScore(locale.toLanguageTag());
}
/**
* Returns a bundle containing non-structured extra information about this result. What is
* returned in the extras is specific to the {@link TextClassifier} implementation.
*
* <p><b>NOTE: </b>Do not modify this bundle.
*/
@NonNull
public Bundle getExtras() {
return mBundle;
}
@Override
public String toString() {
return String.format(
Locale.US,
"TextLanguage {id=%s, locales=%s, bundle=%s}",
mId, mEntityConfidence, mBundle);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mId);
mEntityConfidence.writeToParcel(dest, flags);
dest.writeBundle(mBundle);
}
private static TextLanguage readFromParcel(Parcel in) {
return new TextLanguage(
in.readString(),
EntityConfidence.CREATOR.createFromParcel(in),
in.readBundle());
}
/**
* Builder used to build TextLanguage objects.
*/
public static final class Builder {
@Nullable private String mId;
private final Map<String, Float> mEntityConfidenceMap = new ArrayMap<>();
@Nullable private Bundle mBundle;
/**
* Sets a language locale for the processed text and assigns a confidence score. If the
* locale has already been set, this updates it.
*
* @param confidenceScore a value from 0 (low confidence) to 1 (high confidence).
* 0 implies the locale does not exist for the processed text.
* Values greater than 1 are clamped to 1.
*/
@NonNull
public Builder putLocale(
@NonNull ULocale locale,
@FloatRange(from = 0.0, to = 1.0) float confidenceScore) {
Preconditions.checkNotNull(locale);
mEntityConfidenceMap.put(locale.toLanguageTag(), confidenceScore);
return this;
}
/**
* Sets an optional id for the TextLanguage object.
*/
@NonNull
public Builder setId(@Nullable String id) {
mId = id;
return this;
}
/**
* Sets a bundle containing non-structured extra information about the TextLanguage object.
*/
@NonNull
public Builder setExtras(@NonNull Bundle bundle) {
mBundle = Preconditions.checkNotNull(bundle);
return this;
}
/**
* Builds and returns a new TextLanguage object.
* <p>
* If necessary, this method will verify fields, clamp them, and make them immutable.
*/
@NonNull
public TextLanguage build() {
mBundle = mBundle == null ? Bundle.EMPTY : mBundle;
return new TextLanguage(
mId,
new EntityConfidence(mEntityConfidenceMap),
mBundle);
}
}
/**
* A request object for detecting the language of a piece of text.
*/
public static final class Request implements Parcelable {
public static final @android.annotation.NonNull Creator<Request> CREATOR = new Creator<Request>() {
@Override
public Request createFromParcel(Parcel in) {
return readFromParcel(in);
}
@Override
public Request[] newArray(int size) {
return new Request[size];
}
};
private final CharSequence mText;
private final Bundle mExtra;
@Nullable private String mCallingPackageName;
@UserIdInt
private int mUserId = UserHandle.USER_NULL;
private Request(CharSequence text, Bundle bundle) {
mText = text;
mExtra = bundle;
}
/**
* Returns the text to process.
*/
@NonNull
public CharSequence getText() {
return mText;
}
/**
* Sets the name of the package that is sending this request.
* Package-private for SystemTextClassifier's use.
* @hide
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public void setCallingPackageName(@Nullable String callingPackageName) {
mCallingPackageName = callingPackageName;
}
/**
* Returns the name of the package that sent this request.
* This returns null if no calling package name is set.
*/
@Nullable
public String getCallingPackageName() {
return mCallingPackageName;
}
/**
* Sets the id of the user that sent this request.
* <p>
* Package-private for SystemTextClassifier's use.
*/
void setUserId(@UserIdInt int userId) {
mUserId = userId;
}
/**
* Returns the id of the user that sent this request.
* @hide
*/
@UserIdInt
public int getUserId() {
return mUserId;
}
/**
* Returns a bundle containing non-structured extra information about this request.
*
* <p><b>NOTE: </b>Do not modify this bundle.
*/
@NonNull
public Bundle getExtras() {
return mExtra;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeCharSequence(mText);
dest.writeString(mCallingPackageName);
dest.writeInt(mUserId);
dest.writeBundle(mExtra);
}
private static Request readFromParcel(Parcel in) {
final CharSequence text = in.readCharSequence();
final String callingPackageName = in.readString();
final int userId = in.readInt();
final Bundle extra = in.readBundle();
final Request request = new Request(text, extra);
request.setCallingPackageName(callingPackageName);
request.setUserId(userId);
return request;
}
/**
* A builder for building TextLanguage requests.
*/
public static final class Builder {
private final CharSequence mText;
@Nullable private Bundle mBundle;
/**
* Creates a builder to build TextLanguage requests.
*
* @param text the text to process.
*/
public Builder(@NonNull CharSequence text) {
mText = Preconditions.checkNotNull(text);
}
/**
* Sets a bundle containing non-structured extra information about the request.
*/
@NonNull
public Builder setExtras(@NonNull Bundle bundle) {
mBundle = Preconditions.checkNotNull(bundle);
return this;
}
/**
* Builds and returns a new TextLanguage request object.
* <p>
* If necessary, this method will verify fields, clamp them, and make them immutable.
*/
@NonNull
public Request build() {
return new Request(mText.toString(), mBundle == null ? Bundle.EMPTY : mBundle);
}
}
}
}
|