blob: fdc9f92347db0fe1fd9233ec2dd5e0728448a765 [file] [log] [blame]
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +00001/*
2 * Copyright (C) 2017 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
17package android.view.textclassifier;
18
19import android.annotation.IntRange;
20import android.annotation.NonNull;
Abodunrinwa Toki4cfda0b2017-02-28 18:56:47 +000021import android.annotation.Nullable;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000022import android.annotation.StringDef;
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +010023import android.annotation.WorkerThread;
Abodunrinwa Toki4cfda0b2017-02-28 18:56:47 +000024import android.os.LocaleList;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000025
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +000026import com.android.internal.util.Preconditions;
27
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
31/**
32 * Interface for providing text classification related features.
33 *
Abodunrinwa Toki33ff2002017-10-24 00:49:27 +010034 * <p>Unless otherwise stated, methods of this interface are blocking operations.
35 * Avoid calling them on the UI thread.
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000036 */
37public interface TextClassifier {
38
Abodunrinwa Toki692b1962017-08-15 15:05:11 +010039 /** @hide */
Abodunrinwa Toki008f3872017-11-27 19:32:35 +000040 String DEFAULT_LOG_TAG = "androidtc";
Abodunrinwa Toki692b1962017-08-15 15:05:11 +010041
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +000042 String TYPE_UNKNOWN = "";
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000043 String TYPE_OTHER = "other";
44 String TYPE_EMAIL = "email";
45 String TYPE_PHONE = "phone";
46 String TYPE_ADDRESS = "address";
Abodunrinwa Toki9b4c82a2017-02-06 20:29:36 +000047 String TYPE_URL = "url";
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000048
Abodunrinwa Toki45cb3e62017-03-29 21:51:45 +010049 /** @hide */
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000050 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkey5db9a912017-12-08 17:32:32 -070051 @StringDef(prefix = { "TYPE_" }, value = {
52 TYPE_UNKNOWN,
53 TYPE_OTHER,
54 TYPE_EMAIL,
55 TYPE_PHONE,
56 TYPE_ADDRESS,
57 TYPE_URL,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000058 })
59 @interface EntityType {}
60
61 /**
62 * No-op TextClassifier.
63 * This may be used to turn off TextClassifier features.
64 */
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +010065 TextClassifier NO_OP = new TextClassifier() {};
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000066
67 /**
Abodunrinwa Toki33ff2002017-10-24 00:49:27 +010068 * Returns suggested text selection start and end indices, recognized entity types, and their
69 * associated confidence scores. The entity types are ordered from highest to lowest scoring.
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000070 *
71 * @param text text providing context for the selected text (which is specified
72 * by the sub sequence starting at selectionStartIndex and ending at selectionEndIndex)
73 * @param selectionStartIndex start index of the selected part of text
74 * @param selectionEndIndex end index of the selected part of text
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +010075 * @param options optional input parameters
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000076 *
77 * @throws IllegalArgumentException if text is null; selectionStartIndex is negative;
Abodunrinwa Toki4cfda0b2017-02-28 18:56:47 +000078 * selectionEndIndex is greater than text.length() or not greater than selectionStartIndex
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +000079 *
80 * @see #suggestSelection(CharSequence, int, int)
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000081 */
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +010082 @WorkerThread
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000083 @NonNull
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +010084 default TextSelection suggestSelection(
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +000085 @NonNull CharSequence text,
86 @IntRange(from = 0) int selectionStartIndex,
Abodunrinwa Toki4cfda0b2017-02-28 18:56:47 +000087 @IntRange(from = 0) int selectionEndIndex,
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +010088 @Nullable TextSelection.Options options) {
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +000089 Utils.validateInput(text, selectionStartIndex, selectionEndIndex);
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +010090 return new TextSelection.Builder(selectionStartIndex, selectionEndIndex).build();
91 }
92
93 /**
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +000094 * Returns suggested text selection start and end indices, recognized entity types, and their
95 * associated confidence scores. The entity types are ordered from highest to lowest scoring.
96 *
97 * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
98 * {@link #suggestSelection(CharSequence, int, int, TextSelection.Options)}. If that method
99 * calls this method, a stack overflow error will happen.
100 *
101 * @param text text providing context for the selected text (which is specified
102 * by the sub sequence starting at selectionStartIndex and ending at selectionEndIndex)
103 * @param selectionStartIndex start index of the selected part of text
104 * @param selectionEndIndex end index of the selected part of text
105 *
106 * @throws IllegalArgumentException if text is null; selectionStartIndex is negative;
107 * selectionEndIndex is greater than text.length() or not greater than selectionStartIndex
108 *
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100109 * @see #suggestSelection(CharSequence, int, int, TextSelection.Options)
110 */
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000111 @WorkerThread
112 @NonNull
113 default TextSelection suggestSelection(
114 @NonNull CharSequence text,
115 @IntRange(from = 0) int selectionStartIndex,
116 @IntRange(from = 0) int selectionEndIndex) {
117 return suggestSelection(text, selectionStartIndex, selectionEndIndex,
118 (TextSelection.Options) null);
119 }
120
121 /**
122 * See {@link #suggestSelection(CharSequence, int, int)} or
123 * {@link #suggestSelection(CharSequence, int, int, TextSelection.Options)}.
124 *
125 * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
126 * {@link #suggestSelection(CharSequence, int, int, TextSelection.Options)}. If that method
127 * calls this method, a stack overflow error will happen.
128 */
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100129 @WorkerThread
130 @NonNull
131 default TextSelection suggestSelection(
132 @NonNull CharSequence text,
133 @IntRange(from = 0) int selectionStartIndex,
134 @IntRange(from = 0) int selectionEndIndex,
135 @Nullable LocaleList defaultLocales) {
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000136 final TextSelection.Options options = (defaultLocales != null)
137 ? new TextSelection.Options().setDefaultLocales(defaultLocales)
138 : null;
139 return suggestSelection(text, selectionStartIndex, selectionEndIndex, options);
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100140 }
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000141
142 /**
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100143 * Classifies the specified text and returns a {@link TextClassification} object that can be
144 * used to generate a widget for handling the classified text.
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000145 *
146 * @param text text providing context for the text to classify (which is specified
147 * by the sub sequence starting at startIndex and ending at endIndex)
148 * @param startIndex start index of the text to classify
149 * @param endIndex end index of the text to classify
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100150 * @param options optional input parameters
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000151 *
152 * @throws IllegalArgumentException if text is null; startIndex is negative;
Abodunrinwa Toki4cfda0b2017-02-28 18:56:47 +0000153 * endIndex is greater than text.length() or not greater than startIndex
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000154 *
155 * @see #classifyText(CharSequence, int, int)
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000156 */
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100157 @WorkerThread
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000158 @NonNull
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100159 default TextClassification classifyText(
Abodunrinwa Toki4cfda0b2017-02-28 18:56:47 +0000160 @NonNull CharSequence text,
161 @IntRange(from = 0) int startIndex,
162 @IntRange(from = 0) int endIndex,
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100163 @Nullable TextClassification.Options options) {
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000164 Utils.validateInput(text, startIndex, endIndex);
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100165 return TextClassification.EMPTY;
166 }
167
168 /**
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000169 * Classifies the specified text and returns a {@link TextClassification} object that can be
170 * used to generate a widget for handling the classified text.
171 *
172 * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
173 * {@link #classifyText(CharSequence, int, int, TextClassification.Options)}. If that method
174 * calls this method, a stack overflow error will happen.
175 *
176 * @param text text providing context for the text to classify (which is specified
177 * by the sub sequence starting at startIndex and ending at endIndex)
178 * @param startIndex start index of the text to classify
179 * @param endIndex end index of the text to classify
180 *
181 * @throws IllegalArgumentException if text is null; startIndex is negative;
182 * endIndex is greater than text.length() or not greater than startIndex
183 *
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100184 * @see #classifyText(CharSequence, int, int, TextClassification.Options)
185 */
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000186 @WorkerThread
187 @NonNull
188 default TextClassification classifyText(
189 @NonNull CharSequence text,
190 @IntRange(from = 0) int startIndex,
191 @IntRange(from = 0) int endIndex) {
192 return classifyText(text, startIndex, endIndex, (TextClassification.Options) null);
193 }
194
195 /**
196 * See {@link #classifyText(CharSequence, int, int, TextClassification.Options)} or
197 * {@link #classifyText(CharSequence, int, int)}.
198 *
199 * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
200 * {@link #classifyText(CharSequence, int, int, TextClassification.Options)}. If that method
201 * calls this method, a stack overflow error will happen.
202 */
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100203 @WorkerThread
204 @NonNull
205 default TextClassification classifyText(
206 @NonNull CharSequence text,
207 @IntRange(from = 0) int startIndex,
208 @IntRange(from = 0) int endIndex,
209 @Nullable LocaleList defaultLocales) {
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000210 final TextClassification.Options options = (defaultLocales != null)
211 ? new TextClassification.Options().setDefaultLocales(defaultLocales)
212 : null;
213 return classifyText(text, startIndex, endIndex, options);
Abodunrinwa Toki2b6020f2017-10-28 02:28:45 +0100214 }
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000215
216 /**
Richard Ledley68d94522017-10-05 10:52:19 +0100217 * Returns a {@link TextLinks} that may be applied to the text to annotate it with links
218 * information.
219 *
220 * @param text the text to generate annotations for
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000221 * @param options configuration for link generation
Richard Ledley68d94522017-10-05 10:52:19 +0100222 *
223 * @throws IllegalArgumentException if text is null
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000224 *
225 * @see #generateLinks(CharSequence)
Richard Ledley68d94522017-10-05 10:52:19 +0100226 */
227 @WorkerThread
228 default TextLinks generateLinks(
229 @NonNull CharSequence text, @Nullable TextLinks.Options options) {
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000230 Utils.validateInput(text);
Richard Ledley68d94522017-10-05 10:52:19 +0100231 return new TextLinks.Builder(text.toString()).build();
232 }
233
234 /**
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000235 * Returns a {@link TextLinks} that may be applied to the text to annotate it with links
236 * information.
237 *
238 * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
239 * {@link #generateLinks(CharSequence, TextLinks.Options)}. If that method calls this method,
240 * a stack overflow error will happen.
241 *
242 * @param text the text to generate annotations for
243 *
244 * @throws IllegalArgumentException if text is null
245 *
246 * @see #generateLinks(CharSequence, TextLinks.Options)
247 */
248 @WorkerThread
249 default TextLinks generateLinks(@NonNull CharSequence text) {
250 return generateLinks(text, null);
251 }
252
253 /**
Abodunrinwa Toki1d775572017-05-08 16:03:01 +0100254 * Logs a TextClassifier event.
255 *
256 * @param source the text classifier used to generate this event
257 * @param event the text classifier related event
258 * @hide
259 */
260 @WorkerThread
261 default void logEvent(String source, String event) {}
Abodunrinwa Toki0e6b43e2017-09-19 23:18:40 +0100262
263 /**
264 * Returns this TextClassifier's settings.
265 * @hide
266 */
267 default TextClassifierConstants getSettings() {
268 return TextClassifierConstants.DEFAULT;
269 }
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000270
271
272 /**
273 * Utility functions for TextClassifier methods.
274 *
275 * <ul>
276 * <li>Provides validation of input parameters to TextClassifier methods
277 * </ul>
278 *
279 * Intended to be used only in this package.
280 * @hide
281 */
282 final class Utils {
283
284 /**
285 * @throws IllegalArgumentException if text is null; startIndex is negative;
286 * endIndex is greater than text.length() or is not greater than startIndex;
287 * options is null
288 */
289 static void validateInput(
290 @NonNull CharSequence text, int startIndex, int endIndex) {
291 Preconditions.checkArgument(text != null);
292 Preconditions.checkArgument(startIndex >= 0);
293 Preconditions.checkArgument(endIndex <= text.length());
294 Preconditions.checkArgument(endIndex > startIndex);
295 }
296
297 /**
298 * @throws IllegalArgumentException if text is null or options is null
299 */
300 static void validateInput(@NonNull CharSequence text) {
301 Preconditions.checkArgument(text != null);
302 }
303 }
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000304}