blob: 69c38ee4db4feb07447cfb4d9022a5e509318597 [file] [log] [blame]
Abodunrinwa Toki43e03502017-01-13 13:46:33 -08001/*
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
Abodunrinwa Tokif67f0a52017-02-09 22:41:58 +000017package android.view.textclassifier;
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080018
Jan Althaus705b9e92018-01-22 18:22:29 +010019import android.annotation.Nullable;
Lukas Zilka9d6e9d72017-10-18 11:39:13 +020020import android.content.res.AssetFileDescriptor;
21
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080022/**
23 * Java wrapper for SmartSelection native library interface.
24 * This library is used for detecting entities in text.
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080025 */
Abodunrinwa Tokif67f0a52017-02-09 22:41:58 +000026final class SmartSelection {
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080027
28 static {
Abodunrinwa Toki099ff112017-03-02 20:43:01 +000029 System.loadLibrary("textclassifier");
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080030 }
31
Abodunrinwa Tokid2d13992017-03-24 21:43:13 +000032 /** Hints the classifier that this may be a url. */
33 static final int HINT_FLAG_URL = 0x01;
34 /** Hints the classifier that this may be an email. */
35 static final int HINT_FLAG_EMAIL = 0x02;
36
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080037 private final long mCtx;
38
39 /**
40 * Creates a new instance of SmartSelect predictor, using the provided model image,
41 * given as a file descriptor.
42 */
Abodunrinwa Tokif67f0a52017-02-09 22:41:58 +000043 SmartSelection(int fd) {
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080044 mCtx = nativeNew(fd);
45 }
46
47 /**
Lukas Zilka9d6e9d72017-10-18 11:39:13 +020048 * Creates a new instance of SmartSelect predictor, using the provided model image, given as a
49 * file path.
50 */
51 SmartSelection(String path) {
52 mCtx = nativeNewFromPath(path);
53 }
54
55 /**
56 * Creates a new instance of SmartSelect predictor, using the provided model image, given as an
57 * AssetFileDescriptor.
58 */
59 SmartSelection(AssetFileDescriptor afd) {
60 mCtx = nativeNewFromAssetFileDescriptor(afd, afd.getStartOffset(), afd.getLength());
61 if (mCtx == 0L) {
62 throw new IllegalArgumentException(
63 "Couldn't initialize TC from given AssetFileDescriptor");
64 }
65 }
66
67 /**
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080068 * Given a string context and current selection, computes the SmartSelection suggestion.
69 *
70 * The begin and end are character indices into the context UTF8 string. selectionBegin is the
71 * character index where the selection begins, and selectionEnd is the index of one character
72 * past the selection span.
73 *
74 * 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[] suggest(String context, int selectionBegin, int selectionEnd) {
78 return nativeSuggest(mCtx, context, selectionBegin, selectionEnd);
79 }
80
81 /**
82 * Given a string context and current selection, classifies the type of the selected text.
83 *
84 * The begin and end params are character indices in the context string.
85 *
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +000086 * Returns an array of ClassificationResult objects with the probability
87 * scores for different collections.
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080088 */
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +000089 public ClassificationResult[] classifyText(
Abodunrinwa Tokid2d13992017-03-24 21:43:13 +000090 String context, int selectionBegin, int selectionEnd, int hintFlags) {
Abodunrinwa Tokib5ab2592017-03-27 21:09:23 +010091 return nativeClassifyText(mCtx, context, selectionBegin, selectionEnd, hintFlags);
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080092 }
93
94 /**
Lukas Zilka9d6e9d72017-10-18 11:39:13 +020095 * Annotates given input text. Every word of the input is a part of some annotation.
96 * The annotations are sorted by their position in the context string.
97 * The annotations do not overlap.
98 */
99 public AnnotatedSpan[] annotate(String text) {
100 return nativeAnnotate(mCtx, text);
101 }
102
103 /**
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800104 * Frees up the allocated memory.
105 */
106 public void close() {
107 nativeClose(mCtx);
108 }
109
Abodunrinwa Toki146d0d42017-04-25 01:39:19 +0100110 /**
Jan Althausef0156d2018-01-29 19:28:41 +0100111 * Returns a comma separated list of locales supported by the model as BCP 47 tags.
Abodunrinwa Toki146d0d42017-04-25 01:39:19 +0100112 */
Jan Althausef0156d2018-01-29 19:28:41 +0100113 public static String getLanguages(int fd) {
Abodunrinwa Toki146d0d42017-04-25 01:39:19 +0100114 return nativeGetLanguage(fd);
115 }
116
117 /**
118 * Returns the version of the model.
119 */
120 public static int getVersion(int fd) {
121 return nativeGetVersion(fd);
122 }
123
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800124 private static native long nativeNew(int fd);
125
Lukas Zilka9d6e9d72017-10-18 11:39:13 +0200126 private static native long nativeNewFromPath(String path);
127
128 private static native long nativeNewFromAssetFileDescriptor(AssetFileDescriptor afd,
129 long offset, long size);
130
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800131 private static native int[] nativeSuggest(
132 long context, String text, int selectionBegin, int selectionEnd);
133
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000134 private static native ClassificationResult[] nativeClassifyText(
Abodunrinwa Tokib5ab2592017-03-27 21:09:23 +0100135 long context, String text, int selectionBegin, int selectionEnd, int hintFlags);
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800136
Lukas Zilka9d6e9d72017-10-18 11:39:13 +0200137 private static native AnnotatedSpan[] nativeAnnotate(long context, String text);
138
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800139 private static native void nativeClose(long context);
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000140
Abodunrinwa Toki146d0d42017-04-25 01:39:19 +0100141 private static native String nativeGetLanguage(int fd);
142
143 private static native int nativeGetVersion(int fd);
144
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000145 /** Classification result for classifyText method. */
146 static final class ClassificationResult {
147 final String mCollection;
148 /** float range: 0 - 1 */
149 final float mScore;
Jan Althaus705b9e92018-01-22 18:22:29 +0100150 @Nullable final DatetimeParseResult mDatetime;
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000151
152 ClassificationResult(String collection, float score) {
153 mCollection = collection;
154 mScore = score;
Jan Althaus705b9e92018-01-22 18:22:29 +0100155 mDatetime = null;
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000156 }
Jan Althaus705b9e92018-01-22 18:22:29 +0100157
158 ClassificationResult(String collection, float score, DatetimeParseResult datetime) {
159 mCollection = collection;
160 mScore = score;
161 mDatetime = datetime;
162 }
163 }
164
165 /** Parsed date information for the classification result. */
166 static final class DatetimeParseResult {
167 long mMsSinceEpoch;
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000168 }
Lukas Zilka9d6e9d72017-10-18 11:39:13 +0200169
170 /** Represents a result of Annotate call. */
171 public static final class AnnotatedSpan {
172 final int mStartIndex;
173 final int mEndIndex;
174 final ClassificationResult[] mClassification;
175
176 AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification) {
177 mStartIndex = startIndex;
178 mEndIndex = endIndex;
179 mClassification = classification;
180 }
181
182 public int getStartIndex() {
183 return mStartIndex;
184 }
185
186 public int getEndIndex() {
187 return mEndIndex;
188 }
189
190 public ClassificationResult[] getClassification() {
191 return mClassification;
192 }
193 }
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800194}