• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.android.textclassifier;
18 
19 import java.util.Collection;
20 import java.util.concurrent.atomic.AtomicBoolean;
21 
22 /**
23  * Java wrapper for Annotator native library interface. This library is used for detecting entities
24  * in text.
25  *
26  * @hide
27  */
28 public final class AnnotatorModel implements AutoCloseable {
29   private final AtomicBoolean isClosed = new AtomicBoolean(false);
30 
31   static {
32     System.loadLibrary("textclassifier");
33   }
34 
35   // Keep these in sync with the constants defined in AOSP.
36   static final String TYPE_UNKNOWN = "";
37   static final String TYPE_OTHER = "other";
38   static final String TYPE_EMAIL = "email";
39   static final String TYPE_PHONE = "phone";
40   static final String TYPE_ADDRESS = "address";
41   static final String TYPE_URL = "url";
42   static final String TYPE_DATE = "date";
43   static final String TYPE_DATE_TIME = "datetime";
44   static final String TYPE_FLIGHT_NUMBER = "flight";
45 
46   public static final double INVALID_LATITUDE = 180;
47   public static final double INVALID_LONGITUDE = 360;
48   public static final float INVALID_LOCATION_ACCURACY_METERS = 0;
49 
50   private long annotatorPtr;
51   // To tell GC to keep the LangID model alive at least as long as this object.
52   private LangIdModel langIdModel;
53 
54   /** Enumeration for specifying the usecase of the annotations. */
55   public static enum AnnotationUsecase {
56     /** Results are optimized for Smart{Select,Share,Linkify}. */
57     SMART(0),
58 
59     /**
60      * Results are optimized for using TextClassifier as an infrastructure that annotates as much as
61      * possible.
62      */
63     RAW(1);
64 
65     private final int value;
66 
AnnotationUsecase(int value)67     AnnotationUsecase(int value) {
68       this.value = value;
69     }
70 
getValue()71     public int getValue() {
72       return value;
73     }
74   };
75 
76   /**
77    * Creates a new instance of SmartSelect predictor, using the provided model image, given as a
78    * file descriptor.
79    */
AnnotatorModel(int fileDescriptor)80   public AnnotatorModel(int fileDescriptor) {
81     annotatorPtr = nativeNewAnnotator(fileDescriptor);
82     if (annotatorPtr == 0L) {
83       throw new IllegalArgumentException("Couldn't initialize TC from file descriptor.");
84     }
85   }
86 
87   /**
88    * Creates a new instance of SmartSelect predictor, using the provided model image, given as a
89    * file path.
90    */
AnnotatorModel(String path)91   public AnnotatorModel(String path) {
92     annotatorPtr = nativeNewAnnotatorFromPath(path);
93     if (annotatorPtr == 0L) {
94       throw new IllegalArgumentException("Couldn't initialize TC from given file.");
95     }
96   }
97 
98   /** Initializes the knowledge engine, passing the given serialized config to it. */
initializeKnowledgeEngine(byte[] serializedConfig)99   public void initializeKnowledgeEngine(byte[] serializedConfig) {
100     if (!nativeInitializeKnowledgeEngine(annotatorPtr, serializedConfig)) {
101       throw new IllegalArgumentException("Couldn't initialize the KG engine");
102     }
103   }
104 
105   /** Initializes the contact engine, passing the given serialized config to it. */
initializeContactEngine(byte[] serializedConfig)106   public void initializeContactEngine(byte[] serializedConfig) {
107     if (!nativeInitializeContactEngine(annotatorPtr, serializedConfig)) {
108       throw new IllegalArgumentException("Couldn't initialize the contact engine");
109     }
110   }
111 
112   /** Initializes the installed app engine, passing the given serialized config to it. */
initializeInstalledAppEngine(byte[] serializedConfig)113   public void initializeInstalledAppEngine(byte[] serializedConfig) {
114     if (!nativeInitializeInstalledAppEngine(annotatorPtr, serializedConfig)) {
115       throw new IllegalArgumentException("Couldn't initialize the installed app engine");
116     }
117   }
118 
119   /**
120    * Sets the LangId model to the annotator. Do not call close on the given LangIdModel object
121    * before this object is closed. Also, this object does not take the memory ownership of the given
122    * LangIdModel object.
123    */
setLangIdModel(LangIdModel langIdModel)124   public void setLangIdModel(LangIdModel langIdModel) {
125     this.langIdModel = langIdModel;
126     nativeSetLangId(annotatorPtr, langIdModel == null ? 0 : langIdModel.getNativePointer());
127   }
128 
129   /**
130    * Given a string context and current selection, computes the selection suggestion.
131    *
132    * <p>The begin and end are character indices into the context UTF8 string. selectionBegin is the
133    * character index where the selection begins, and selectionEnd is the index of one character past
134    * the selection span.
135    *
136    * <p>The return value is an array of two ints: suggested selection beginning and end, with the
137    * same semantics as the input selectionBeginning and selectionEnd.
138    */
suggestSelection( String context, int selectionBegin, int selectionEnd, SelectionOptions options)139   public int[] suggestSelection(
140       String context, int selectionBegin, int selectionEnd, SelectionOptions options) {
141     return nativeSuggestSelection(annotatorPtr, context, selectionBegin, selectionEnd, options);
142   }
143 
144   /**
145    * Given a string context and current selection, classifies the type of the selected text.
146    *
147    * <p>The begin and end params are character indices in the context string.
148    *
149    * <p>Returns an array of ClassificationResult objects with the probability scores for different
150    * collections.
151    */
classifyText( String context, int selectionBegin, int selectionEnd, ClassificationOptions options)152   public ClassificationResult[] classifyText(
153       String context, int selectionBegin, int selectionEnd, ClassificationOptions options) {
154     return classifyText(
155         context,
156         selectionBegin,
157         selectionEnd,
158         options,
159         /*appContext=*/ null,
160         /*resourcesLocale=*/ null);
161   }
162 
classifyText( String context, int selectionBegin, int selectionEnd, ClassificationOptions options, Object appContext, String resourcesLocale)163   public ClassificationResult[] classifyText(
164       String context,
165       int selectionBegin,
166       int selectionEnd,
167       ClassificationOptions options,
168       Object appContext,
169       String resourcesLocale) {
170     return nativeClassifyText(
171         annotatorPtr, context, selectionBegin, selectionEnd, options, appContext, resourcesLocale);
172   }
173 
174   /**
175    * Annotates given input text. The annotations should cover the whole input context except for
176    * whitespaces, and are sorted by their position in the context string.
177    */
annotate(String text, AnnotationOptions options)178   public AnnotatedSpan[] annotate(String text, AnnotationOptions options) {
179     return nativeAnnotate(annotatorPtr, text, options);
180   }
181 
182   /**
183    * Annotates multiple fragments of text at once. There will be one AnnotatedSpan array for each
184    * input fragment to annotate.
185    */
annotateStructuredInput( InputFragment[] fragments, AnnotationOptions options)186   public AnnotatedSpan[][] annotateStructuredInput(
187       InputFragment[] fragments, AnnotationOptions options) {
188     return nativeAnnotateStructuredInput(annotatorPtr, fragments, options);
189   }
190 
191   /**
192    * Looks up a knowledge entity by its identifier. Returns null if the entity is not found or on
193    * error.
194    */
lookUpKnowledgeEntity(String id)195   public byte[] lookUpKnowledgeEntity(String id) {
196     return nativeLookUpKnowledgeEntity(annotatorPtr, id);
197   }
198 
199   /** Frees up the allocated memory. */
200   @Override
close()201   public void close() {
202     if (isClosed.compareAndSet(false, true)) {
203       nativeCloseAnnotator(annotatorPtr);
204       annotatorPtr = 0L;
205     }
206   }
207 
208   @Override
finalize()209   protected void finalize() throws Throwable {
210     try {
211       close();
212     } finally {
213       super.finalize();
214     }
215   }
216 
217   /** Returns a comma separated list of locales supported by the model as BCP 47 tags. */
getLocales(int fd)218   public static String getLocales(int fd) {
219     return nativeGetLocales(fd);
220   }
221 
222   /** Returns the version of the model. */
getVersion(int fd)223   public static int getVersion(int fd) {
224     return nativeGetVersion(fd);
225   }
226 
227   /** Returns the name of the model. */
getName(int fd)228   public static String getName(int fd) {
229     return nativeGetName(fd);
230   }
231 
232   /** Information about a parsed time/date. */
233   public static final class DatetimeResult {
234 
235     public static final int GRANULARITY_YEAR = 0;
236     public static final int GRANULARITY_MONTH = 1;
237     public static final int GRANULARITY_WEEK = 2;
238     public static final int GRANULARITY_DAY = 3;
239     public static final int GRANULARITY_HOUR = 4;
240     public static final int GRANULARITY_MINUTE = 5;
241     public static final int GRANULARITY_SECOND = 6;
242 
243     private final long timeMsUtc;
244     private final int granularity;
245 
DatetimeResult(long timeMsUtc, int granularity)246     public DatetimeResult(long timeMsUtc, int granularity) {
247       this.timeMsUtc = timeMsUtc;
248       this.granularity = granularity;
249     }
250 
getTimeMsUtc()251     public long getTimeMsUtc() {
252       return timeMsUtc;
253     }
254 
getGranularity()255     public int getGranularity() {
256       return granularity;
257     }
258   }
259 
260   /** Classification result for classifyText method. */
261   public static final class ClassificationResult {
262     private final String collection;
263     private final float score;
264     private final DatetimeResult datetimeResult;
265     private final byte[] serializedKnowledgeResult;
266     private final String contactName;
267     private final String contactGivenName;
268     private final String contactFamilyName;
269     private final String contactNickname;
270     private final String contactEmailAddress;
271     private final String contactPhoneNumber;
272     private final String contactId;
273     private final String appName;
274     private final String appPackageName;
275     private final NamedVariant[] entityData;
276     private final byte[] serializedEntityData;
277     private final RemoteActionTemplate[] remoteActionTemplates;
278     private final long durationMs;
279     private final long numericValue;
280     private final double numericDoubleValue;
281 
ClassificationResult( String collection, float score, DatetimeResult datetimeResult, byte[] serializedKnowledgeResult, String contactName, String contactGivenName, String contactFamilyName, String contactNickname, String contactEmailAddress, String contactPhoneNumber, String contactId, String appName, String appPackageName, NamedVariant[] entityData, byte[] serializedEntityData, RemoteActionTemplate[] remoteActionTemplates, long durationMs, long numericValue, double numericDoubleValue)282     public ClassificationResult(
283         String collection,
284         float score,
285         DatetimeResult datetimeResult,
286         byte[] serializedKnowledgeResult,
287         String contactName,
288         String contactGivenName,
289         String contactFamilyName,
290         String contactNickname,
291         String contactEmailAddress,
292         String contactPhoneNumber,
293         String contactId,
294         String appName,
295         String appPackageName,
296         NamedVariant[] entityData,
297         byte[] serializedEntityData,
298         RemoteActionTemplate[] remoteActionTemplates,
299         long durationMs,
300         long numericValue,
301         double numericDoubleValue) {
302       this.collection = collection;
303       this.score = score;
304       this.datetimeResult = datetimeResult;
305       this.serializedKnowledgeResult = serializedKnowledgeResult;
306       this.contactName = contactName;
307       this.contactGivenName = contactGivenName;
308       this.contactFamilyName = contactFamilyName;
309       this.contactNickname = contactNickname;
310       this.contactEmailAddress = contactEmailAddress;
311       this.contactPhoneNumber = contactPhoneNumber;
312       this.contactId = contactId;
313       this.appName = appName;
314       this.appPackageName = appPackageName;
315       this.entityData = entityData;
316       this.serializedEntityData = serializedEntityData;
317       this.remoteActionTemplates = remoteActionTemplates;
318       this.durationMs = durationMs;
319       this.numericValue = numericValue;
320       this.numericDoubleValue = numericDoubleValue;
321     }
322 
323     /** Returns the classified entity type. */
getCollection()324     public String getCollection() {
325       return collection;
326     }
327 
328     /** Confidence score between 0 and 1. */
getScore()329     public float getScore() {
330       return score;
331     }
332 
getDatetimeResult()333     public DatetimeResult getDatetimeResult() {
334       return datetimeResult;
335     }
336 
getSerializedKnowledgeResult()337     public byte[] getSerializedKnowledgeResult() {
338       return serializedKnowledgeResult;
339     }
340 
getContactName()341     public String getContactName() {
342       return contactName;
343     }
344 
getContactGivenName()345     public String getContactGivenName() {
346       return contactGivenName;
347     }
348 
getContactFamilyName()349     public String getContactFamilyName() {
350       return contactFamilyName;
351     }
352 
getContactNickname()353     public String getContactNickname() {
354       return contactNickname;
355     }
356 
getContactEmailAddress()357     public String getContactEmailAddress() {
358       return contactEmailAddress;
359     }
360 
getContactPhoneNumber()361     public String getContactPhoneNumber() {
362       return contactPhoneNumber;
363     }
364 
getContactId()365     public String getContactId() {
366       return contactId;
367     }
368 
getAppName()369     public String getAppName() {
370       return appName;
371     }
372 
getAppPackageName()373     public String getAppPackageName() {
374       return appPackageName;
375     }
376 
getEntityData()377     public NamedVariant[] getEntityData() {
378       return entityData;
379     }
380 
getSerializedEntityData()381     public byte[] getSerializedEntityData() {
382       return serializedEntityData;
383     }
384 
getRemoteActionTemplates()385     public RemoteActionTemplate[] getRemoteActionTemplates() {
386       return remoteActionTemplates;
387     }
388 
getDurationMs()389     public long getDurationMs() {
390       return durationMs;
391     }
392 
getNumericValue()393     public long getNumericValue() {
394       return numericValue;
395     }
396 
getNumericDoubleValue()397     public double getNumericDoubleValue() {
398       return numericDoubleValue;
399     }
400   }
401 
402   /** Represents a result of Annotate call. */
403   public static final class AnnotatedSpan {
404     private final int startIndex;
405     private final int endIndex;
406     private final ClassificationResult[] classification;
407 
AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification)408     AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification) {
409       this.startIndex = startIndex;
410       this.endIndex = endIndex;
411       this.classification = classification;
412     }
413 
getStartIndex()414     public int getStartIndex() {
415       return startIndex;
416     }
417 
getEndIndex()418     public int getEndIndex() {
419       return endIndex;
420     }
421 
getClassification()422     public ClassificationResult[] getClassification() {
423       return classification;
424     }
425   }
426 
427   /** Represents a fragment of text to the AnnotateStructuredInput call. */
428   public static final class InputFragment {
429 
430     /** Encapsulates the data required to set the relative time of an InputFragment. */
431     public static final class DatetimeOptions {
432       private final String referenceTimezone;
433       private final Long referenceTimeMsUtc;
434 
DatetimeOptions(String referenceTimezone, Long referenceTimeMsUtc)435       public DatetimeOptions(String referenceTimezone, Long referenceTimeMsUtc) {
436         this.referenceTimeMsUtc = referenceTimeMsUtc;
437         this.referenceTimezone = referenceTimezone;
438       }
439     }
440 
InputFragment(String text)441     public InputFragment(String text) {
442       this.text = text;
443       this.datetimeOptionsNullable = null;
444     }
445 
InputFragment(String text, DatetimeOptions datetimeOptions)446     public InputFragment(String text, DatetimeOptions datetimeOptions) {
447       this.text = text;
448       this.datetimeOptionsNullable = datetimeOptions;
449     }
450 
451     private final String text;
452     // The DatetimeOptions can't be Optional because the _api16 build of the TCLib SDK does not
453     // support java.util.Optional.
454     private final DatetimeOptions datetimeOptionsNullable;
455 
getText()456     public String getText() {
457       return text;
458     }
459 
hasDatetimeOptions()460     public boolean hasDatetimeOptions() {
461       return datetimeOptionsNullable != null;
462     }
463 
getReferenceTimeMsUtc()464     public long getReferenceTimeMsUtc() {
465       return datetimeOptionsNullable.referenceTimeMsUtc;
466     }
467 
getReferenceTimezone()468     public String getReferenceTimezone() {
469       return datetimeOptionsNullable.referenceTimezone;
470     }
471   }
472 
473   /**
474    * Represents options for the suggestSelection call. TODO(b/63427420): Use location with Selection
475    * options.
476    */
477   public static final class SelectionOptions {
478     private final String locales;
479     private final String detectedTextLanguageTags;
480     private final int annotationUsecase;
481     private final double userLocationLat;
482     private final double userLocationLng;
483     private final float userLocationAccuracyMeters;
484 
SelectionOptions( String locales, String detectedTextLanguageTags, int annotationUsecase)485     public SelectionOptions(
486         String locales, String detectedTextLanguageTags, int annotationUsecase) {
487       this.locales = locales;
488       this.detectedTextLanguageTags = detectedTextLanguageTags;
489       this.annotationUsecase = annotationUsecase;
490       this.userLocationLat = INVALID_LATITUDE;
491       this.userLocationLng = INVALID_LONGITUDE;
492       this.userLocationAccuracyMeters = INVALID_LOCATION_ACCURACY_METERS;
493     }
494 
SelectionOptions(String locales, String detectedTextLanguageTags)495     public SelectionOptions(String locales, String detectedTextLanguageTags) {
496       this(locales, detectedTextLanguageTags, AnnotationUsecase.SMART.getValue());
497     }
498 
getLocales()499     public String getLocales() {
500       return locales;
501     }
502 
503     /** Returns a comma separated list of BCP 47 language tags. */
getDetectedTextLanguageTags()504     public String getDetectedTextLanguageTags() {
505       return detectedTextLanguageTags;
506     }
507 
getAnnotationUsecase()508     public int getAnnotationUsecase() {
509       return annotationUsecase;
510     }
511 
getUserLocationLat()512     public double getUserLocationLat() {
513       return userLocationLat;
514     }
515 
getUserLocationLng()516     public double getUserLocationLng() {
517       return userLocationLng;
518     }
519 
getUserLocationAccuracyMeters()520     public float getUserLocationAccuracyMeters() {
521       return userLocationAccuracyMeters;
522     }
523   }
524 
525   /**
526    * Represents options for the classifyText call. TODO(b/63427420): Use location with
527    * Classification options.
528    */
529   public static final class ClassificationOptions {
530     private final long referenceTimeMsUtc;
531     private final String referenceTimezone;
532     private final String locales;
533     private final String detectedTextLanguageTags;
534     private final int annotationUsecase;
535     private final double userLocationLat;
536     private final double userLocationLng;
537     private final float userLocationAccuracyMeters;
538     private final String userFamiliarLanguageTags;
539 
ClassificationOptions( long referenceTimeMsUtc, String referenceTimezone, String locales, String detectedTextLanguageTags, int annotationUsecase, String userFamiliarLanguageTags)540     public ClassificationOptions(
541         long referenceTimeMsUtc,
542         String referenceTimezone,
543         String locales,
544         String detectedTextLanguageTags,
545         int annotationUsecase,
546         String userFamiliarLanguageTags) {
547       this.referenceTimeMsUtc = referenceTimeMsUtc;
548       this.referenceTimezone = referenceTimezone;
549       this.locales = locales;
550       this.detectedTextLanguageTags = detectedTextLanguageTags;
551       this.annotationUsecase = annotationUsecase;
552       this.userLocationLat = INVALID_LATITUDE;
553       this.userLocationLng = INVALID_LONGITUDE;
554       this.userLocationAccuracyMeters = INVALID_LOCATION_ACCURACY_METERS;
555       this.userFamiliarLanguageTags = userFamiliarLanguageTags;
556     }
557 
ClassificationOptions( long referenceTimeMsUtc, String referenceTimezone, String locales, String detectedTextLanguageTags)558     public ClassificationOptions(
559         long referenceTimeMsUtc,
560         String referenceTimezone,
561         String locales,
562         String detectedTextLanguageTags) {
563       this(
564           referenceTimeMsUtc,
565           referenceTimezone,
566           locales,
567           detectedTextLanguageTags,
568           AnnotationUsecase.SMART.getValue(),
569           "");
570     }
571 
getReferenceTimeMsUtc()572     public long getReferenceTimeMsUtc() {
573       return referenceTimeMsUtc;
574     }
575 
getReferenceTimezone()576     public String getReferenceTimezone() {
577       return referenceTimezone;
578     }
579 
getLocale()580     public String getLocale() {
581       return locales;
582     }
583 
584     /** Returns a comma separated list of BCP 47 language tags. */
getDetectedTextLanguageTags()585     public String getDetectedTextLanguageTags() {
586       return detectedTextLanguageTags;
587     }
588 
getAnnotationUsecase()589     public int getAnnotationUsecase() {
590       return annotationUsecase;
591     }
592 
getUserLocationLat()593     public double getUserLocationLat() {
594       return userLocationLat;
595     }
596 
getUserLocationLng()597     public double getUserLocationLng() {
598       return userLocationLng;
599     }
600 
getUserLocationAccuracyMeters()601     public float getUserLocationAccuracyMeters() {
602       return userLocationAccuracyMeters;
603     }
604 
getUserFamiliarLanguageTags()605     public String getUserFamiliarLanguageTags() {
606       return userFamiliarLanguageTags;
607     }
608   }
609 
610   /** Represents options for the annotate call. */
611   public static final class AnnotationOptions {
612     private final long referenceTimeMsUtc;
613     private final String referenceTimezone;
614     private final String locales;
615     private final String detectedTextLanguageTags;
616     private final String[] entityTypes;
617     private final int annotationUsecase;
618     private final boolean hasLocationPermission;
619     private final boolean hasPersonalizationPermission;
620     private final boolean isSerializedEntityDataEnabled;
621     private final double userLocationLat;
622     private final double userLocationLng;
623     private final float userLocationAccuracyMeters;
624 
AnnotationOptions( long referenceTimeMsUtc, String referenceTimezone, String locales, String detectedTextLanguageTags, Collection<String> entityTypes, int annotationUsecase, boolean hasLocationPermission, boolean hasPersonalizationPermission, boolean isSerializedEntityDataEnabled, double userLocationLat, double userLocationLng, float userLocationAccuracyMeters)625     public AnnotationOptions(
626         long referenceTimeMsUtc,
627         String referenceTimezone,
628         String locales,
629         String detectedTextLanguageTags,
630         Collection<String> entityTypes,
631         int annotationUsecase,
632         boolean hasLocationPermission,
633         boolean hasPersonalizationPermission,
634         boolean isSerializedEntityDataEnabled,
635         double userLocationLat,
636         double userLocationLng,
637         float userLocationAccuracyMeters) {
638       this.referenceTimeMsUtc = referenceTimeMsUtc;
639       this.referenceTimezone = referenceTimezone;
640       this.locales = locales;
641       this.detectedTextLanguageTags = detectedTextLanguageTags;
642       this.entityTypes = entityTypes == null ? new String[0] : entityTypes.toArray(new String[0]);
643       this.annotationUsecase = annotationUsecase;
644       this.isSerializedEntityDataEnabled = isSerializedEntityDataEnabled;
645       this.userLocationLat = userLocationLat;
646       this.userLocationLng = userLocationLng;
647       this.userLocationAccuracyMeters = userLocationAccuracyMeters;
648       this.hasLocationPermission = hasLocationPermission;
649       this.hasPersonalizationPermission = hasPersonalizationPermission;
650     }
651 
AnnotationOptions( long referenceTimeMsUtc, String referenceTimezone, String locales, String detectedTextLanguageTags, Collection<String> entityTypes, int annotationUsecase, boolean isSerializedEntityDataEnabled, double userLocationLat, double userLocationLng, float userLocationAccuracyMeters)652     public AnnotationOptions(
653         long referenceTimeMsUtc,
654         String referenceTimezone,
655         String locales,
656         String detectedTextLanguageTags,
657         Collection<String> entityTypes,
658         int annotationUsecase,
659         boolean isSerializedEntityDataEnabled,
660         double userLocationLat,
661         double userLocationLng,
662         float userLocationAccuracyMeters) {
663       this(
664           referenceTimeMsUtc,
665           referenceTimezone,
666           locales,
667           detectedTextLanguageTags,
668           entityTypes,
669           annotationUsecase,
670           /* hasLocationPermission */ true,
671           /* hasPersonalizationPermission */ true,
672           isSerializedEntityDataEnabled,
673           userLocationLat,
674           userLocationLng,
675           userLocationAccuracyMeters);
676     }
677 
AnnotationOptions( long referenceTimeMsUtc, String referenceTimezone, String locales, String detectedTextLanguageTags, Collection<String> entityTypes, int annotationUsecase, boolean isSerializedEntityDataEnabled)678     public AnnotationOptions(
679         long referenceTimeMsUtc,
680         String referenceTimezone,
681         String locales,
682         String detectedTextLanguageTags,
683         Collection<String> entityTypes,
684         int annotationUsecase,
685         boolean isSerializedEntityDataEnabled) {
686       this(
687           referenceTimeMsUtc,
688           referenceTimezone,
689           locales,
690           detectedTextLanguageTags,
691           entityTypes,
692           annotationUsecase,
693           isSerializedEntityDataEnabled,
694           INVALID_LATITUDE,
695           INVALID_LONGITUDE,
696           INVALID_LOCATION_ACCURACY_METERS);
697     }
698 
AnnotationOptions( long referenceTimeMsUtc, String referenceTimezone, String locales, String detectedTextLanguageTags)699     public AnnotationOptions(
700         long referenceTimeMsUtc,
701         String referenceTimezone,
702         String locales,
703         String detectedTextLanguageTags) {
704       this(
705           referenceTimeMsUtc,
706           referenceTimezone,
707           locales,
708           detectedTextLanguageTags,
709           null,
710           AnnotationUsecase.SMART.getValue(),
711           /* isSerializedEntityDataEnabled */ false);
712     }
713 
getReferenceTimeMsUtc()714     public long getReferenceTimeMsUtc() {
715       return referenceTimeMsUtc;
716     }
717 
getReferenceTimezone()718     public String getReferenceTimezone() {
719       return referenceTimezone;
720     }
721 
getLocale()722     public String getLocale() {
723       return locales;
724     }
725 
726     /** Returns a comma separated list of BCP 47 language tags. */
getDetectedTextLanguageTags()727     public String getDetectedTextLanguageTags() {
728       return detectedTextLanguageTags;
729     }
730 
getEntityTypes()731     public String[] getEntityTypes() {
732       return entityTypes;
733     }
734 
getAnnotationUsecase()735     public int getAnnotationUsecase() {
736       return annotationUsecase;
737     }
738 
isSerializedEntityDataEnabled()739     public boolean isSerializedEntityDataEnabled() {
740       return isSerializedEntityDataEnabled;
741     }
742 
getUserLocationLat()743     public double getUserLocationLat() {
744       return userLocationLat;
745     }
746 
getUserLocationLng()747     public double getUserLocationLng() {
748       return userLocationLng;
749     }
750 
getUserLocationAccuracyMeters()751     public float getUserLocationAccuracyMeters() {
752       return userLocationAccuracyMeters;
753     }
754 
hasLocationPermission()755     public boolean hasLocationPermission() {
756       return hasLocationPermission;
757     }
758 
hasPersonalizationPermission()759     public boolean hasPersonalizationPermission() {
760       return hasPersonalizationPermission;
761     }
762   }
763 
764   /**
765    * Retrieves the pointer to the native object. Note: Need to keep the AnnotatorModel alive as long
766    * as the pointer is used.
767    */
getNativeAnnotatorPointer()768   long getNativeAnnotatorPointer() {
769     return nativeGetNativeModelPtr(annotatorPtr);
770   }
771 
nativeNewAnnotator(int fd)772   private static native long nativeNewAnnotator(int fd);
773 
nativeNewAnnotatorFromPath(String path)774   private static native long nativeNewAnnotatorFromPath(String path);
775 
nativeNewAnnotatorWithOffset(int fd, long offset, long size)776   private static native long nativeNewAnnotatorWithOffset(int fd, long offset, long size);
777 
nativeGetLocales(int fd)778   private static native String nativeGetLocales(int fd);
779 
nativeGetLocalesWithOffset(int fd, long offset, long size)780   private static native String nativeGetLocalesWithOffset(int fd, long offset, long size);
781 
nativeGetVersion(int fd)782   private static native int nativeGetVersion(int fd);
783 
nativeGetVersionWithOffset(int fd, long offset, long size)784   private static native int nativeGetVersionWithOffset(int fd, long offset, long size);
785 
nativeGetName(int fd)786   private static native String nativeGetName(int fd);
787 
nativeGetNameWithOffset(int fd, long offset, long size)788   private static native String nativeGetNameWithOffset(int fd, long offset, long size);
789 
nativeGetNativeModelPtr(long context)790   private native long nativeGetNativeModelPtr(long context);
791 
nativeInitializeKnowledgeEngine(long context, byte[] serializedConfig)792   private native boolean nativeInitializeKnowledgeEngine(long context, byte[] serializedConfig);
793 
nativeInitializeContactEngine(long context, byte[] serializedConfig)794   private native boolean nativeInitializeContactEngine(long context, byte[] serializedConfig);
795 
nativeInitializeInstalledAppEngine(long context, byte[] serializedConfig)796   private native boolean nativeInitializeInstalledAppEngine(long context, byte[] serializedConfig);
797 
nativeInitializePersonNameEngine( long context, int fd, long offset, long size)798   private native boolean nativeInitializePersonNameEngine(
799       long context, int fd, long offset, long size);
800 
nativeSetLangId(long annotatorPtr, long langIdPtr)801   private native void nativeSetLangId(long annotatorPtr, long langIdPtr);
802 
nativeSuggestSelection( long context, String text, int selectionBegin, int selectionEnd, SelectionOptions options)803   private native int[] nativeSuggestSelection(
804       long context, String text, int selectionBegin, int selectionEnd, SelectionOptions options);
805 
nativeClassifyText( long context, String text, int selectionBegin, int selectionEnd, ClassificationOptions options, Object appContext, String resourceLocales)806   private native ClassificationResult[] nativeClassifyText(
807       long context,
808       String text,
809       int selectionBegin,
810       int selectionEnd,
811       ClassificationOptions options,
812       Object appContext,
813       String resourceLocales);
814 
nativeAnnotate( long context, String text, AnnotationOptions options)815   private native AnnotatedSpan[] nativeAnnotate(
816       long context, String text, AnnotationOptions options);
817 
nativeAnnotateStructuredInput( long context, InputFragment[] inputFragments, AnnotationOptions options)818   private native AnnotatedSpan[][] nativeAnnotateStructuredInput(
819       long context, InputFragment[] inputFragments, AnnotationOptions options);
820 
nativeLookUpKnowledgeEntity(long context, String id)821   private native byte[] nativeLookUpKnowledgeEntity(long context, String id);
822 
nativeCloseAnnotator(long context)823   private native void nativeCloseAnnotator(long context);
824 }
825