blob: 2c93a19bbe0e8c58801511d271611f7010405fab [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
Lukas Zilka9d6e9d72017-10-18 11:39:13 +020019import android.content.res.AssetFileDescriptor;
20
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080021/**
22 * Java wrapper for SmartSelection native library interface.
23 * This library is used for detecting entities in text.
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080024 */
Abodunrinwa Tokif67f0a52017-02-09 22:41:58 +000025final class SmartSelection {
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080026
27 static {
Abodunrinwa Toki099ff112017-03-02 20:43:01 +000028 System.loadLibrary("textclassifier");
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080029 }
30
Abodunrinwa Tokid2d13992017-03-24 21:43:13 +000031 /** Hints the classifier that this may be a url. */
32 static final int HINT_FLAG_URL = 0x01;
33 /** Hints the classifier that this may be an email. */
34 static final int HINT_FLAG_EMAIL = 0x02;
35
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080036 private final long mCtx;
37
38 /**
39 * Creates a new instance of SmartSelect predictor, using the provided model image,
40 * given as a file descriptor.
41 */
Abodunrinwa Tokif67f0a52017-02-09 22:41:58 +000042 SmartSelection(int fd) {
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080043 mCtx = nativeNew(fd);
44 }
45
46 /**
Lukas Zilka9d6e9d72017-10-18 11:39:13 +020047 * Creates a new instance of SmartSelect predictor, using the provided model image, given as a
48 * file path.
49 */
50 SmartSelection(String path) {
51 mCtx = nativeNewFromPath(path);
52 }
53
54 /**
55 * Creates a new instance of SmartSelect predictor, using the provided model image, given as an
56 * AssetFileDescriptor.
57 */
58 SmartSelection(AssetFileDescriptor afd) {
59 mCtx = nativeNewFromAssetFileDescriptor(afd, afd.getStartOffset(), afd.getLength());
60 if (mCtx == 0L) {
61 throw new IllegalArgumentException(
62 "Couldn't initialize TC from given AssetFileDescriptor");
63 }
64 }
65
66 /**
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080067 * Given a string context and current selection, computes the SmartSelection suggestion.
68 *
69 * The begin and end are character indices into the context UTF8 string. selectionBegin is the
70 * character index where the selection begins, and selectionEnd is the index of one character
71 * past the selection span.
72 *
73 * The return value is an array of two ints: suggested selection beginning and end, with the
74 * same semantics as the input selectionBeginning and selectionEnd.
75 */
76 public int[] suggest(String context, int selectionBegin, int selectionEnd) {
77 return nativeSuggest(mCtx, context, selectionBegin, selectionEnd);
78 }
79
80 /**
81 * Given a string context and current selection, classifies the type of the selected text.
82 *
83 * The begin and end params are character indices in the context string.
84 *
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +000085 * Returns an array of ClassificationResult objects with the probability
86 * scores for different collections.
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080087 */
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +000088 public ClassificationResult[] classifyText(
Abodunrinwa Tokid2d13992017-03-24 21:43:13 +000089 String context, int selectionBegin, int selectionEnd, int hintFlags) {
Abodunrinwa Tokib5ab2592017-03-27 21:09:23 +010090 return nativeClassifyText(mCtx, context, selectionBegin, selectionEnd, hintFlags);
Abodunrinwa Toki43e03502017-01-13 13:46:33 -080091 }
92
93 /**
Lukas Zilka9d6e9d72017-10-18 11:39:13 +020094 * Annotates given input text. Every word of the input is a part of some annotation.
95 * The annotations are sorted by their position in the context string.
96 * The annotations do not overlap.
97 */
98 public AnnotatedSpan[] annotate(String text) {
99 return nativeAnnotate(mCtx, text);
100 }
101
102 /**
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800103 * Frees up the allocated memory.
104 */
105 public void close() {
106 nativeClose(mCtx);
107 }
108
Abodunrinwa Toki146d0d42017-04-25 01:39:19 +0100109 /**
110 * Returns the language of the model.
111 */
112 public static String getLanguage(int fd) {
113 return nativeGetLanguage(fd);
114 }
115
116 /**
117 * Returns the version of the model.
118 */
119 public static int getVersion(int fd) {
120 return nativeGetVersion(fd);
121 }
122
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800123 private static native long nativeNew(int fd);
124
Lukas Zilka9d6e9d72017-10-18 11:39:13 +0200125 private static native long nativeNewFromPath(String path);
126
127 private static native long nativeNewFromAssetFileDescriptor(AssetFileDescriptor afd,
128 long offset, long size);
129
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800130 private static native int[] nativeSuggest(
131 long context, String text, int selectionBegin, int selectionEnd);
132
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000133 private static native ClassificationResult[] nativeClassifyText(
Abodunrinwa Tokib5ab2592017-03-27 21:09:23 +0100134 long context, String text, int selectionBegin, int selectionEnd, int hintFlags);
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800135
Lukas Zilka9d6e9d72017-10-18 11:39:13 +0200136 private static native AnnotatedSpan[] nativeAnnotate(long context, String text);
137
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800138 private static native void nativeClose(long context);
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000139
Abodunrinwa Toki146d0d42017-04-25 01:39:19 +0100140 private static native String nativeGetLanguage(int fd);
141
142 private static native int nativeGetVersion(int fd);
143
Abodunrinwa Tokia6096f62017-03-08 17:21:40 +0000144 /** Classification result for classifyText method. */
145 static final class ClassificationResult {
146 final String mCollection;
147 /** float range: 0 - 1 */
148 final float mScore;
149
150 ClassificationResult(String collection, float score) {
151 mCollection = collection;
152 mScore = score;
153 }
154 }
Lukas Zilka9d6e9d72017-10-18 11:39:13 +0200155
156 /** Represents a result of Annotate call. */
157 public static final class AnnotatedSpan {
158 final int mStartIndex;
159 final int mEndIndex;
160 final ClassificationResult[] mClassification;
161
162 AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification) {
163 mStartIndex = startIndex;
164 mEndIndex = endIndex;
165 mClassification = classification;
166 }
167
168 public int getStartIndex() {
169 return mStartIndex;
170 }
171
172 public int getEndIndex() {
173 return mEndIndex;
174 }
175
176 public ClassificationResult[] getClassification() {
177 return mClassification;
178 }
179 }
Abodunrinwa Toki43e03502017-01-13 13:46:33 -0800180}