1 /* 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 17 package android.view.textclassifier; 18 19 import android.content.res.AssetFileDescriptor; 20 21 /** 22 * Java wrapper for TextClassifier native library interface. This library is used for detecting 23 * entities in text. 24 */ 25 final class TextClassifierImplNative { 26 27 static { 28 System.loadLibrary("textclassifier"); 29 } 30 31 private final long mModelPtr; 32 33 /** 34 * Creates a new instance of TextClassifierImplNative, using the provided model image, given as 35 * a file descriptor. 36 */ 37 TextClassifierImplNative(int fd) { 38 mModelPtr = nativeNew(fd); 39 if (mModelPtr == 0L) { 40 throw new IllegalArgumentException("Couldn't initialize TC from file descriptor."); 41 } 42 } 43 44 /** 45 * Creates a new instance of TextClassifierImplNative, using the provided model image, given as 46 * a file path. 47 */ 48 TextClassifierImplNative(String path) { 49 mModelPtr = nativeNewFromPath(path); 50 if (mModelPtr == 0L) { 51 throw new IllegalArgumentException("Couldn't initialize TC from given file."); 52 } 53 } 54 55 /** 56 * Creates a new instance of TextClassifierImplNative, using the provided model image, given as 57 * an AssetFileDescriptor. 58 */ 59 TextClassifierImplNative(AssetFileDescriptor afd) { 60 mModelPtr = nativeNewFromAssetFileDescriptor(afd, afd.getStartOffset(), afd.getLength()); 61 if (mModelPtr == 0L) { 62 throw new IllegalArgumentException( 63 "Couldn't initialize TC from given AssetFileDescriptor"); 64 } 65 } 66 67 /** 68 * Given a string context and current selection, computes the SmartSelection suggestion. 69 * 70 * <p>The begin and end are character indices into the context UTF8 string. selectionBegin is 71 * the character index where the selection begins, and selectionEnd is the index of one 72 * character past the selection span. 73 * 74 * <p>The return value is an array of two ints: suggested selection beginning and end, with the 75 * same semantics as the input selectionBeginning and selectionEnd. 76 */ 77 public int[] suggestSelection( 78 String context, int selectionBegin, int selectionEnd, SelectionOptions options) { 79 return nativeSuggestSelection(mModelPtr, context, selectionBegin, selectionEnd, options); 80 } 81 82 /** 83 * Given a string context and current selection, classifies the type of the selected text. 84 * 85 * <p>The begin and end params are character indices in the context string. 86 * 87 * <p>Returns an array of ClassificationResult objects with the probability scores for different 88 * collections. 89 */ 90 public ClassificationResult[] classifyText( 91 String context, int selectionBegin, int selectionEnd, ClassificationOptions options) { 92 return nativeClassifyText(mModelPtr, context, selectionBegin, selectionEnd, options); 93 } 94 95 /** 96 * Annotates given input text. The annotations should cover the whole input context except for 97 * whitespaces, and are sorted by their position in the context string. 98 */ 99 public AnnotatedSpan[] annotate(String text, AnnotationOptions options) { 100 return nativeAnnotate(mModelPtr, text, options); 101 } 102 103 /** Frees up the allocated memory. */ 104 public void close() { 105 nativeClose(mModelPtr); 106 } 107 108 /** Returns a comma separated list of locales supported by the model as BCP 47 tags. */ 109 public static String getLocales(int fd) { 110 return nativeGetLocales(fd); 111 } 112 113 /** Returns the version of the model. */ 114 public static int getVersion(int fd) { 115 return nativeGetVersion(fd); 116 } 117 118 /** Represents a datetime parsing result from classifyText calls. */ 119 public static final class DatetimeResult { 120 static final int GRANULARITY_YEAR = 0; 121 static final int GRANULARITY_MONTH = 1; 122 static final int GRANULARITY_WEEK = 2; 123 static final int GRANULARITY_DAY = 3; 124 static final int GRANULARITY_HOUR = 4; 125 static final int GRANULARITY_MINUTE = 5; 126 static final int GRANULARITY_SECOND = 6; 127 128 private final long mTimeMsUtc; 129 private final int mGranularity; 130 131 DatetimeResult(long timeMsUtc, int granularity) { 132 mGranularity = granularity; 133 mTimeMsUtc = timeMsUtc; 134 } 135 136 public long getTimeMsUtc() { 137 return mTimeMsUtc; 138 } 139 140 public int getGranularity() { 141 return mGranularity; 142 } 143 } 144 145 /** Represents a result of classifyText method call. */ 146 public static final class ClassificationResult { 147 private final String mCollection; 148 private final float mScore; 149 private final DatetimeResult mDatetimeResult; 150 151 ClassificationResult( 152 String collection, float score, DatetimeResult datetimeResult) { 153 mCollection = collection; 154 mScore = score; 155 mDatetimeResult = datetimeResult; 156 } 157 158 public String getCollection() { 159 if (mCollection.equals(TextClassifier.TYPE_DATE) && mDatetimeResult != null) { 160 switch (mDatetimeResult.getGranularity()) { 161 case DatetimeResult.GRANULARITY_HOUR: 162 // fall through 163 case DatetimeResult.GRANULARITY_MINUTE: 164 // fall through 165 case DatetimeResult.GRANULARITY_SECOND: 166 return TextClassifier.TYPE_DATE_TIME; 167 default: 168 return TextClassifier.TYPE_DATE; 169 } 170 } 171 return mCollection; 172 } 173 174 public float getScore() { 175 return mScore; 176 } 177 178 public DatetimeResult getDatetimeResult() { 179 return mDatetimeResult; 180 } 181 } 182 183 /** Represents a result of Annotate call. */ 184 public static final class AnnotatedSpan { 185 private final int mStartIndex; 186 private final int mEndIndex; 187 private final ClassificationResult[] mClassification; 188 189 AnnotatedSpan( 190 int startIndex, int endIndex, ClassificationResult[] classification) { 191 mStartIndex = startIndex; 192 mEndIndex = endIndex; 193 mClassification = classification; 194 } 195 196 public int getStartIndex() { 197 return mStartIndex; 198 } 199 200 public int getEndIndex() { 201 return mEndIndex; 202 } 203 204 public ClassificationResult[] getClassification() { 205 return mClassification; 206 } 207 } 208 209 /** Represents options for the suggestSelection call. */ 210 public static final class SelectionOptions { 211 private final String mLocales; 212 213 SelectionOptions(String locales) { 214 mLocales = locales; 215 } 216 217 public String getLocales() { 218 return mLocales; 219 } 220 } 221 222 /** Represents options for the classifyText call. */ 223 public static final class ClassificationOptions { 224 private final long mReferenceTimeMsUtc; 225 private final String mReferenceTimezone; 226 private final String mLocales; 227 228 ClassificationOptions(long referenceTimeMsUtc, String referenceTimezone, String locale) { 229 mReferenceTimeMsUtc = referenceTimeMsUtc; 230 mReferenceTimezone = referenceTimezone; 231 mLocales = locale; 232 } 233 234 public long getReferenceTimeMsUtc() { 235 return mReferenceTimeMsUtc; 236 } 237 238 public String getReferenceTimezone() { 239 return mReferenceTimezone; 240 } 241 242 public String getLocale() { 243 return mLocales; 244 } 245 } 246 247 /** Represents options for the Annotate call. */ 248 public static final class AnnotationOptions { 249 private final long mReferenceTimeMsUtc; 250 private final String mReferenceTimezone; 251 private final String mLocales; 252 253 AnnotationOptions(long referenceTimeMsUtc, String referenceTimezone, String locale) { 254 mReferenceTimeMsUtc = referenceTimeMsUtc; 255 mReferenceTimezone = referenceTimezone; 256 mLocales = locale; 257 } 258 259 public long getReferenceTimeMsUtc() { 260 return mReferenceTimeMsUtc; 261 } 262 263 public String getReferenceTimezone() { 264 return mReferenceTimezone; 265 } 266 267 public String getLocale() { 268 return mLocales; 269 } 270 } 271 272 private static native long nativeNew(int fd); 273 274 private static native long nativeNewFromPath(String path); 275 276 private static native long nativeNewFromAssetFileDescriptor( 277 AssetFileDescriptor afd, long offset, long size); 278 279 private static native int[] nativeSuggestSelection( 280 long context, 281 String text, 282 int selectionBegin, 283 int selectionEnd, 284 SelectionOptions options); 285 286 private static native ClassificationResult[] nativeClassifyText( 287 long context, 288 String text, 289 int selectionBegin, 290 int selectionEnd, 291 ClassificationOptions options); 292 293 private static native AnnotatedSpan[] nativeAnnotate( 294 long context, String text, AnnotationOptions options); 295 296 private static native void nativeClose(long context); 297 298 private static native String nativeGetLocales(int fd); 299 300 private static native int nativeGetVersion(int fd); 301 } 302